Click here to Skip to main content
12,756,705 members (30,401 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
Good day

Please help Im trying to get my form prepopulated when my page loads but i keep on getting this error: Procedure or function 'sp_Getuserinfo' expects parameter '@EmployeeNumber', which was not supplied.

IM I CALLING MY FUNCTION CORRECTLY ON PAGE LOAD?

MY STORE PROCEDURE:

USE [Portal]
GO
/****** Object: StoredProcedure [dbo].[sp_Getuserinfo] Script Date: 01/30/2013 07:11:13 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_Getuserinfo]
@EmployeeNumber varchar(30)

AS
BEGIN
-- [dbo].[sp_Getuserinfo] 'F3519554'
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here

SELECT EmployeeNumber
,CostCenter
,BusinessArea
,Telephone
,Category
,SubCategory

FROM [Portal].[dbo].[TaskRequest]
WHERE ID = (SELECT MAX(ID) FROM [Portal].[dbo].[TaskRequest]
WHERE EmployeeNumber = @EmployeeNumber)
END

MY CODE ON.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{

lblError.Visible = false;
// lblErrorConfirm.Visible = false;
btnSubmit.Enabled = true;
if (!IsPostBack)
{

//this.PopulateUserDetails();
#if DEBUG
ClearApplicationCache();

#endif



#region Max. file size message
System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
HttpRuntimeSection section = config.GetSection("system.web/httpRuntime") as HttpRuntimeSection;
double maxFileSize = Math.Round(section.MaxRequestLength / 1024.0, 1);
spUploadInfo.InnerText = spUploadInfo.InnerText + string.Format("Make sure the total file size is under {0:0.#} MB.", maxFileSize);
#endregion Max. file size message

lblLogin.Text = Page.User.Identity.Name;


if (SessionId == 1005)
{
SessionId = Login();
}




int statusId = ws.ServerStatus(SessionId);
string errorMessage = string.Empty;
switch (statusId)
{
case 1:
lblError.Visible = true;
btnSubmit.Enabled = false;
lblError.Text = "An error occurred while retrieving the session
Error details:
The service desk server is not available.";
errorMessage = "User: " + Page.User.Identity.Name + "\n
" + lblError.Text;

FitWebDev.Library.Logging.Log.WriteToEvent(errorMessage, System.Diagnostics.EventLogEntryType.Error, eventLogSource, "Application");
Session.Remove("sid");
break;
case 0:
#region Get Category and employee detail

string employeeNumber = Page.User.Identity.Name.Substring(Page.User.Identity.Name.LastIndexOf("\\") + 1);

lblEmployeeNumber.Text = employeeNumber;



//Pre_Populating Cost Center, Tel and Business Area // 25 JAN 2013
try
{

using (portalCnn)
{
portalCnn.Open();
using (SqlCommand command = new SqlCommand("sp_Getuserinfo", portalCnn))
{

SqlDataReader reader = command.ExecuteReader();
//SqlDataReader reader = GetUserDetails(lblEmployeeNo);

while (reader.Read())
{

lblEmployeeNumber.Text = (String)reader["EmployeeNumber"];
lblCC.Text = (String)reader["CostCenter"];
txtTelephone.Text = (String)reader["Telephone"];
ddlBusinessArea.Text = (String)reader["BusinessArea"];
ddlCategory.Text = (String)reader["Category"];
ddlSubCategory.Text = (String)reader["SubCategory"];

}
}
}
FUNCTION//Pre_Populating Cost Center, Tel and Business Area : Date:29/01
private SqlDataReader PopulateUserDetails(string EmployeeNo)
{

using (portalCnn)
{

SqlCommand cmd = new SqlCommand("sp_Getuserinfo", portalCnn);

cmd.CommandType = System.Data.CommandType.StoredProcedure;
SqlParameter param = new SqlParameter("@EmployeeNumber", SqlDbType.Int);
param.Direction = ParameterDirection.InputOutput;
param.Value = lblEmployeeNumber.Text;
cmd.Parameters.Add(param);
cmd.Parameters.AddWithValue("@EmployeeNumber", EmployeeNo);

cmd.Parameters.Add("@EmployeeNumber", System.Data.SqlDbType.VarChar, 30);
param.Value = lblEmployeeNumber.Text;

SqlDataReader reader = cmd.ExecuteReader();
// reader.SelectCommand = cmd;

portalCnn.Open();
return cmd.ExecuteReader();


}
}


PLEASE ASSIST I DONT KNOW WHAT IM DOING WRONG CAUSE WHEN I TEST THE STORE PROCEDURE IS WORKING FINE ON THE DATABASE :-(
Posted 29-Jan-13 22:17pm
ESTHERM385

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Try the following code..

public SqlDataReader PopulateUserDetails(string EmployeeNo)
        {
            SqlDataReader reader = null;
            try
            {
                //get the connection string value from configuration file
                string strConnString = ConfigurationManager.ConnectionStrings["<connectionstringname>"].ConnectionString; 
                using(SqlConnection con = new SqlConnection())
                {
                    SqlCommand cmd = new SqlCommand();
                    con.ConnectionString = strConnString;
                    cmd.Connection = con;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "dbo.sp_Getuserinfo";
                    SqlParameter param = new SqlParameter("@EmployeeNumber", EmployeeNo);
                    cmd.Parameters.Add(param);
                    reader = cmd.ExecuteReader();
                }

            }
            catch (Exception ex)
            {
                if (handleErrors)
                {
                    //Log the error
                    LogManager.Writer(ex.Message,LogType.Error);
                }
                else
                    throw;
            }

            return reader;
        }


you are unnecessarily adding the SqlParameter more than once
following is your part of code
SqlParameter param = new SqlParameter("@EmployeeNumber", SqlDbType.Int);
param.Direction = ParameterDirection.InputOutput;
param.Value = lblEmployeeNumber.Text;
cmd.Parameters.Add(param);
cmd.Parameters.AddWithValue("@EmployeeNumber", EmployeeNo);
cmd.Parameters.Add("@EmployeeNumber", System.Data.SqlDbType.VarChar, 30);


with in your stored procedure, you are not declared any output parameter.. and you declared one output parameter in C# code.
that is the reason, it is throwing "expects parameter '@EmployeeNumber', which was not supplied."

If you want more help see the above code for ref.

Let us know, still if you are not able to resolve the prob..
Happy coding.. :)
  Permalink  
v2

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

    Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170217.1 | Last Updated 31 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100