Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void button1_Click(object sender, EventArgs e)
        {
            string connetionString = null;
            SqlConnection cnn ;
           connetionString = "Data Source=JDPANDEY-PC;Initial Catalog=Janardan;User ID = sa;Password= 123";
            cnn = new SqlConnection(connetionString);
            try
            {
                cnn.Open();     
                MessageBox.Show ("Connection Open ! ");
               
               string insertString = @" insert into emp values ('" + textBox1.Text.Trim () + "','" + textBox2.Text .Trim() + "','" + textBox3.Text.Trim() + "','"+ textBox4.Text.Trim() +"')";
                              
cnn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Can not open connection ! ");
            }
Posted
Updated 4-Dec-11 23:25pm
v2
Comments
Karthik Harve 5-Dec-11 5:25am    
how many columns does the emp table has..??? and all columns are of type varchar or char..??

Because you just assigned your INSERT query in a string variable and never used it.

-Eduard
 
Share this answer
 
You shoul also use these lines of code
C#
SqlCommand cmd = new SqlCommand(insertstring, cnn);
            
            cmd.CommandType = CommandType.Text;
            
            cmd.ExecuteNonQuery();
            cnn.Close();
 
Share this answer
 
v2
C#
string insertString =" insert into emp values ('" + textBox1.Text.Trim () + "','" + textBox2.Text .Trim() + "','"+ textBox3.Text.Trim() + "', '" + textBox4.Text.Trim() + "')"


C#
SqlCommand ScalarCommand = new SqlCommand();
            try
            {
                ScalarCommand.CommandText = insertString;
                ScalarCommand.Connection = Cnn;
ScalarCommand.ExecuteScalar();

use above code.
make sure all datatype of database is matches with insert value.
 
Share this answer
 
Comments
S.P.Tiwari 5-Dec-11 5:34am    
or call ScalarCommand.ExecuteNoNQuery()
You actually need to create a valid SqlCommand object and execute the command on database with a valid open connection associated with it.
 
Share this answer
 

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


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900