locked
How to bind an SQL query to a ComboBox which whenever you select any Item from it, the sql query will be executed independently????

    Question

  • Hi,

    I have created an sql query which reads data from an sql database, and it'll output the data as link labels. See the following:


                private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {

                string combobox= comboBox1.Text;
                SqlConnection Connection = new SqlConnection(@"Data Source="";Initial Catalog="";");
                SqlCommand SqlCmd = new Command();
                SqlCmd.CommandType = CommandType.Text;
                SqlCmd.Parameters.Add("@name", SqlDbType.VarChar).Value = combobox;
                SqlCmd.CommandText = " SELECT * from TABEL where ColumnName = @name";
                SqlCmd.Connection = Connection;
                SqlDataReader rdr = null;
                Connection.Open();

                rdr = SqlCmd.ExecuteReader();
                int top = 100;
                while (rdr.Read())
                {
                    lnkLabl = new LinkLabel();
                    lnkLabl.Text = rdr["ColumnName"].ToString();
                    lnkLabl.Location = new Point(20, top);
                    this.Controls.Add(lnkLabl);
                    top += 50;
                }
                rdr.Close();
                Connection.Close();

           }


    By doing that, when I select a value from comboBox1, the query works, and I get the data related to that selected item in the comboBox as link labels one after the other one. Now, when I do another selection from the comboBox, the new result will execute above the old ones and the new ones will not show up. The new link labels will be above the old link labels from the previous comboBox selection.

    How can I fix this????

    How can I get the result of the query every time I select an item from the ComboBox without being affected with the result of the previous selection????   

    Wednesday, March 23, 2011 8:04 PM

Answers

  • Hi

    I misunderstood the question since the way you have explained in the post.

    Here the code for what you have to do . . . 

    // create a list like this . . . 
    private List<LinkLabel> list = new List<LinkLabel> ();
     
    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
      {
    
       string combobox= comboBox1.Text;
       SqlConnection Connection = new SqlConnection(@"Data Source="";Initial Catalog="";");
       SqlCommand SqlCmd = new Command();
       SqlCmd.CommandType = CommandType.Text;
       SqlCmd.Parameters.Add("@name", SqlDbType.VarChar).Value = combobox;
       SqlCmd.CommandText = " SELECT * from TABEL where ColumnName = @name";
       SqlCmd.Connection = Connection;
       SqlDataReader rdr = null;
       Connection.Open();
    
       rdr = SqlCmd.ExecuteReader();
       int top = 100;
       int i = 0;
       
       // here we remove the old controls, this has to be done in order to put the new controls in the same location
       if (list.Count > 0)
       {
        foreach (LinkLabel l in list)
         this.Controls.Remove(l);
    
        list.Clear();
       }
    
       while (rdr.Read())
       {
        list.Add(new LinkLabel());
        list[i].Text = rdr["ColumnName"].ToString();
        list[i].Location = new Point(20, top);
        this.Controls.Add(lnkLabl);
        top += 50;
        i++;
       }
       
       rdr.Close();
       Connection.Close();
    
      }
    

     

     

    This will help you. . . .


    My blog : http://thuruinhttp.wordpress.com
    • Proposed as answer by thuruv Thursday, March 24, 2011 5:06 PM
    • Marked as answer by Sarah Putrus Thursday, March 24, 2011 6:10 PM
    Thursday, March 24, 2011 5:06 PM

All replies

  • Hi,

    Here the ComboBox works fine, and retreives the new results, but your problem is that your code places the new controls on top of the old controls.

    Change the parameters to the location of the link buttons, then every thing will be fine.


    My blog : http://thuruinhttp.wordpress.com
    Thursday, March 24, 2011 7:07 AM
  •  How can I do that? I need the new controls to be shown on the same place where the old ones are, and I need the old ones to be gone when a new selection is made, so this new result will show up.

    Can u please explain what you suggested in codes?

    Thursday, March 24, 2011 11:34 AM
  • Hi

    I misunderstood the question since the way you have explained in the post.

    Here the code for what you have to do . . . 

    // create a list like this . . . 
    private List<LinkLabel> list = new List<LinkLabel> ();
     
    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
      {
    
       string combobox= comboBox1.Text;
       SqlConnection Connection = new SqlConnection(@"Data Source="";Initial Catalog="";");
       SqlCommand SqlCmd = new Command();
       SqlCmd.CommandType = CommandType.Text;
       SqlCmd.Parameters.Add("@name", SqlDbType.VarChar).Value = combobox;
       SqlCmd.CommandText = " SELECT * from TABEL where ColumnName = @name";
       SqlCmd.Connection = Connection;
       SqlDataReader rdr = null;
       Connection.Open();
    
       rdr = SqlCmd.ExecuteReader();
       int top = 100;
       int i = 0;
       
       // here we remove the old controls, this has to be done in order to put the new controls in the same location
       if (list.Count > 0)
       {
        foreach (LinkLabel l in list)
         this.Controls.Remove(l);
    
        list.Clear();
       }
    
       while (rdr.Read())
       {
        list.Add(new LinkLabel());
        list[i].Text = rdr["ColumnName"].ToString();
        list[i].Location = new Point(20, top);
        this.Controls.Add(lnkLabl);
        top += 50;
        i++;
       }
       
       rdr.Close();
       Connection.Close();
    
      }
    

     

     

    This will help you. . . .


    My blog : http://thuruinhttp.wordpress.com
    • Proposed as answer by thuruv Thursday, March 24, 2011 5:06 PM
    • Marked as answer by Sarah Putrus Thursday, March 24, 2011 6:10 PM
    Thursday, March 24, 2011 5:06 PM
  • Thank you sooooo much, you really helped me
    Thursday, March 24, 2011 7:23 PM
  •  

    You are mostly welcomed. :)


    My blog : http://thuruinhttp.wordpress.com
    Friday, March 25, 2011 4:34 AM