Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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#
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.
Posted
Updated 25-Mar-14 2:40am
v2
Comments
Raul Iloc 25-Mar-14 14:01pm    
Did you try my solution (Solution1)?
My Doubt 29-Mar-14 0:24am    
yea.. but the thing is that we must insert some values to not null fields.
Member 10704263 28-Mar-14 9:34am    
plz i want this in vb code :)
My Doubt 29-Mar-14 0:24am    
Hey friend I don't know vb. May be this link will help you
http://social.msdn.microsoft.com/Forums/vstudio/en-US/d11ed406-d9bf-4363-84b6-99f4e9ee4631/how-to-insert-data-into-sql-table-using-vbnet?forum=vbgeneral

Your SQL used is incorrect, and should be like the next one:
C#
SqlCommand cmd = new SqlCommand("insert into login (Username, Password) values(@UserName, @Password)", con);
 
Share this answer
 
v2
Comments
Christian Amado 25-Mar-14 10:58am    
This is the first solution posted and is correct. Solution 2 and 3 are correct too.
Raul Iloc 25-Mar-14 14:00pm    
Thank you for your feeback!
Try this..

IF You want to insert specific columns USE Columns name


SQL
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);
 
Share this answer
 
v3
IF You want to insert specific columns data only then you have to write it also in query like that...

C#
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);
 
Share this answer
 

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