Click here to Skip to main content
13,199,318 members (68,396 online)
Rate this:
 
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 4:29am
sariqkhan1.4K
Updated 25-Aug-12 4:34am
Wendelius363.1K
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__ 25-Aug-12 14:37pm
   
+5
Mika Wendelius 25-Aug-12 14:42pm
   
Thanks :)
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 25-Aug-12 22:37pm
   
thanks bro
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 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web01 | 2.8.171020.1 | Last Updated 26 Aug 2012
Copyright © CodeProject, 1999-2017
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