Click here to Skip to main content
16,012,061 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected void Button1_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text != null)
        {
            String connectionString = "Data Source=mycomputer;Initial Catalog=master;Integrated Security=True";
            SqlConnection conn = new SqlConnection(connectionString);
            SqlCommand command = new SqlCommand("select * from listofjobs where jobs like '%'" + TextBox1.Text, conn);
            conn.Open();
            command.Connection = conn;
            SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
            GridView1.DataSource = reader;
            GridView1.DataBind();
            GridView1.Visible = true;
        }

       
       }
}


and getting the following error
{"Incorrect syntax near 'Clerk'."}
class-15
error code-2146232060
number-102
line number 1
Posted
Updated 12-Jul-12 3:01am
v2
Comments
[no name] 12-Jul-12 9:00am    
You left off the other single quote at the end of your query string.
[no name] 12-Jul-12 9:02am    
Actually now that I have edited your posting so that it looks decent, you need to move the second single quote to the end of your query string. "select * from listofjobs where jobs like '%" + TextBox1.Text + "'"

Try this:
SQL
SqlCommand command = new SqlCommand("select * from listofjobs where jobs like '%'" + "'" + TextBox1.Text + "'", conn);
 
Share this answer
 
Comments
[no name] 12-Jul-12 10:12am    
Too many single quotes maybe?
Manas Bhardwaj 12-Jul-12 11:20am    
Correct +5!
Prasad_Kulkarni 13-Jul-12 0:02am    
Thank you Manas!
Try
SqlCommand command = new SqlCommand("select * from listofjobs where jobs like '%" + TextBox1.Text + "'", conn);
 
Share this answer
 
Comments
Manas Bhardwaj 12-Jul-12 11:20am    
Correct +5!
Abhinav S 12-Jul-12 12:13pm    
Thanks Manas.
You can do somthing like this too:

C#
string job = TextBox1.Text;
string query = String.Format("select * from listofjobs where jobs like '%{0}'", job);
SqlCommand command = new SqlCommand(query, conn);


Where the value in variable "job"is assigned to the index {0} in the string "query".


But Prasad_Kulkarni answer your question, mine is just a sugestion.
 
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