Click here to Skip to main content
15,898,036 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have populated a DropDownList with values from an SQL statement and it works.

VB
Try
     cmdStr = "SELECT [idt],[col1] FROM [test];"
     Using conn As New SqlConnection(connStr)
         Using cmd As New SqlCommand(cmdStr, conn)
             conn.Open()
             cmd.ExecuteNonQuery()
             Using da As New SqlDataAdapter(cmd)
                 da.Fill(ds)
                 DropDownList1.DataSource = ds.Tables(0)
                 DropDownList1.DataTextField = "idt"
                 DropDownList1.DataValueField = "col1"
                 DropDownList1.DataBind()
             End Using
             cmd.Dispose()
             conn.Close()
             conn.Dispose()
         End Using
     End Using
 Catch ex As Exception
     TextBox1.Text = ex.Message
 End Try
The problem is the SQL list starts with idt=68 but DropDownList1 equals DropDownList1.DataTextField="" instead of 68.

VB
Try
   cmdStr = "SELECT [datetime],[col1],[col2],[col3] FROM [test] WHERE [idt]=@idt;"
   Using conn As New SqlConnection(connStr)
       Using cmd As New SqlCommand(cmdStr, conn)
           conn.Open()
           cmd.Parameters.AddWithValue("@idt", DropDownList1.DataTextField)
           cmd.ExecuteNonQuery()
 ...


How do I retrieve the number 68?
Posted
Updated 1-Apr-14 14:08pm
v3
Comments
CHill60 1-Apr-14 20:00pm    
Not clear. We cannot see your database nor your dropdownlist. How have you populated your dropdownlist ... if the text = 1 and not 68 then the populate clearly doesn't work as you expected it to. Post some code before we can help
Ajay_Babu 2-Apr-14 0:42am    
Which values are you store in DropDownList1.DataTextField, DropDownList1.DataValueField.what the values of idt,col1?

cmd.Parameters.AddWithValue("@idt", DropDownList1.DataValueField)
 
Share this answer
 
cmd.Parameters.AddWithValue("@idt", DropDownList1.SelectedItem.Text)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900