Click to See Complete Forum and Search --> : dropdown list


LovelyHelp
February 5th, 2006, 09:55 PM
how to create dropdown list. I am using visual studio 2005 and i want to create dropdown list for gender and work classification in the same form.

how should I create my database which i am using sql 2000 and how should I retieve my items to my dropdown list in vb languages

HairyMonkeyMan
February 6th, 2006, 06:13 AM
You should define your drop down list like this:
<asp:DropDownList ID="ddlGender" runat="server">
</asp:DropDownList>

<!--
Note, you can also add items to your list here (ones that don't need to be pulled from the database! Like as follows
-->

<asp:DropDownList ID="ddlGender" runat="server">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:DropDownList>

If you need to bring items from your database to your list.... there are a few ways to do this, I find the easiest way (using sql server) is to create an sqldatasource and then write a databinding routine.

<asp:SqlDataSource ID="Classification" runat="server" ConnectionString="<%$ ConnectionStrings:MyDbConn1 %>"
ProviderName="<%$ ConnectionStrings:MyDbConn1.ProviderName %>" SelectCommand="SELECT ClassID, ClassDesc FROM Classification ORDER BY ClassDesc">
</asp:SqlDataSource>

Sub BindStaff()
ddlClass.DataSource = Classification
ddlClass.DataValueField = "ClassID"
ddlClass.DataTextField = "ClassDesc"
ddlClass.DataBind()

' insert blank choice on top of the staff dropdownlist.
ddlClass.Items.Insert(0, New ListItem("Select Classification...", [String].Empty))
End Sub

Anyway, good luck :wave:

LovelyHelp
February 6th, 2006, 10:35 AM
I couldn't understand your code. For sqlDataSource is it a new function in vb 2005?

my code is something like this for classification
--------------------------------------------------------------------------------------------

Sub Page_Load()
If Not IsPostBack Then


conUserLogin = New SqlConnection("server=localhost;UID=userlogin;psw=119478;database=userLogin")
conUserLogin.Open()
cmdSelect = New SqlCommand("selectuser_classification from t_classification", conUserLogin)


ddlClassification.DataSource = dtrclassification
ddlClassification.DataTextField = "user_classification"
ddlClassification.DataBind()

dtrclassification.Close()
conUserLogin.Close()

End If
End Sub

<asp:DropDownList ID="ddlClassification" runat="server" Width="86px"></asp:DropDownList>
-------------------------------------------------------------------------------------------

HairyMonkeyMan
February 6th, 2006, 11:52 AM
I think (although not totally sure) that the sqldatasource object also works in asp.net 1.0 - although, I have been using vwd2005 to develop...

Your code looks ok to me.... What errors are you getting?

conUserLogin = New SqlConnection("server=localhost;UID=userlogin;psw=******;database=userLogin")


Have you Dimmed this globally?

ps: I would mask your database passwords if i were you

LovelyHelp
February 6th, 2006, 09:36 PM
i couldnt get data from my database..the is no item in my drop down list.

LovelyHelp
February 6th, 2006, 09:41 PM
what do you mean by dimmend globally?

thanks for the password reminder :D

HairyMonkeyMan
February 7th, 2006, 03:39 AM
Hi,

By globally, I was meaning - do you have a statement like this in your code?
Dim conUserLogin As New SqlConnection("server=localhost;UID=userlogin;psw=******;database=userLogin")

And the same thing goes for your command object

Dim cmdSelect As New SqlCommand("selectuser_classification from t_classification", conUserLogin)

Is the 'dtrclassification' object a datareader? If so, you are going to need to fill the datareader with data from the database... Like this:
dtrclassification = cmdSelect.ExecuteReader()

Hope this helps :thumb:

ineed
February 7th, 2006, 06:55 AM
everything goes in this way but still have no item in my dropdown list.
Do I need to create any virtual directry point to my database? If yes, how to do it.

as now, i only create my database and i call my database using sqlConnection.

<script runat="server">
Dim conUserLogin As SqlConnection
Dim cmdSelect1 As SqlCommand
Dim cmdSelect As SqlCommand
Dim parmReturnValue As SqlParameter
Dim intResult As Integer
Dim dtrclassification As SqlDataReader

Sub Page_Load()
If Not IsPostBack Then


conUserLogin = New SqlConnection("server=localhost;UID=userlogin;psw=119478;database=userLogin")
conUserLogin.Open()
cmdSelect = New SqlCommand("select user_classification from t_classification", conUserLogin)
dtrclassification = cmdSelect.ExecuteReader()

ddlClassification.DataSource = dtrclassification
ddlClassification.DataTextField = "user_classification"
'ddlClassification.DataValueField = "id"
ddlClassification.DataBind()

dtrclassification.Close()
conUserLogin.Close()

End If
End Sub

HairyMonkeyMan
February 7th, 2006, 07:55 AM
I just had a thought there...

In one of my applications, I had to create a user in sql server for 'NT AUTHORITY\NETWORK SERVICE' and give it the following permissions 'public', 'db_datareaded', 'db_datawriter'...

I can't see any reason why your code won't work... must be something at the database end of things.