How to bind a DropDownList to a database field

flann

Member
Joined
Jun 8, 2005
Messages
20
Programming Experience
Beginner
I'm trying to bind a ddl with a database field that is a Yes or No field. I can't seem to figure out how to make it work. In design view, I selected the field from the databindings, and then in code view put the normal...

VB.NET:
		    Me.SqlDataAdapter1.SelectCommand.Parameters("@LeadID").Value() = intLeadid
			Me.SqlDataAdapter1.Fill(DsLegal1.ClientInfo)
			Me.DataBind()

My dropdownlist isn't showing me anything, shouldn't it automatically populate the dropdownlist?
 
Ok I use a datareader to poulate ddl's

VB.NET:
[size=2][color=#0000ff]Dim[/color][/size][size=2] cmdSelect [/size][size=2][color=#0000ff]As[/color][/size][size=2] SqlCommand

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] reader [/size][size=2][color=#0000ff]As[/color][/size][size=2] SqlDataReader

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] conn [/size][size=2][color=#0000ff]As[/color][/size][size=2] SqlConnection = [/size][size=2][color=#0000ff]New[/color][/size][size=2] SqlConnection([/size][size=2][color=#800000]"Data Source=.\SQLEXPRESS;AttachDbFilename=|"[/color][/size][size=2] _

& [/size][size=2][color=#800000]"DataDirectory|\Procedures.mdf;Integrated Security=True;User Instance=True"[/color][/size][size=2])

conn.Open()

cmdSelect = [/size][size=2][color=#0000ff]New[/color][/size][size=2] SqlCommand([/size][size=2][color=#800000]"sp_selectZoneTimes "[/color][/size][size=2], conn)

cmdSelect.CommandType = CommandType.StoredProcedure

cmdSelect.Parameters.Add([/size][size=2][color=#0000ff]New[/color][/size][size=2] SqlParameter([/size][size=2][color=#800000]"@ZoneID"[/color][/size][size=2], SqlDbType.Int, 4))

cmdSelect.Parameters([/size][size=2][color=#800000]"@ZoneID"[/color][/size][size=2]).Value = CodeClass.GetZoneId([/size][size=2][color=#0000ff]CInt[/color][/size][size=2](TextBoxCustNo.Text)) [/size][size=2][color=#008000]'calls GetZoneID function to return the customers zone

[/color][/size][size=2]reader = cmdSelect.ExecuteReader()

[/size][size=2][color=#0000ff]While[/color][/size][size=2] reader.Read

[/size][size=2]DropDownListSlotID.Items.Add(reader([/size][size=2][color=#800000]"Time"[/color][/size][size=2]))

[/size][size=2][color=#0000ff]

[/color][/size][size=2][/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]While

[/color][/size]

Right this is a bit different to what you might do but the only difference is that I use a stored procedure instead of an SQL string. The principle is the same though so if you look at it you should be able to change it to suit your needs
 
Back
Top