DropDown Databound

B

Braden

I am having some trouble working through the logic of binding a dropdown list to a database here is my code. Please note that this works fine if I use a dropdown list that I add the values to manually

I have tried to capture the selected item in the button command, I have tried it with and without binding the data after the post, but nothing seems to return the selected item. I am user I am doing something wrong with the post but what. Once again if I create the options manually this works fine

Thanks in advance for any help

//Example --
private void Page_Load(object sender, System.EventArgs e

TextBox1.Text = ListBox1.SelectedValue.ToString()

if(!Page.IsPostBack

GetProducts(SelectedProduct)



private void GetProducts(int ProductSelected

string getProducts = "select ProductNumber, iResolutionGroup from sometable"

System.Data.SqlClient.SqlConnection sqlCon2 = new System.Data.SqlClient.SqlConnection("server=server;uid=sa;pwd=password;database=db")
System.Data.SqlClient.SqlCommand sqlComm = new System.Data.SqlClient.SqlCommand(getProducts,sqlCon2)
System.Data.SqlClient.SqlDataAdapter sqlDA = new System.Data.SqlClient.SqlDataAdapter()

sqlDA.SelectCommand = sqlComm

DataSet Products_ds = new DataSet()

sqlDA.Fill(Products_ds)
sqlCon2.Close()

//Products_dd is the DropDown list
Products_dd.DataSource = Products_ds
Products_dd.DataTextField = "ProductNumber"
Products_dd.DataValueField = "iResolutionGroup"
Products_dd.DataBind()


private void Button1_Click(object sender, System.EventArgs e

//Do something, but more importantly create the post
 
B

Braden

I think this might be a better example. I did almos exactly what the kb article http://support.microsoft.com/default.aspx?scid=kb;EN-US;312489 stated. Why won't this work


private void Page_Load(object sender, System.EventArgs e

if(!IsPostBack

string getProducts = "espigIncidentProducts 1, NULL, 1"

//Connect to SQL and the information for the ticke
System.Data.SqlClient.SqlConnection sqlCon2 = new System.Data.SqlClient.SqlConnection("server=server;uid=user;pwd=password;database=database1")
System.Data.SqlClient.SqlCommand sqlComm = new System.Data.SqlClient.SqlCommand(getProducts,sqlCon2)

sqlCon2.Open()

System.Data.SqlClient.SqlDataReader sqlDA = sqlComm.ExecuteReader()

DropDownList1.DataSource = sqlDA
DropDownList1.DataTextField = "chProductNumber"
DropDownList1.DataValueField = "iResolutionGroup"
DropDownList1.DataBind()
sqlCon2.Close()
sqlDA.Close()



private void LinkButton1_Click(object sender, System.EventArgs e

TextBox1.Text = DropDownList1.SelectedValue.ToString()
 
A

Alvin Bruney [MVP]

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top