Click here to Skip to main content
15,888,048 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
C#
protected void Submit_Click(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS; database= Registration; trusted_connection=yes");
    conn.Open();
    
    SqlCommand com = new SqlCommand("insert into register(Username, password, email id, first name) values(" + TextBoxUS.Text + "," + TextBoxPass.Text + "," + TextBoxEA.Text + "," + TextBoxFN.Text + ") ", conn);

    com.ExecuteNonQuery();
}


I am sorry for before post
The error its showing as "SqlException was unhandled "
The error is in this line= com.ExecuteNonQuery(); it showing "Incorrect syntax near 'id'."
Posted
Updated 23-May-12 10:40am
v4
Comments
Ed Nutting 23-May-12 13:55pm    
Edit: Added code formatting

So what is the error message? What is going wrong? We can't really help you if all you say is "this isn't working" - we need more detail than that. Please use the Improve Question link above to improve this so we can help.

Thanks,
Ed
selflearning 23-May-12 14:18pm    
Thanks for your valuable reply please can you check now as I updated the question
Maciej Los 23-May-12 13:57pm    
What's the problem, error...?
Sandeep Mewara 23-May-12 13:58pm    
This is not a well framed question! We cannot work out what you are trying to do/ask from the post. Please elaborate and be specific.
Use the "Improve question" link to edit your question and provide better information.
selflearning 23-May-12 14:19pm    
Thanks for your valuable reply I had improved the question and updated it please check it and give some solution...

Column names cannot have spaces in them if they do you must put brackets around them.

For example : email id -> [email id]
 
Share this answer
 
Comments
Maciej Los 23-May-12 14:03pm    
Good answer, my 5!
Mehdi Gholam 23-May-12 14:07pm    
Thanks losmac!
selflearning 23-May-12 14:20pm    
Thanks
Wendelius 23-May-12 14:25pm    
Exactly :)
Mehdi Gholam 23-May-12 14:27pm    
Thanks Mika!
And another thing. Never concatenate values directly to your SQL statements. This leaves you vulnerable to SQL injections, data type conversion problems and so on. Instead, always use SqlParameter[^]

So your query could look something like
C#
...
SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS; database= Registration; trusted_connection=yes");
conn.Open();
    
SqlCommand com = new SqlCommand("insert into register(Username, password, [email id], [first name]) values(@username, @password, @emailid, @firstname) ", conn);

com.Parameters.AddWithValue("@username", TextBoxUS.Text);
com.Parameters.AddWithValue("@password", TextBoxPass.Text);
com.Parameters.AddWithValue("@emailid", TextBoxEA.Text);
com.Parameters.AddWithValue("@firstname", TextBoxFN.Text);

com.ExecuteNonQuery();
...
 
Share this answer
 
v2
Comments
selflearning 23-May-12 14:27pm    
Thanks for your reply but it showing the same error
The error its showing as "SqlException was unhandled "
The error is in this line com.ExecuteNonQuery(); it showing "Incorrect syntax near 'id'."
Wendelius 23-May-12 14:35pm    
Sorry, my bad :) Forgot to add the brackets. Answer is updated
Mehdi Gholam 23-May-12 14:27pm    
5'ed, (you forgot the brackets :))
Wendelius 23-May-12 14:36pm    
Yeah I noticed, but then again you already mentioned that :)
Ed Nutting 23-May-12 14:28pm    
A very good point, my 5+ :)
don`t use space between column name. Use column name in bracket Like [email id] instead of email id.

if space in column name then SQL read as different column name. email and another one is id.
 
Share this answer
 
i think you can try like this

C#
SqlCommand com = new SqlCommand("insert into register(Username, password, email_id, first_name) values(" + TextBoxUS.Text + "," + TextBoxPass.Text + "," + TextBoxEA.Text + "," + TextBoxFN.Text + ") ", conn);


Because in the insert statement it doesn't accept spaces for the column name (write in the insert statement what the column names are present in the table)
 
Share this answer
 
v2
Comments
selflearning 23-May-12 15:03pm    
protected void Submit_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS; database= Registration; trusted_connection=yes");
conn.Open();
SqlCommand com = new SqlCommand("insert into register(Username, password, [email id], [first name]) values(@Username, @password, @emailid, @firstname) ", conn);
com.Parameters.AddWithValue("@Username", TextBoxUS.Text);
com.Parameters.AddWithValue("@password", TextBoxPass.Text);
com.Parameters.AddWithValue("@emailid", TextBoxEA.Text);
com.Parameters.AddWithValue("@firstname", TextBoxFN.Text);
com.ExecuteNonQuery();


}
I got error like =Cannot insert the value NULL into column 'Id', table 'Registration.dbo.Register'; column does not allow nulls. INSERT fails.
The statement has been terminated.
anilkumar.6714 23-May-12 21:42pm    
I think when you are creating the table you declared 'Id' datatype as "int", int doesn't take null values, so your getting error like that.
we can not give space in database field name.
change the name like email_id or emailid.....
 
Share this answer
 
Try to ignore all the below :

1) There is an identity field which can not be null inserted either you can do identity=true or send from front end.
2) It not a good habbit to use space in fields or use keywords that are already used in sql server.
3) use procedure to insert data into database rather using inline queries for ignoring sql injection.


Thanks,

rate if you like
 
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