Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
cmd = new SqlCommand("select intrfacname from transfer where ipad='" + comboBox2.SelectedItem.ToString() + "'", cn.con);
dr1 = cmd.ExecuteReader();
while (dr1.Read())
{
    textBox2.Text = dr1[0].ToString();
}
dr1.Dispose();
cmd.Dispose();


Am Selecting the combo box value and read into an text box but value of that selected index is not reading in a string Before dis code i bind the values in combo box the code for that is here but it works
C#
 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
  cmd = new SqlCommand("select ipad from transfer where ntwrk='" + comboBox1.SelectedItem.ToString() + "'", cn.con);
            

            dr = cmd.ExecuteReader();
            DataTable dt = new DataTable();

            dt.Columns.Add("ipad", typeof(string));
           // dt.Columns.Add("contactname", typeof(string));
            dt.Load(dr);

            comboBox2.ValueMember = "ipad";
            comboBox2.DisplayMember = "ipad";
            comboBox2.DataSource = dt;
            cmd.Dispose();
            dr.Dispose();
}


But the above code for reading a value does not work help me pls
Posted

Just Update itlike that....:)



C#
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
  cmd = new SqlCommand("select ipad from transfer where ntwrk='" + comboBox1.SelectedItem.ToString() + "'", cn.con);
            
 
            dr = cmd.ExecuteReader();
            DataTable dt = new DataTable();
 
            dt.Columns.Add("ipad", typeof(string));
           // dt.Columns.Add("contactname", typeof(string));
            dt.Load(dr);
 
            comboBox2.DataSource = dt;
            comboBox2.ValueMember = "ipad";
            comboBox2.DisplayMember = "ipad";
           
            cmd.Dispose();
            dr.Dispose();
}
 
Share this answer
 
Comments
usha C 4-Jul-13 1:09am    
thnks fr ur code...but still it not works the code which i given n 1st block is not working.unable to read the value in textbox
Nirav Prabtani 4-Jul-13 1:33am    
See my another answer below...Solution 3
Nirav Prabtani 4-Jul-13 1:34am    
Up vote my solution you got Your solution...:)
Hi,
you can try this:
comboBox1.SelectedValue.ToString();
 
Share this answer
 
Comments
usha C 4-Jul-13 1:30am    
But i need only cmbobox 2 value and not the 1
praks_1 4-Jul-13 1:37am    
comboBox1.GetItemText(comboBox1.SelectedItem);
Try this....:)

C#
cmd = new SqlCommand("select intrfacname from transfer where ipad='" + comboBox2.SelectedItem.ToString() + "'", cn.con);
         dr1 = cmd.ExecuteReader();
         while (dr1.Read())
         {
             textBox2.Text = dr1[0]["intrfacname"].ToString();
         }
         dr1.Dispose();
         cmd.Dispose();
 
Share this answer
 
Comments
usha C 4-Jul-13 1:35am    
"Cannot apply indexing with [] to an expression of type 'object'" Am getting this eddror
Hi,
try this:
comboBox1.GetItemText(comboBox1.SelectedItem);
 
Share this answer
 
Comments
usha C 4-Jul-13 1:40am    
Thanks lot it Works

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