Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
i just want to insert a data into database column using event handler like a button
i have inserted a text box's text but i dont know about combobox
can anybody help me out
i have my insertion code here
private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=c:\documents and settings\aquib\my documents\visual studio 2010\Projects\login\login\Database1.mdf;Integrated Security=True;User Instance=True";
            string q="insert into Table1 values('"+textBox1.Text+"','"+textBox2.Text+"','"+textBox3.Text+"')";
            con.Open();
            SqlCommand cmd = new SqlCommand(q, con);
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("suceed");
        }
i just want to add one combo box's data here to add in database column
thanks in advance
Posted 25-Aug-12 5:29am
sariqkhan1.4K
Edited 25-Aug-12 5:34am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

First of all, don't concatenate the values into your SQL statement directly. This will leave you open to SQL injections and may cause data type conversion problems and so on. Instead use the SqlParameter[^].
 
About the actual question, you can use the SelectedValue[^] of the combobox to retrieve the value behind the selection, taken that you have defined the ValueMember[^]
  Permalink  
Comments
__TR__ at 25-Aug-12 14:37pm
   
+5
Mika Wendelius at 25-Aug-12 14:42pm
   
Thanks :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Just like for Textbox you get the txt using textbox1.Text, for combobox, you need to get the selectedindex and then get the text
 
Try this..
 
 
private void button1_Click(object sender, EventArgs e)
        {
            string s = "";
             if(comboBox1.SelectedIndex>=0)
                 s = comboBox1.Items[comboBox1.SelectedIndex].ToString();
            SqlConnection con = new SqlConnection();
            con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=c:\documents and settings\aquib\my documents\visual studio 2010\Projects\login\login\Database1.mdf;Integrated Security=True;User Instance=True";
            string q="insert into Table1 values('"+textBox1.Text+"','"+textBox2.Text+"','"+s+"')";
            con.Open();
            SqlCommand cmd = new SqlCommand(q, con);
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("suceed");
        }
 
 
Here I replaced the last parameter with combox selected item.
 
Hope this helps.
cheers
  Permalink  
v2
Comments
sariqkhan at 25-Aug-12 22:37pm
   
bro my columnis not updating.
but no error in code is there. so whats the problem is
Sandip.Nascar at 26-Aug-12 2:07am
   
Can you debug these lines,
 
string s = "";
if(comboBox1.SelectedIndex>=0)
s = comboBox1.Items[comboBox1.SelectedIndex].ToString();
 
Whether s is asigned with any value.
 
cheers
sariqkhan at 26-Aug-12 4:47am
   
thanks bro your code worked.
given a good knowledge
thanks once again
+5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

just swap between
textBox1.Text
and
comboBox1.Items[0].ToString()
where items[0] mean the first value in ComboBox and you can replace it with the second value items[1] ...etc >> good luck
  Permalink  
Comments
sariqkhan at 25-Aug-12 22:37pm
   
thanks bro

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 DamithSL 245
1 Sergey Alexandrovich Kryukov 217
2 OriginalGriff 200
3 Peter Leow 160
4 Afzaal Ahmad Zeeshan 85
0 OriginalGriff 7,380
1 DamithSL 5,359
2 Sergey Alexandrovich Kryukov 4,944
3 Maciej Los 4,906
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web03 | 2.8.141223.1 | Last Updated 26 Aug 2012
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