Click here to Skip to main content
12,397,380 members (63,275 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET
Incorrect syntax near 'Quantity'.

DateTime date = DateTime.Parse(DateTime.Now.ToShortDateString());
string dt = date.ToString("MM/dd/yyyy");
string month = date.ToString("MMMM");
float mm=float.Parse(TextBox2.Text);
string df = "select Quantity from Item where Item='" + TextBox1.Text + "'";
SqlCommand dd = new SqlCommand(df,DbConnection.mCon);
SqlDataAdapter dg = new SqlDataAdapter(dd);
DataTable sd = new DataTable();
dg.Fill(sd);
if (sd.Rows.Count > 0)
{
  cc = float.Parse(sd.Rows[0][0].ToString());
}
float nb = cc + mm;
string s11 = "update Item set Item='" + TextBox1.Text + "' Quantity='" + nb + "' Price='" + TextBox3.Text + "' Date='" + dt + "' Month='" + month + "' where Item='" + TextBox1.Text + "' ";
 SqlCommand cmdh = new SqlCommand(s11, DbConnection.mCon);
 cmdh.ExecuteNonQuery();

Table name Item

fieldname Quantity datatype float
Posted 16-Dec-12 23:57pm
Updated 16-Dec-12 23:58pm
__TR__29K
v2
Comments
__TR__ 17-Dec-12 6:00am
   
Try executing the update statement generated by your code in SQL server. You might get an idea on what is wrong.
ravuravu 17-Dec-12 6:00am
   
error shows in the last line cmdh.executenonquery statement
ravuravu 17-Dec-12 6:03am
   
i don,t know what is the real problem
jayantbramhankar 17-Dec-12 6:05am
   
add comma(,) after each column in update query.
update Item set Item='" + TextBox1.Text + "', Quantity='" + nb + "' , ans so
ravuravu 17-Dec-12 6:09am
   
yes u are correct
ravuravu 17-Dec-12 6:10am
   
thanks jayantbramhankar and __tr__ for helping me
Richard MacCutchan 17-Dec-12 6:11am
   
You should not use string concatenation to create SQL commands; you are leaving your system open to SQL injection by spammers and viruses. Use proper parameterized queries.
ravuravu 17-Dec-12 6:34am
   
how to use parameterised queries pls give me an example mr Richard MacCutchan.
i waiting for your example
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

in Query you forgot to write comma separator in update fields
see, underlined portion in below code...
string s11 = "update Item set Item='" + TextBox1.Text + "' , Quantity='" + nb + "' ,  Price='" + TextBox3.Text + "' ,  Date='" + dt + "' ,  Month='" + month + "' where Item='" + TextBox1.Text + "' ";
 SqlCommand cmdh = new SqlCommand(s11, DbConnection.mCon);
 cmdh.ExecuteNonQuery();
Happy Coding!
Smile | :)
  Permalink  
Comments
ravuravu 17-Dec-12 6:30am
   
thanks aarti mam
Aarti Meswania 17-Dec-12 6:51am
   
welcome! :)
Glad to help you! :)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

string s11 = "update Item set Item='" + TextBox1.Text + "', Quantity='" + nb + "', Price='" + TextBox3.Text + "', Date='" + dt + "' Month='" + month + "' where Item='" + TextBox1.Text + "' ";
 SqlCommand cmdh = new SqlCommand(s11, DbConnection.mCon);
 cmdh.ExecuteNonQuery();
  Permalink  
Comments
ravuravu 17-Dec-12 6:31am
   
thanks sir
Krunal Rohit 17-Dec-12 6:34am
   
If you're satisfied with the given answer, Accept the Solution :)
-Krunal R.
ravuravu 17-Dec-12 6:47am
   
ya i accepted and vote it
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

change your statement to:

string s11 = "update Item set Item='" + TextBox1.Text + "', Quantity=" + nb + ", Price='" + TextBox3.Text + "', Date='" + dt + "', Month='" + month + "' where Item='" + TextBox1.Text + "' ";

note that a comma has been inserted between each column, apart from that I think that the quantity column is of type float so you should not be enclosing the value between single quotes, which has been removed in above query. Also check on what is the type of Price columns, since if it is also numeric/float/int type you will need to remove the single quotes from the value like this too:

string s11 = "update Item set Item='" + TextBox1.Text + "', Quantity=" + nb + ", Price=" + TextBox3.Text + ", Date='" + dt + "', Month='" + month + "' where Item='" + TextBox1.Text + "' ";

Regards
Pawan
  Permalink  
Comments
ravuravu 17-Dec-12 6:32am
   
thanks rai
dimpledevani 17-Dec-12 7:44am
   
My 5+
Rai Pawan 17-Dec-12 7:49am
   
Thanks! ravuravu just to guide you further please see tutorials on how to use sql command parameters since directly concatenating the values in sql command is not a good option.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

I think you are missing comma in the update SQL query. You should correct like this

string s11 = "update Item set Item='" + TextBox1.Text + "', Quantity='" + nb + "', Price='" + TextBox3.Text + "', Date='" + dt + "', Month='" + month + "' where Item='" + TextBox1.Text + "' ";
  Permalink  
v2
Comments
ravuravu 17-Dec-12 6:32am
   
thanks
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

you are missing , after the column name, please see below query

string s11 = "update Item set Item='" + TextBox1.Text + "', Quantity='" + nb + "', Price='" + TextBox3.Text + "', Date='" + dt + "', Month='" + month + "' where Item='" + TextBox1.Text + "' ";

Note: Use parameterised query for security issues.
  Permalink  
v2

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 18 Dec 2012
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