Click here to Skip to main content
12,068,967 members (65,662 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# .NET
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
 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 3-Jul-13 19:55pm
usha C1.1K
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Just Update itlike that....Smile | :)



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();
}
  Permalink  
Comments
usha nanthini 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...:)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Hi,
you can try this:
comboBox1.SelectedValue.ToString();
  Permalink  
Comments
usha nanthini 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);
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Try this....Smile | :)

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();
  Permalink  
Comments
usha nanthini 4-Jul-13 1:35am
   
"Cannot apply indexing with [] to an expression of type 'object'" Am getting this eddror
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

Hi,
try this:
comboBox1.GetItemText(comboBox1.SelectedItem);
  Permalink  
Comments
usha nanthini 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web03 | 2.8.160208.1 | Last Updated 4 Jul 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100