Answered by:
Must declare the scalar variable "@UserName".

Question
-
User481677910 posted
Hi,
I am having this error :
Must declare the scalar variable "@UserName".
my aspx page
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="true" OnPageIndexChanging="OnPageIndexChanging" PageSize="10"> <Columns> <asp:BoundField ItemStyle-Width="150px" DataField="Order_Name" HeaderText="Order_Name" /> <asp:BoundField ItemStyle-Width="150px" DataField="Date" HeaderText="Date" /> <asp:BoundField ItemStyle-Width="150px" DataField="Order#" HeaderText="Order#" /> <asp:BoundField ItemStyle-Width="150px" DataField="Status" HeaderText="Status" /> </Columns> </asp:GridView>
and my cs is
protected void Page_Load(object sender, EventArgs e) { Session["UserName"] = HttpContext.Current.User.Identity.Name; if (!this.IsPostBack) { this.BindGrid(); } } private void BindGrid() { string constr = ConfigurationManager.ConnectionStrings["urConnectionString"].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand("SELECT Order_Name, Date, Order#, Status FROM DB_120 WHERE UserName= @UserName")) { using (SqlDataAdapter sda = new SqlDataAdapter()) { cmd.Connection = con; sda.SelectCommand = cmd; using (DataTable dt = new DataTable()) { sda.Fill(dt); GridView1.DataSource = dt; GridView1.DataBind(); } } } } } protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; this.BindGrid(); } }
Can anyone help me in this please.
Friday, February 16, 2018 12:08 AM
Answers
-
User409696431 posted
Your Select statement is expecting a value for the select parameter @Username. You have not defined that anywhere.
Before using your select command, you must define any parameters and give them a value:
cmd.Parameters.AddWithValue("@Username", HttpContext.Current.User.Identity.Name);
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, February 16, 2018 1:14 AM
All replies
-
User409696431 posted
Your Select statement is expecting a value for the select parameter @Username. You have not defined that anywhere.
Before using your select command, you must define any parameters and give them a value:
cmd.Parameters.AddWithValue("@Username", HttpContext.Current.User.Identity.Name);
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, February 16, 2018 1:14 AM -
User481677910 posted
Your Select statement is expecting a value for the select parameter @Username. You have not defined that anywhere. Before using your select command, you must define any parameters and give them a value: cmd.Parameters.AddWithValue("@Username", HttpContext.Current.User.Identity.Name);
Thank you. It solved my problem.
using (SqlDataAdapter sda = new SqlDataAdapter()) { cmd.Parameters.AddWithValue("@Username", HttpContext.Current.User.Identity.Name);
Friday, February 16, 2018 7:56 AM