Hi,
I have a table login in sql it contains lots of fields(for example fname,lname,Password,address,Username etc). I need to insert some data only to the table(Username and Password). I use the code in C#
SqlCommand cmd = new SqlCommand("insert into login values(@UserName)", con);
cmd.Parameters.AddWithValue("@UserName", txtUserName.Text);
cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
on clicking submit button it shows an error message:
"Column name or number of supplied values does not match table definition"
Please correct the problem.
Loading


Munesh SharmaRank #174 · 13.9K reputationPosted Mar 25, 2014, 4:36 AM EDT
SqlCommand cmd = new SqlCommand("INSERT INTO tablename (UserName,Password) VALUES(@UserName, @Password)", con);
cmd.Parameters.AddWithValue("@UserName", txtUserName.Text);
cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
these username and password should be same as column name which you pass in table.
Brijendra GautamRank #1,900 · 778 reputationPosted Mar 25, 2014, 4:40 AM EDT
Below is the correct command:
SqlCommand cmd = new SqlCommand("Insert Into login(UserName,Password) values(@UserName,@Password)", con);
cmd.Parameters.AddWithValue("@UserName", txtUserName.Text);
cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
Try this.
Abhay ShankerRank #375 · 6.7K reputationPosted Mar 25, 2014, 5:09 AM EDT
SqlCommand cmd = new SqlCommand("insert into login values(@UserName,@Password)", con);