A few tips:
1) You are repeating your code unnecessarily
2) Your code is vulnerable for
sql injections[
^]
My suggestion:
Build your SQL-query based on selection in dropdownlist:
string sql = "SELECT * FROM ['ISB VAS Nodes$'] ";
if (DropDownList1.Text == "Type")
{
sql += "where [Type]=@Type";
}
else if (DropDownList1.Text == "Desc")
{
sql += "where [Desc]=@SearchText";
}
else if (DropDownList1.Text == "Model")
{
sql += "where [Model]=@SearchText";
}
else if (DropDownList1.Text == "SN")
{
sql += "where [SN]=@SearchText";
}
else if (DropDownList1.Text == "Asset tag")
{
sql += "where [AssetTag]=@SearchText";
}
else if (DropDownList1.Text == "ID")
{
sql += "where [ID]=@SearchText";
}
Or you could use the dropdownlist's Id property to search in the correct column:
var column = DropDownList1.SelectedValue;
string sql = string.Format("SELECT * FROM ['ISB VAS Nodes$'] where [{0}]=@Type", column);
Then you can use your sql-query to to show the result in your grid.
SqlConnection con = new SqlConnection(connstring);
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddWithValue("@SearchText", TextBox2.Text);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
GridView1.DataSource = dr;
GridView1.DataBind();
}
else
{
Response.Write("Value does not exist");
}
dr.Close();
con.Close();
And to answer your question. Are you sure the data is in your database? I don't think its anything wrong with your code here. Double check your data.