Click here to Skip to main content
12,395,252 members (61,292 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
Hi....

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

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 11-Apr-12 18:56pm
Updated 11-Apr-12 18:57pm
uspatel30.3K
v2
Comments
Uma Shankar Patel 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
Priyaaammu 12-Apr-12 1:37am
   
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!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
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
Nilesh Patil Kolhapur 12-Apr-12 1:16am
   
very nice sir great great great
Uma Shankar Patel 12-Apr-12 1:27am
   
5+
good catch.......
Mehdi Gholam 12-Apr-12 1:31am
   
:)
Member 10918102 2-Jul-14 0:35am
   
cool
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 7

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
  Permalink  
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
Priyaaammu 12-Apr-12 2:43am
   
Im not clear...why do u mean that its a keyword
Nilesh Patil Kolhapur 12-Apr-12 2:43am
   
accept the solution
Priyaaammu 12-Apr-12 2:46am
   
Ok...i accepted,but clear me y u mean that as keyword
Nilesh Patil Kolhapur 12-Apr-12 2:46am
   
name have special meaning in sql server like private, int u cant use this keyword as column name if u want to use then need remove its meaning using [] ok hope its clear
Priyaaammu 12-Apr-12 2:49am
   
Ok...thanks,got it clearly
Nilesh Patil Kolhapur 12-Apr-12 2:50am
   
accept it now
Nilesh Patil Kolhapur 12-Apr-12 2:49am
   
just type name in ur sql n check it out....
Priyaaammu 12-Apr-12 3:37am
   
Again im back with another doubt. Now my work is to display success message after inserting record and have to declare my connection in another function and to call that in Save event. I tried like this...
protected void btnSave_Click(object sender, EventArgs e)
{
Insert();
}
private bool Insert()
{
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 into studentDetails([name],qualification,age,mobileno) values('" + txtName.Text.ToString().Trim() + "','" + txtQual.Text.ToString().Trim() + "','" + txtAge.Text.ToString().Trim() + "','" + txtMobile.Text.Trim() + "')";
SqlCommand cmd = new SqlCommand(strquery, mycon);
if (cmd.ExecuteNonQuery() == true)
{
lblSuccess.Text = "Saved Successfully";
}
else
{
lblFailed.Text = "Failed";
}
}
but dnt know how to move on... also im getting red error line in if condition.
Nilesh Patil Kolhapur 12-Apr-12 5:45am
   
wait m busy in my work i ll solve it
Nilesh Patil Kolhapur 12-Apr-12 5:49am
   
here is ur solution sorry for delay
protected void btnSave_Click(object sender, EventArgs e)
{
if (Insert())
{
lblSuccess.Text = "Saved Successfully";
}
else
{
lblFailed.Text = "Failed";
}
}

private bool Insert()
{
try
{
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 into studentDetails([name],qualification,age,mobileno) values('" + txtName.Text.ToString().Trim() + "','" + txtQual.Text.ToString().Trim() + "','" + txtAge.Text.ToString().Trim() + "','" + txtMobile.Text.Trim() + "')";
SqlCommand cmd = new SqlCommand(strquery, mycon);
cmd.ExecuteNonQuery();
return true;
}
catch (Exception Ex)
{ return false; }
}
lalit badule 29-Jun-14 4:31am
   
succesfully run...thanks....
Member 7757310 22-Dec-12 6:18am
   
thanks nilesh sir
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Hi Priya....

U can try this like below

"insert into userInfo(name,designation,age,EmailId,MobileNo,address) values ('" + EmpName + "','" + EmpDesig + "'," + Empage + ",'" + EmpEmailId + "','" + EmpMobNo + "','" + EmpAddress + "')"
  Permalink  
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

See
It i snot a good wy to write query like this because it may cause sql injection.
Try this
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[^]
  Permalink  
v3
Comments
Priyaaammu 12-Apr-12 1:25am
   
Its not working...actually why we are using parameters here?
Uma Shankar Patel 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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

Hello ,


you got
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..
  Permalink  
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?
Madhuri Patil 12-Apr-12 1:41am
   
you just change nvarchar(11) for mobile.
Arul R Ece 12-Apr-12 1:42am
   
s i know pa ..i just check
Arul R Ece 12-Apr-12 1:41am
   
when i try this
('arul','be','25','97893832801')
i got error
Priyaaammu 12-Apr-12 1:45am
   
Guide me soon...
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 6

check the query again and tell me that what error u get.
or your connection String is ok or not????
  Permalink  
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
Priyaaammu 12-Apr-12 2:39am
   
I tried like 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() + "')";

But having doubt in this,why name given as [name]
Arul R Ece 12-Apr-12 2:53am
   
sari pa
Priyaaammu 12-Apr-12 3:00am
   
Ok...thanks for all ur comments!
Priyaaammu 12-Apr-12 3:36am
   
Hi Arul...
Again im back with another doubt. Now my work is to display success message after inserting record and have to declare my connection in another function and to call that in Save event. I tried like this...
protected void btnSave_Click(object sender, EventArgs e)
{
Insert();
}
private bool Insert()
{
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 into studentDetails([name],qualification,age,mobileno) values('" + txtName.Text.ToString().Trim() + "','" + txtQual.Text.ToString().Trim() + "','" + txtAge.Text.ToString().Trim() + "','" + txtMobile.Text.Trim() + "')";
SqlCommand cmd = new SqlCommand(strquery, mycon);
if (cmd.ExecuteNonQuery() == true)
{
lblSuccess.Text = "Saved Successfully";
}
else
{
lblFailed.Text = "Failed";
}
}
but dnt know how to move on... also im getting red error line in if condition
Arul R Ece 12-Apr-12 4:18am
   
protected void btnSave_Click(object sender, EventArgs e) { Insert(); }
private bool Insert() {
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 into studentDetails([name],qualification,age,mobileno) values('" + txtName.Text.ToString().Trim() + "','" + txtQual.Text.ToString().Trim() + "','" + txtAge.Text.ToString().Trim() + "','" + txtMobile.Text.Trim() + "')";

SqlCommand cmd = new SqlCommand(strquery, mycon);

string message="success";

if (message="success")
{
cconsole.writeline("Record added successfully")
}

else
{
cconsole.writeline(" Please check")
}

try like this way.may be it will help for u

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 15 Feb 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100