Write It this Way
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
Country:
<asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
Id:
<asp:TextBox ID="txtId" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Search" OnClick = "Search" />
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false" OnPageIndexChanging="GridView1_OnPageIndexChanging" PageSize="3" AllowPaging="true">
<Columns>
<asp:BoundField DataField="CustomerId" HeaderText="Contact Id" />
<asp:BoundField DataField="ContactName" HeaderText="Contact Id" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
protected void Search(object sender, EventArgs e)
{
this.BindGrid();
}
private void BindGrid()
{
string conString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
string query = "SELECT * FROM Customers";
string country = txtSearch.Text.Trim();
string countryNames = string.Empty;
if (!string.IsNullOrEmpty(country))
{
if (country.Contains(","))
{
string[] countries = country.Split(',');
foreach (string s in countries)
{
countryNames += "'" + s + "'" + ",";
}
countryNames = countryNames.Remove(countryNames.LastIndexOf(","));
country = string.Format(" where Country IN ({0}) AND CustomerId = '{1}'", countryNames,this.txtId.Text.Trim());
}
else
{
country = string.Format(" where Country = '{0}' AND CustomerId = '{1}'", country, this.txtId.Text.Trim());
}
}
SqlCommand cmd = new SqlCommand(query + country);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
}
}
protected void GridView1_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
this.BindGrid();
}
}
If your Id is integer then remove '' from CustomerId = '{1}'