Krishna

Krishna

  • NA
  • 37
  • 9.5k

Databinding

Mar 20 2015 12:48 AM
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<product> values = new List<product>();
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" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" method="POST" runat="server">
<br />
<a href ="http://localhost:49369/Home.aspx">Home</a>
<a href =" http://localhost:49369/login.aspx">Login</a>
<br />
<br />
<img alt="" src="Images/aa2.png" style="height: 237px; width: 875px" />;
<h3>Comparable Entites in Lexical Patterns </h3>
<p>
&nbsp;</p>
<p>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</p>
<br />
<br />
</form>
</body>
</html>
 

Answers (5)