how solved this error the error is bellow
ERROR:
Unclosed quotation mark after the character string 'Cambridge'.
Incorrect syntax near 'Cambridge'.
my code is bellow
search.aspx.cs code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
namespace csvfileupload2
{
public partial class search : System.Web.UI.Page
{
string str = "Data Source=ANIRUDDHA-PC;Initial Catalog=sark;Integrated Security=True";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
String strQuery = "SELECT distinct (city) FROM profilesrr";
SqlConnection con = new SqlConnection(str);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
cmd.Connection = con;
try
{
con.Open();
DropDownList1.DataSource = cmd.ExecuteReader();
DropDownList1.DataTextField = "city";
DropDownList1.DataValueField = "city";
DropDownList1.DataBind();
// DropDownList2.SelectedIndex = 1;
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
String strQuery = "SELECT distinct (city) FROM profilesrr";
SqlConnection con = new SqlConnection(str);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
cmd.Connection = con;
try
{
con.Open();
DropDownList1.DataSource = cmd.ExecuteReader();
DropDownList1.DataTextField = "city";
DropDownList1.DataValueField = "city";
DropDownList1.DataBind();
// DropDownList2.SelectedIndex = 1;
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string str = "Data Source=ANIRUDDHA-PC;Initial Catalog=sark;Integrated Security=True";
SqlConnection con = new SqlConnection(str);
SqlCommand cmd = new SqlCommand("select * from profilesrr where city = '" + DropDownList1.SelectedValue + "", con);
SqlDataAdapter Adpt = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
Adpt.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
and search.aspx code are bellow<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="search.aspx.cs" Inherits="csvfileupload2.search" %>
Upendra Pratap ShahiPosted Jul 16, 2015, 3:23 AM
protected void Button1_Click(object sender, EventArgs e)
{
string str = "Data Source=ANIRUDDHA-PC;Initial Catalog=sark;Integrated Security=True";
SqlConnection con = new SqlConnection(str);
SqlCommand cmd = new SqlCommand("select * from profilesrr where city = '" + DropDownList1.SelectedItem.Text.ToString() + "'", con);
SqlDataAdapter Adpt = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
Adpt.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
Upendra Pratap ShahiPosted Jul 16, 2015, 3:29 AM
Posted Jul 16, 2015, 3:27 AM
Posted Jul 16, 2015, 3:21 AM
Upendra Pratap ShahiPosted Jul 16, 2015, 3:00 AM
Use below code-
{
string str = "Data Source=ANIRUDDHA-PC;Initial Catalog=sark;Integrated Security=True";
SqlConnection con = new SqlConnection(str);
SqlCommand cmd = new SqlCommand("select * from profilesrr where city = '" + DropDownList1.SelectedItem.Text.ToString() + "", con);
SqlDataAdapter Adpt = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
Adpt.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}