title: c# and sql Incorrect syntax near the keyword 'and'
body: <p>I have tried to find the syntax error into my sql statment but i couldnt i tried to add () and [] but its same nothing change so could please help me with this error as i am getting this error message: " Incorrect syntax near the keyword 'and'. " next to : " ad.Fill(cdt);"</p>
  
  <pre><code>HttpCookie cookie = Request.Cookies.Get("Location");
          using (SqlConnection carcon = new SqlConnection(ConfigurationManager.ConnectionStrings["BeravaConnectionString"].ConnectionString))
  
              if (cookie != null)
              {
              string CarSqlST = @"SELECT [JobNum], [Dept], [PubDate], [EndDate], [Employer],[VacCountry], [VacState], [VacCity],
              [Jobtitle], [CompLogo], SUBSTRING([jobdesc],1,40) as jobdesc FROM [jobs] Where 1=1 and [VacCountry] = [@Location] ORDER BY [PubDate] DESC ";
  
  
                  var Location = cookie.Value;
  
                  string condition = "";
  
                  if (filterstathpjob.SelectedValue != "")
                  {
                      condition += " and State='" + filterstathpjob.SelectedValue + "'";
                  }
                  if (filterJobhpjob.SelectedValue != "")
                  {
                      condition += " and City='" + filterJobhpjob.SelectedValue + "'";
                  }
  
                  DataTable cdt = new DataTable();
                  carcon.Open();
                  SqlCommand ccmd = new SqlCommand();
                  ccmd.Connection = carcon;
                  ccmd.CommandType = CommandType.Text;
                  ccmd.Parameters.AddWithValue("@Location", Location);
                  //ccmd.Parameters.AddWithValue("@CATE", cat);
                  ccmd.CommandText = CarSqlST + condition;
                  SqlDataAdapter ad = new SqlDataAdapter();
                  ad.SelectCommand = ccmd;
  
                  ad.Fill(cdt);
                  Joblistview.DataSource = cdt;
                  Joblistview.DataBind();
  
              }
  </code></pre>
  
  <p>the 2nd code is </p>
  
  <pre><code> protected void FilterBtn_Click(object sender, EventArgs e)
      {
          HttpCookie cookie = Request.Cookies.Get("Location");
          using (SqlConnection carcon = new SqlConnection(ConfigurationManager.ConnectionStrings["BeravaConnectionString"].ConnectionString))
  
              if (cookie != null)
              {
                  string sql = @"SELECT [JobNum], [Dept], [PubDate], [EndDate], [Employer],[VacCountry], [VacState], [VacCity],
              [Jobtitle], [CompLogo], SUBSTRING([jobdesc],1,40) as jobdesc FROM [jobs] 
              Where [VacCountry] = @Location AND
              (@State IS NULL OR VacState = @State) AND
              (@City IS NULL OR VacCity = @City)                
              ORDER BY [PubDate] DESC ";
  
                  DataTable cdt = new DataTable();
                  SqlCommand ccmd = new SqlCommand(sql, carcon);
                  var Location = cookie.Value;
                  ccmd.Parameters.AddWithValue("@Location", Location);
                  ccmd.Parameters.AddWithValue("@State", filterstathpjob.SelectedValue);
                  ccmd.Parameters.AddWithValue("@City", filterJobhpjob.SelectedValue);
  
                  SqlDataAdapter ad = new SqlDataAdapter(ccmd);
                  ad.Fill(cdt);
                  Joblistview.DataSource = cdt;
                  Joblistview.DataBind();
  
              }
      }
  </code></pre>