Click here to Skip to main content
15,903,736 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have error as incorrect syntax near '=' , in given coding. please correct that error
C#
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
       {

           cmd = new SqlCommand(" select * from imagedemo where='"+listBox1.SelectedItem.ToString()+"'", con);
           con.Open();
           SqlDataReader dr;
           try
           {
               dr = cmd.ExecuteReader();
               if (dr.Read())
               {
                   byte[] picture = (byte[])dr["imgimage"];
                   ms = new MemoryStream(picture);
                   ms.Seek(0, SeekOrigin.Begin);
                   pictureBox1.Image = Image.FromStream(ms);

               }

           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
           finally
           {
               con.Close();
           }
       }
Posted
Updated 1-Nov-11 0:12am
v2
Comments
Abhinav S 1-Nov-11 6:13am    
Tags added.

There is an issue with the query select * from imagedemo where='"+listBox1.SelectedItem.ToString()+"'".
You have not provided the column name.

Try running this query in the SQL backend editor directly to see if the syntax is all correct.
 
Share this answer
 
Comments
Amir Mahfoozi 1-Nov-11 6:30am    
+5
Your SQL query does not state the field that you are querying against. Instead of WHERE = , you should have WHERE yourField = (where yourField should be substituted for the name af the field you wish to query against. You should also consider using parameterised queries instead of string concatenation, so I would write the query like this:-

SELECT field1, field2, field3 FROM myTable WHERE field4 = @field4Parameter;<br />
mycommand.Parameters.AddWithValue("@field4Parameter", myParameter)


Notice i named all my fields in the query instead of using the * symbol. That way when I retrieve the results they always come back in the same order.

Hope that helps
 
Share this answer
 
Comments
Abhinav S 1-Nov-11 6:27am    
5. My point as well.
Espen Harlinn 1-Nov-11 8:57am    
My 5 - using parameters instead of string concatenation helps to prevent sql injection, and should solev the problem too :)
You are missing parameter in where clause,must specify field name after where and before =
 
Share this answer
 

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