Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
sorry i am a newbie and my friend found out some coding error.
i am not able to understand what the problem is.

SqlConnection conn = new SqlConnection();
            conn.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\project\Projects\labelnameinsecondformtry\labelnameinsecondformtry\Database1.mdf;Integrated Security=True;User Instance=True";
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            conn.Open();
            try
            {
                cmd.CommandText = "Insert into Table2 values (username)";
                cmd.Parameters.Add("@username", SqlDbType.VarChar).Value =textBox1.Text;
                cmd.ExecuteNonQuery();
            }
            finally
            {
                conn.Close();
                MessageBox.Show("hello welcome");
            }


the error is comming new executenonquery and saying that
Invalid column name 'username' but username column us there in my database whats the real problem in this coding
Posted

1 solution

Your commandText is incorrect. The proper syntax for an insert statement would be:
SQL
INSERT INTO Table2 (username) VALUES (@username)

Here is a resource on the INSERT statement: TSQL INSERT[^]
 
Share this answer
 
v3
Comments
shaikh-adil 13-Nov-12 10:42am    
oh sorry it was a silly mistake
thanks for helping
fjdiewornncalwe 13-Nov-12 13:05pm    
You are welcome.
RaisKazi 13-Nov-12 11:08am    
A 5!
fjdiewornncalwe 13-Nov-12 13:05pm    
Thanks, Rais
sariqkhan 15-Nov-12 7:30am    
+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