Click here to Skip to main content
15,879,490 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi....

Im writin simple application to insert datas into database through grid. I want insert query in C# code. I wrote like this,

C#
SqlConnection mycon = new SqlConnection(strcon);
mycon.Open();
string strquery = "INSERT INT0 studentDetails values('" + txtName.Text.ToString() + "','" + txtQual.Text.ToString() + "','" + txtAge.Text.ToString() + "','" + txtMobile.Text + "')";
SqlCommand cmd = new SqlCommand(strquery, mycon);
cmd.ExecuteNonQuery();


But i cant insert insert data,guide me soon.
Posted
Updated 11-Apr-12 18:57pm
v2
Comments
uspatel 12-Apr-12 0:59am    
any error?
Priyaaammu 12-Apr-12 1:04am    
Ya..."Incorrect syntax near 'studentDetails'." near cmd.ExecuteNonQuery
Arul R Ece 12-Apr-12 1:08am    
what is the data type for age n mobile no
Priyaaammu 12-Apr-12 1:09am    
age-nvarchar,mobileno-varchar
Nilesh Patil Kolhapur 12-Apr-12 1:24am    
"String or binary data would be truncated.
The statement has been terminated."
the error says you are updating the column with larger size than what it can accomodate. Check for blank space in the column value
check it out

It should be INSERT INTO not INSERT INT0 (a zero on the end).

You should generally follow the error message and look before the area as the query processor will guide you to the point where it cannot comprehend.
 
Share this answer
 
Comments
Priyaaammu 12-Apr-12 1:15am    
Ok...i changed to "insert into..." and now getting error as
"String or binary data would be truncated.
The statement has been terminated."
Mehdi Gholam 12-Apr-12 1:31am    
Your data does not fit in the column size, check your sizes in the table definition and the size of the data you are trying to insert.
Priyaaammu 12-Apr-12 1:35am    
I gave datatype as name-nvarchar(20),qualification-nvarchar(20),age-nvarchar(20),mobileno-varchar(10)...And i insert datas as "Priya,B.E,27,044256892" in my application. what's my mistake!
Mehdi Gholam 12-Apr-12 1:41am    
Try removing characters from the input to find where the problem is.
Priyaaammu 12-Apr-12 1:48am    
Tried...still getting same incorrect syntax error
Hi,

try this
string strquery = "INSERT INTO studentDetails([name],qualification,age,mobileno) values('" + txtName.Text.ToString().Trim() + "','" + txtQual.Text.ToString().Trim() + "','" + txtAge.Text.ToString().Trim() + "','" + txtMobile.Text.Trim() + "')";
Best Luck
 
Share this answer
 
Comments
Nilesh Patil Kolhapur 12-Apr-12 1:54am    
have u try this
Priyaaammu 12-Apr-12 2:25am    
Ya..tried,but didnt
Priyaaammu 12-Apr-12 2:27am    
Thanks a lot...its working fine!
Priyaaammu 12-Apr-12 2:28am    
Having doubt...y did u gave name in []
Nilesh Patil Kolhapur 12-Apr-12 2:41am    
because name is keyword so accept this solution
Hi Priya....

U can try this like below

SQL
"insert into userInfo(name,designation,age,EmailId,MobileNo,address) values ('" + EmpName + "','" + EmpDesig + "'," + Empage + ",'" + EmpEmailId + "','" + EmpMobNo + "','" + EmpAddress + "')"
 
Share this answer
 
v2
Comments
Priyaaammu 12-Apr-12 1:08am    
Thanks for your comment...
I tried,but still getting same error as "Incorrect syntax near 'studentDetails" near cmd.ExecuteNonQuery
See
It i snot a good wy to write query like this because it may cause sql injection.
Try this
C#
string strquery = "INSERT INTO studentDetails(col1,col2,col3,col4) values(@col1,@col2,@col3,@col4)";
SqlCommand cmd=new SqlCommand(strquery,con);
cmd.Parameters.AddWithValue("col1",txtName.Text);
cmd.Parameters.AddWithValue("col2",txtQual.Text);
cmd.Parameters.AddWithValue("col3",txtAge.Text);
cmd.Parameters.AddWithValue("col4",txtMobile.Text);
cmd.ExecuteNonQuery();

SQL Injection Attacks and Some Tips on How to Prevent Them[^]
 
Share this answer
 
v3
Comments
Priyaaammu 12-Apr-12 1:25am    
Its not working...actually why we are using parameters here?
uspatel 12-Apr-12 1:31am    
to prevent from SQL Injection .
See updated answer.....
Member 10860948 19-Jun-14 6:01am    
Thank You It is also help me......Thank You So Much....My Application also working

Pragnesh Patel
Hello ,


you got
VB
String or binary data would be truncated.
error,

first you just verify in table for Fields datatype and its size ,because it may be happened that you have to insert data more than its size,so check in DB and increase size of fields,


Hope this will help you..
 
Share this answer
 
Comments
Priyaaammu 12-Apr-12 1:29am    
I gave datatype as name-nvarchar(20),qualification-nvarchar(20),age-nvarchar(20),mobileno-varchar(10)...what's my mistake
Arul R Ece 12-Apr-12 1:34am    
Hi priya
can u pls tell size of mobile no which u enter in coding part.

because in DB u create only 10.if u enter more than 10 digit

"String or binary data would be truncated."

Eror will come.pls check
Priyaaammu 12-Apr-12 1:37am    
Hi Arul...
I gave datatype as name-nvarchar(20),qualification-nvarchar(20),age-nvarchar(20),mobileno-varchar(10)...And i insert datas as "Priya,B.E,27,044256892" in my application. what's my mistake!
Arul R Ece 12-Apr-12 1:36am    
i am also try ,when i enter 11 digit mobile no.that nentioned error came.
so may be that place error.
Priyaaammu 12-Apr-12 1:39am    
But i gave 9 digits only,why thar error came?
check the query again and tell me that what error u get.
or your connection String is ok or not????
 
Share this answer
 
Comments
Priyaaammu 12-Apr-12 1:51am    
protected void btnSave_Click(object sender, EventArgs e)
{
string strcon = "Data Source=USER-PC\\SQLEXPRESS;Initial Catalog=Course;User ID=sa;Password=sql2008";
SqlConnection mycon = new SqlConnection(strcon);
mycon.Open();
string strquery = "INSERT INT0 studentDetails ('" + txtName.Text.ToString() + "','" + txtQual.Text.ToString() + "','" + txtAge.Text.ToString() + "','" + txtMobile.Text + "')";
SqlCommand cmd = new SqlCommand(strquery, mycon);
cmd.ExecuteNonQuery();
}

My database fields are name-nvarchar(20),qualification-nvarchar(20),age-nvarchar(20),mobileno-varchar(10)
Arul R Ece 12-Apr-12 2:09am    
hi priya ..did u got the answer
Priyaaammu 12-Apr-12 2:25am    
No...still struggling with same error!
Priyaaammu 12-Apr-12 2:29am    
Ya...got it!
Arul R Ece 12-Apr-12 2:33am    
oh good ..
what was the problem

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