Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi friends,

How to write Insert query in next line.
I have lot of fields in my database.

This is my code...it shows error

sqlcommand cmd = new sqlcommand("INSERT into UserInfo VALUES('" + TxtUserName.Text + "','" + TxtPassword.Text + "','" + TxtFirstName.Text + "','" + TxtLastName.Text + "')",con);

Please help thanks
Posted
Comments
[no name] 14-May-14 7:52am    
We can't see your screen to know what error this is showing to you.
Vi(ky 14-May-14 8:17am    
update your question . Specify the error
Member239258 14-May-14 8:22am    
I want to write Sql Query in Multiple line.

Please help.

Thanks
[no name] 14-May-14 8:22am    
You need to specify the fields you want to insert your data into and you also need to use a parameterized query.
CHill60 14-May-14 9:28am    
Is the error "The type or namespace name 'sqlcommand' could not be found (are you missing a using directive or an assembly reference?)"?

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);
 
Share this answer
 
Comments
Maciej Los 14-May-14 13:04pm    
+5!
What about stored procedures?
CHill60 14-May-14 13:10pm    
Thank you! Yes a stored procedure would certainly be appropriate here, but the OP wasn't providing much extra information so I just went with the code they had in the hope that somewhere in there I managed to cover their problem :-)
Maciej Los 14-May-14 13:25pm    
Yeah, i understand it, but OP marked this question with ASP.NET tag. I strongly recommend to use SP instead paramterized queries.
CHill60 14-May-14 13:40pm    
I agree - hopefully they will read one of our comments and come back with "how do I do this in a Stored Procedure" ... or do the research.
Maciej Los 14-May-14 13:55pm    
;)
try following.

SQL
string cmdtxt="";
cmdtxt = cmdtxt + "INSERT into UserInfo VALUES('" + TxtUserName.Text ;
cmdtxt = cmdtxt + "','" + TxtPassword.Text + "','" + TxtFirstName.Text + "','" + TxtLastName.Text ;
sqlcommand cmd = new sqlcommand(cmdtxt,con);
 
Share this answer
 
Comments
[no name] 14-May-14 9:10am    
Why should he try that? String concatenation for an SQL query? And a query that is not going to work anyway?
CHill60 14-May-14 9:36am    
a. This won't compile. SqlCommand not sqlcommand for a start.
b. You should use parameterised queries. Never construct a sql query from user input by concatenating strings like this.
c. This won't work because you have missed a single-quote (parameterised queries remove that issue as well) and the final closing bracket
d. If you really are going to concatenate strings then consider using StringBuilder in the System.Text namespace

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