Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
default.aspx.cs



error is The parameterized query '(@UserName nvarchar(4000))select * from tblRegistration where Em' expects the parameter '@UserName', which was not supplied.


error in out put

The parameterized query '(@UserName nvarchar(4000))select * from tblRegistration where Em' expects the parameter '@UserName', which was not supplied.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: The parameterized query '(@UserName nvarchar(4000))select * from tblRegistration where Em' expects the parameter '@UserName', which was not supplied.

Source Error:


Line 31: //SqlDataReader dr=cmd.ExecuteReader();
Line 32: DataTable dt = new DataTable();
Line 33: da.Fill(dt);
Line 34: txtFirstname.Text = dt.Rows[0].ItemArray[1].ToString();
Line 35: txtLastname.Text = dt.Rows[0].ItemArray[2].ToString();
Posted
Comments
PhilLenoir 27-Aug-14 8:57am    
We need to see how you are setting up your query (command object and it's parameters?). Please use the improve your question function.
harshavardhan12345678 27-Aug-14 9:02am    
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=SREENI\\SQLEXPRESS;Initial Catalog=TestDB;User ID=sa;Password=sa123");
SqlCommand cmd;
//int upid;
string UserName;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
retrieve();
}
}
public void retrieve()
{
con.Open();
cmd = new SqlCommand("select * from tblRegistration where Email=@UserName");
cmd.Connection = con;
cmd.Parameters.AddWithValue("@UserName", Request.QueryString["UserName"]);
SqlDataAdapter da = new SqlDataAdapter(cmd);

//SqlDataReader dr=cmd.ExecuteReader();
DataTable dt = new DataTable();
da.Fill(dt);
txtFirstname.Text = dt.Rows[0].ItemArray[1].ToString();
txtLastname.Text = dt.Rows[0].ItemArray[2].ToString();
txtPwd.Text = dt.Rows[0].ItemArray[3].ToString();
string ddl = dt.Rows[0].ItemArray[4].ToString();
ddlNationality.Items.FindByText(ddl).Selected = true;
string cc = dt.Rows[0].ItemArray[5].ToString();
ddlCurrentLoc.Items.FindByText(cc).Selected = true;
txtMNum.Text = dt.Rows[0].ItemArray[6].ToString();
string gen = dt.Rows[0].ItemArray[7].ToString();
ddlGender.Items.FindByText(gen).Selected = true;
txtKeyskills.Text = dt.Rows[0].ItemArray[8].ToString();
txtResumeTitle.Text = dt.Rows[0].ItemArray[9].ToString();
//= dt.Rows[0].ItemArray[10].ToString();

string url1 = dt.Rows[0].ItemArray[11].ToString();
img.ImageUrl = "images/" + url1;
txtEmail.Text = dt.Rows[0].ItemArray[13].ToString();
cmd.ExecuteNonQuery();
con.Close();
}
protected void btnSubmit_Click(object sender, EventArgs e)
{

}
}
Ashi0891 27-Aug-14 12:39pm    
You should have better added this code in your question rather than putting it in comments, as unformatted code, using Improve question option.
[no name] 27-Aug-14 9:00am    
"expects the parameter '@UserName', which was not supplied" tells you clearly that you did not supply the required parameters for your query. Supply the required parameters.
harshavardhan12345678 27-Aug-14 9:02am    
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=SREENI\\SQLEXPRESS;Initial Catalog=TestDB;User ID=sa;Password=sa123");
SqlCommand cmd;
//int upid;
string UserName;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
retrieve();
}
}
public void retrieve()
{
con.Open();
cmd = new SqlCommand("select * from tblRegistration where Email=@UserName");
cmd.Connection = con;
cmd.Parameters.AddWithValue("@UserName", Request.QueryString["UserName"]);
SqlDataAdapter da = new SqlDataAdapter(cmd);

//SqlDataReader dr=cmd.ExecuteReader();
DataTable dt = new DataTable();
da.Fill(dt);
txtFirstname.Text = dt.Rows[0].ItemArray[1].ToString();
txtLastname.Text = dt.Rows[0].ItemArray[2].ToString();
txtPwd.Text = dt.Rows[0].ItemArray[3].ToString();
string ddl = dt.Rows[0].ItemArray[4].ToString();
ddlNationality.Items.FindByText(ddl).Selected = true;
string cc = dt.Rows[0].ItemArray[5].ToString();
ddlCurrentLoc.Items.FindByText(cc).Selected = true;
txtMNum.Text = dt.Rows[0].ItemArray[6].ToString();
string gen = dt.Rows[0].ItemArray[7].ToString();
ddlGender.Items.FindByText(gen).Selected = true;
txtKeyskills.Text = dt.Rows[0].ItemArray[8].ToString();
txtResumeTitle.Text = dt.Rows[0].ItemArray[9].ToString();
//= dt.Rows[0].ItemArray[10].ToString();

string url1 = dt.Rows[0].ItemArray[11].ToString();
img.ImageUrl = "images/" + url1;
txtEmail.Text = dt.Rows[0].ItemArray[13].ToString();
cmd.ExecuteNonQuery();
con.Close();
}
protected void btnSubmit_Click(object sender, EventArgs e)
{

}
}

1 solution

Couple of things to note here.

1. No need to open the connection when you are using SqlDataAdapter.
2. As you are giving QueryString value as a parameter, so you should check if it exists or not before processing.
C#
if(Request.QueryString["UserName"] != null && !string.IsNullOrEmpty(Request.QueryString["UserName"].ToString()))
{
    // Do everything. Add parameter and execute the command.
}
 
Share this answer
 
Comments
Ashi0891 27-Aug-14 12:37pm    
Yeah one needs to check for null or existance of querystring parameter. Make sure UserName querystring parameter has some value before passing it. +5
Thanks. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900