Dropdownlist problem...

S

Scott D

I am currently binding my dropdownlists from a database using the
logic below. And they are populated correctly.

My problem is when I go to submit the information to the database
using Me.ddSID.DataValueField no matter what is selected from the
dropdown a blank string is passed to the methods within my codebehind.
I thought after binding in this fashion I would be passed the
information in the VALUE field which should be the SID. Any ideas why
I am not being passed the value?

Private Sub ddSIDBind()
Dim connString As String =
"server=servername;uid=UN;pwd=PWD;Initial Catalog=DB"
Dim conn As SqlConnection = New SqlConnection(connString)
Dim sql As String = "SELECT * FROM tblSales ORDER BY SName"
Dim cmd As SqlCommand = New SqlCommand(sql, conn)

conn.Open()

Dim dr As SqlDataReader = cmd.ExecuteReader()

Me.ddSID.DataSource() = dr

While dr.Read
Dim newListItem As New ListItem()
newListItem.Value = dr("SID")
newListItem.Text = dr("SName")
Me.ddSID.Items.Add(newListItem)
End While

dr.Close()
conn.Close()

End Sub
 
K

Kiran

You are not actually binding the information when you do
it the way you are doing. Try this instead to bind the
info to the drowdown list box. This is in C#, It should be
easily be interpreted in VB.Net also.

sqlda = new SqlDataAdapter(cmd);
sqlds = new DataSet();
sqlda.Fill(sqlds, "tblSales");
sqldt = sqlds.Tables[0];
sqldt.DefaultView.Sort = "SName";
ddSID.DataSource = sqldt.DefaultView;
ddSID.DataTextField = "SName";
ddSID.DataValueField = "SID";
ddSID.DataBind();
ddSID.Items.Insert(0,"Select a SName...");
ddSID.Items[0].Value = "0";

sqlda = null;

Hope this helps
Kiran
 
S

Scott D

Thank you very much. I am new to this but everyone on this board has
been very helpful. Your explanation make a lot of sense.
 
S

Scott D

I just ran into another issue. It binds the SName fine however when I
attempt to submit the information to a database using the SID it seems
to be sending the string "SID" instead of the value of the SID field
which is an integer. Thank you in advance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top