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
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="output.aspx.cs" Inherits="output" %>
;Comparable Entites in Lexical Patterns
5 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Keertti Raj Thangavelu BabuPosted Mar 20, 2015, 1:57 AM
use this code
Note: you should concatenate % sign in string itself not in a sql query
KrishnaPosted Mar 23, 2015, 2:18 AM
Khargesh RajputPosted Mar 23, 2015, 2:10 AM
KrishnaPosted Mar 23, 2015, 2:00 AM
Khargesh RajputPosted Mar 20, 2015, 1:04 AM
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