Problem is that, when i'm selecting CustomerID=2, its still showing the CustomerID=1 details irrespective of my selection. and an error: Must declare the scalar variable
"@Customerid".
DefaultQueryString.aspx
<div>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/QueryStringParameter.aspx?QSCustomerID=2">HyperLink</asp:HyperLink>
</div>
QueryStringParameter.aspx
<div>
<asp:DetailsView ID="DetailsView1" runat="server"></asp:DetailsView>
<selectParameters>
<asp:QueryStringParameter Name="CustomerID" QueryStringField="QSCustomerID" />
</selectParameters>
</div>
QueryStringParameter.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
bindDetailsView();
}
public void bindDetailsView()
{
SqlConnection con = new SqlConnection("Data Source=STD-258E51EA446\\SQLEXPRESS;Initial Catalog=Customers;Integrated Security=True");
SqlCommand cmd = new SqlCommand("Select CustomerID,CustomerName,Address from Customer where CustomerID=@CustomerID", con);
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
cmd.ExecuteScalar();
DetailsView1.DataSource = ds;
DetailsView1.DataBind();
con.Close();
}
The SelectParameters belong to a SqlDataSource control that you don't appear to be using. Remove them from the ASPX and change the code behind to add the parameter there instead:
void bindDetailsView()
{
SqlConnection con = new SqlConnection("Data Source=STD-258E51EA446\\SQLEXPRESS;Initial Catalog=Customers;Integrated Security=True");
SqlCommand cmd = new SqlCommand("Select CustomerID,CustomerName,Address from Customer where CustomerID=@CustomerID", con);
cmd.Parameters.AddWithValue("@CustomerID", Request.QueryString["QSCustomerID"]);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
DetailsView1.DataSource = ds;
DetailsView1.DataBind();
}
rookie tiro
Member
86 Points
130 Posts
Querystring doesnt work
Feb 08, 2012 06:06 PM|LINK
"@Customerid".
Mikesdotnett...
All-Star
154927 Points
19867 Posts
Moderator
MVP
Re: Querystring doesnt work
Feb 08, 2012 06:47 PM|LINK
The SelectParameters belong to a SqlDataSource control that you don't appear to be using. Remove them from the ASPX and change the code behind to add the parameter there instead:
void bindDetailsView() { SqlConnection con = new SqlConnection("Data Source=STD-258E51EA446\\SQLEXPRESS;Initial Catalog=Customers;Integrated Security=True"); SqlCommand cmd = new SqlCommand("Select CustomerID,CustomerName,Address from Customer where CustomerID=@CustomerID", con); cmd.Parameters.AddWithValue("@CustomerID", Request.QueryString["QSCustomerID"]); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); DetailsView1.DataSource = ds; DetailsView1.DataBind(); }Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter