i would like to add the retrieved data to the grid view but its displaying error
Must declare the scalar variable "@x".
.cs
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
DataSet ds = new DataSet();
//var x = Request.QueryString ;
con.Open();
string x = Request.QueryString["Name"];
string selectString = "select * from products where keyword like @x";
SqlCommand cmd = new SqlCommand(selectString, con);
SqlDataAdapter da = new SqlDataAdapter("select * from products where keyword like @x", con);
cmd.Parameters.AddWithValue("@x", "%" + x + "%");
SqlDataReader reader = cmd.ExecuteReader();
List values = new List();
da.Fill(ds,x);
GridView1.DataSource = ds;
GridView1.DataBind();
try
{
while (reader.Read())
{
values.Add(new product()
{
keyword = reader.GetString(reader.GetOrdinal("keyword"))
});
}
reader.Close();
}
catch (Exception ex)
{
}
}
public class product
{
public string keyword { get; set; }
}
}
.aspx
;
Keertti Raj Thangavelu BabuRank #3,979 · 374 reputationPosted Mar 19, 2015, 9:57 PM EDT
use this code
Note: you should concatenate % sign in string itself not in a sql query
Khargesh RajputRank #616 · 3.9K reputationPosted Mar 19, 2015, 9:04 PM EDT
SqlDataAdapter da = new SqlDataAdapter("select * from products where keyword like @x", con);
to
SqlDataAdapter da = new SqlDataAdapter(cmd, con);
use sqldataadapter like below
http://www.webcodeexpert.com/2013/02/how-to-bind-gridview-using.html
Krishna39 reputationPosted Mar 22, 2015, 10:18 PM EDT
Krishna39 reputationPosted Mar 22, 2015, 10:00 PM EDT
Khargesh RajputRank #616 · 3.9K reputationPosted Mar 22, 2015, 10:10 PM EDT