Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 13-Nov-12 3:50am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Your commandText is incorrect. The proper syntax for an insert statement would be:
INSERT INTO Table2 (username) VALUES (@username)
Here is a resource on the INSERT statement: TSQL INSERT[^]
  Permalink  
v3
Comments
shaikh-adil at 13-Nov-12 10:42am
   
oh sorry it was a silly mistake
thanks for helping
Marcus Kramer at 13-Nov-12 13:05pm
   
You are welcome.
RaisKazi at 13-Nov-12 11:08am
   
A 5!
Marcus Kramer at 13-Nov-12 13:05pm
   
Thanks, Rais
sariqkhan at 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)

  Print Answers RSS
0 Nirav Prabtani 298
1 CPallini 209
2 Mika Wendelius 185
3 OriginalGriff 182
4 Sergey Alexandrovich Kryukov 176
0 Nirav Prabtani 398
1 OriginalGriff 292
2 Sergey Alexandrovich Kryukov 251
3 Mika Wendelius 185
4 Abhinav S 180


Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 13 Nov 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