Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
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
C#
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
Updated 24-Dec-19 0:17am
v2

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[^]
 
Share this answer
 
Comments
__TR__ 25-Aug-12 14:37pm    
+5
Wendelius 25-Aug-12 14:42pm    
Thanks :)
just swap between
C#
textBox1.Text
and
C#
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
 
Share this answer
 
Comments
sariqkhan 25-Aug-12 22:37pm    
thanks bro
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
 
Share this answer
 
v2
Comments
sariqkhan 25-Aug-12 22:37pm    
bro my columnis not updating.
but no error in code is there. so whats the problem is
Sandip.Nascar 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 26-Aug-12 4:47am    
thanks bro your code worked.
given a good knowledge
thanks once again
+5

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