DropDown Databound

Discussion in 'ASP .Net Web Controls' started by Braden, Mar 5, 2004.

  1. Braden

    Braden Guest

    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
     
    Braden, Mar 5, 2004
    #1
    1. Advertising

  2. Braden

    Braden Guest

    RE: DropDown Databound (better exapmle)

    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()
     
    Braden, Mar 6, 2004
    #2
    1. Advertising

  3. Re: DropDown Databound (better exapmle)

    place this line of code after this line
    DropDownList1.DataBind();

    DropDownList1.Items.Add("test data", "test);

    if it shows up, and i suspect it will, your dataset is empty.

    --
    Regards,
    Alvin Bruney [ASP.NET MVP]
    Got tidbits? Get it here...
    http://tinyurl.com/3he3b
    "Braden" <> wrote in message
    news:...
    > 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 ticket
    > System.Data.SqlClient.SqlConnection sqlCon2 = new

    System.Data.SqlClient.SqlConnection("server=server;uid=user;pwd=password;dat
    abase=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();
    > }
    >
     
    Alvin Bruney [MVP], Mar 6, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Simon Harvey
    Replies:
    2
    Views:
    427
    Christopher Reed
    Jan 18, 2006
  2. Lair
    Replies:
    2
    Views:
    676
    sloan
    Jun 21, 2006
  3. Steve
    Replies:
    3
    Views:
    480
    Karl Seguin [MVP]
    Sep 14, 2006
  4. rodchar
    Replies:
    0
    Views:
    410
    rodchar
    Nov 26, 2007
  5. Manu
    Replies:
    0
    Views:
    189
Loading...

Share This Page