| | |
Incorrect syntax near '='.
Please support our ASP.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2009
Posts: 1
Reputation:
Solved Threads: 0
Hi there,
Im getting the error above, when i run my code.
All i want to do is query the MS SQL database for data and extract row by row into an HTML table. This has proven to be a real pain in the neck. This simple function is sooo easy with PHP.
Please let me know if im on the right track, or if you can help with my error, i appreciate your help!
Thanks!
Im getting the error above, when i run my code.
string myCommand = "SELECT * FROM Manager WHERE UserName=" + ID;
SqlDataAdapter da = new SqlDataAdapter(myCommand, con);
DataSet ds = new DataSet();
try
{
con.Open();
da.Fill(ds); // <- highlighting this part of the code..
}
finally
{
con.Close();
}
foreach (DataRow dr in ds.Tables[0].Rows)
{
txtUserName.Text = dr[1].ToString();
}
}
}All i want to do is query the MS SQL database for data and extract row by row into an HTML table. This has proven to be a real pain in the neck. This simple function is sooo easy with PHP.
Please let me know if im on the right track, or if you can help with my error, i appreciate your help!
Thanks!
Last edited by RobertKramers; Apr 30th, 2009 at 10:37 am.
Try enclosing your ID value in single quotes.
ASP.NET Syntax (Toggle Plain Text)
string myCommand = "SELECT * FROM Manager WHERE UserName='" + ID + "'";
I would highly recommend against building your queries dynamically like this. You should use parameterized SQL for security and performance reasons, please see:
http://www.daniweb.com/forums/thread176306.html
Here is sample code for your situation:
http://www.daniweb.com/forums/thread176306.html
Here is sample code for your situation:
ASP.NET Syntax (Toggle Plain Text)
private void simpleButton1_Click(object sender, EventArgs e) { const string connStr = @"Data Source=apex2006sql;Initial Catalog=Leather;Integrated Security=True;"; const string query = "Select * From Invoice Where InvNumber = @InvNumber"; const int invNumber = 1100; DataSet ds = new DataSet(); using (SqlConnection conn = new SqlConnection(connStr)) { conn.Open(); using (SqlCommand cmd = new SqlCommand(query, conn)) { cmd.Parameters.Add("@InvNumber", SqlDbType.Int).Value = invNumber; using (SqlDataAdapter da = new SqlDataAdapter(cmd)) { da.Fill(ds); } } conn.Close(); } }
CHECK THAT YOUR ID CONTAINS A VALUE OR NOT. I THINK YOUR ID DOES NOT CONTAIN VALUE... So THAT YOUR QUERY WILL BE LIKE WHERE ...= SO WHEN YOU EXECUTE THE QUERY GETS THIS ERROR.
MARK AS SOLVED if its help you.
REGARDS
MCTS - Shawpnendu bikash maloroy
http://shawpnendu.blogspot.com
REGARDS
MCTS - Shawpnendu bikash maloroy
http://shawpnendu.blogspot.com
This also gives another reason why parameterized queries should be used, because in the case of a blank ID it would run the query looking for null.
Totally agree with SKnake on this, any kind of attempt to use dynamic sql should include thorough checking for SQL injection attempts.
The solution is as stated that the parameter is text and should be enclosed in single quotes spaces around the equals will make no difference at all, if the parameter potentially could contain Unicode text like Japanese or Chinese characters it should also be prefixed with an N to let SQL server know that the contents could be of the NVARCHAR type.
The solution is as stated that the parameter is text and should be enclosed in single quotes spaces around the equals will make no difference at all, if the parameter potentially could contain Unicode text like Japanese or Chinese characters it should also be prefixed with an N to let SQL server know that the contents could be of the NVARCHAR type.
![]() |
Similar Threads
- Syntax error! Urgent. (JSP)
- System.Data.Sqlclient.Sqlexception: Line1 incorrect syntax at ']'. (ASP.NET)
- Incprrect Syntax Problem (ASP.NET)
- Syntax error - confused (ASP)
- Incorrect syntax near '=' (MS SQL)
- Incorrect syntax near '='. (MS SQL)
- Syntax error in "like" in sql statement (ASP.NET)
Other Threads in the ASP.NET Forum
- Previous Thread: How to create new web pages from code
- Next Thread: asp.net
Views: 1005 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for ASP.NET
.net 2.0 activexcontrol advice ajax alltypeofvideos anathor application asp asp.net bc30451 bottomasp.net browser button c# checkbox click commonfunctions confirmationcodegeneration css dataaccesslayer database datagridview datagridviewcheckbox datalist deadlock development dgv dropdownlist dynamically edit editing expose feedback fill flash form formatdecimal formview google grid gridview iframe iis javascript list listbox login microsoft migration mono mouse mssql multistepregistration news numerical object objects opera panelmasterpagebuttoncontrols parent problem project radio registration reportemail richtextbox rotatepage rows save schoolproject search security session silverlight smartcard smoobjects software sql-server sqlserver2005 suse textbox tracking unauthorized update validation vb.net video videos view virtualdirectory vista visualstudio web webapplications webdevelopemnt webprogramming webservice xsl youareanotmemberofthedebuggerusers






