Incorrect syntax near '='.

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2009
Posts: 1
Reputation: RobertKramers is an unknown quantity at this point 
Solved Threads: 0
RobertKramers RobertKramers is offline Offline
Newbie Poster

Incorrect syntax near '='.

 
0
  #1
Apr 30th, 2009
Hi there,

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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 6
Reputation: mrGee is an unknown quantity at this point 
Solved Threads: 0
mrGee mrGee is offline Offline
Newbie Poster

Re: Incorrect syntax near '='.

 
0
  #2
Apr 30th, 2009
try to change your code to :

string myCommand = "SELECT * FROM Manager WHERE UserName= ID";

it should work if it doesn't try more alternatives
. [/I]
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,439
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Incorrect syntax near '='.

 
0
  #3
Apr 30th, 2009
Try enclosing your ID value in single quotes.
  1. string myCommand = "SELECT * FROM Manager WHERE UserName='" + ID + "'";
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 114
Reputation: ninjaimp is an unknown quantity at this point 
Solved Threads: 6
ninjaimp ninjaimp is offline Offline
Junior Poster

Re: Incorrect syntax near '='.

 
0
  #4
Apr 30th, 2009
you need a space before and after the "=" sign. That may help.

Only enlcose your ID value in single quotes if is a text value.
Please mark this post as 'Solved' if it has helped and fixed your issue.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,215
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 573
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Incorrect syntax near '='.

 
0
  #5
May 2nd, 2009
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:
  1. private void simpleButton1_Click(object sender, EventArgs e)
  2. {
  3. const string connStr = @"Data Source=apex2006sql;Initial Catalog=Leather;Integrated Security=True;";
  4. const string query = "Select * From Invoice Where InvNumber = @InvNumber";
  5. const int invNumber = 1100;
  6.  
  7. DataSet ds = new DataSet();
  8.  
  9. using (SqlConnection conn = new SqlConnection(connStr))
  10. {
  11. conn.Open();
  12. using (SqlCommand cmd = new SqlCommand(query, conn))
  13. {
  14. cmd.Parameters.Add("@InvNumber", SqlDbType.Int).Value = invNumber;
  15. using (SqlDataAdapter da = new SqlDataAdapter(cmd))
  16. {
  17. da.Fill(ds);
  18. }
  19. }
  20. conn.Close();
  21. }
  22. }
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 217
Reputation: mail2saion is an unknown quantity at this point 
Solved Threads: 32
mail2saion's Avatar
mail2saion mail2saion is offline Offline
Posting Whiz in Training

Re: Incorrect syntax near '='.

 
0
  #6
May 2nd, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,215
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 573
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Incorrect syntax near '='.

 
0
  #7
May 2nd, 2009
Originally Posted by mail2saion View Post
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.
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.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 130
Reputation: sedgey is on a distinguished road 
Solved Threads: 8
sedgey's Avatar
sedgey sedgey is offline Offline
Junior Poster

Re: Incorrect syntax near '='.

 
0
  #8
May 3rd, 2009
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.
David Ridgway: so little daylight, too much caffeine
MCSD MCAD MCSE
http://web2asp.net
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC