Click here to Skip to main content
Rate this: bad
good
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 at 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 at 4-Jul-13 1:33am
   
See my another answer below...Solution 3
Nirav Prabtani at 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 at 4-Jul-13 1:30am
   
But i need only cmbobox 2 value and not the 1
praks_1 at 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 at 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 at 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
0 OriginalGriff 406
1 /\jmot 180
2 Suraj Sahoo | Coding Passion 170
3 Afzaal Ahmad Zeeshan 159
4 BillWoodruff 154
0 OriginalGriff 8,344
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,634
3 Maciej Los 5,024
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 4 Jul 2013
Copyright © CodeProject, 1999-2014
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