The first error you will get is on
sqlcommand
and that error will be
Quote:
The type or namespace name 'sqlcommand' could not be found (are you missing a using directive or an assembly reference?)"
That's because it should be
SqlCommand
- C# is a case-sensitive language. There's a discussion on why that might be
on this link[
^]
Next you will (eventually) get problems because you are concatenating textbox contents to build your sql string. You should use Parameterized Queries - this link gives
some reasons why[
^]
Next possible issue is prompted by your comment
Member239258 said:
I have lot of fields in my database.
If your table
UserInfo
has more columns than the four values you are trying to insert, then you must let SQL Server know which columns you are providing (
reference[
^]). The same is true when you provide values that are not in the same order that the columns were defined. E.g. in your case (I have guessed at the column names)
INSERT into UserInfo (UserName, Password, FirstName, LastName) VALUES(...
This code should work for you given the information you've provided
string sql = "INSERT into UserInfo (UserName, Password, FirstName, LastName) VALUES(@UserName,@Password,@FirstName,@LastName)";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddWithValue("@UserName", TxtUserName.Text);
cmd.Parameters.AddWithValue("@Password", TxtPassword.Text);
cmd.Parameters.AddWithValue("@FirstName", TxtFirstName.Text);
cmd.Parameters.AddWithValue("@LastName", TxtLastName.Text);