参数值在c#asp.net中传递

时间:2013-06-20 12:51:30

标签: c# asp.net sql-server-2008

我有2个asp页面。首先我通过登录页面登录,第二页是主页,我有一些任务的按钮。除此之外,我获得了包含FULLNAME,ADDRESS,CELL NUMBER,BLOOD GROUP和EMAILID的用户详细信息,一旦用户使用他的用户名和密码登录,就应该在DATABASE的特定标签中动态显示。

我在GetLoginDetails存储过程中为此编写了查询。我必须在登录后显示员工姓名,上次登录日期,时间等,并以与获取用户详细信息相同的方式进入主页。

 ALTER PROCEDURE [dbo].[GetLastLogin]

 @LoggedInUser nvarchar(50),
 @FullName nvarchar(50),
 @Address nvarchar(50),
 @MobileNumber bigint,
 @EmailID nvarchar(50),
 @BloodGroup nvarchar(50),
 @EmpName nvarchar(50)

 As

 Declare @LastLogin int

 Set @LastLogin = (Select MAX(AccessID)from dbo.ACCESS_INFO where Flag = 1)

 Select Access_Date, Access_Time from dbo.ACCESS_INFO where LoggedInUser = @LoggedInUser     and AccessID = @LastLogin

 Update dbo.EmployeeData 
 Set Empname = @EmpName
 where FullName = @FullName and Address = @Address and MobileNumber = @MobileNumber and     EmailID = @EmailID and BloodGroup = @BloodGroup ;

我得错误说(“程序或功能'GetLastLogin'期望参数'@FullName',这是未提供的。”)请帮帮我

后端代码

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Username"] != null)
        {
            try
            {
                MTMSDTO objc = new MTMSDTO();

                LblLogdInUser.Text = Session["EmpName"].ToString();
                LblUser.Text = Session["Username"].ToString();


                objc.LoggedInUser = LblUser.Text;
                DataSet laslogin = obj.GetLastLogin(objc);
                DataView LasLogin = new DataView();
                LasLogin.Table = laslogin.Tables[0];
                GrdLasLogin.DataSource = LasLogin;
                GrdLasLogin.DataBind();

                if (!IsPostBack)
                {
                    int lastlog = GrdLasLogin.Rows.Count;
                    if (lastlog == 0)
                    {
                        LblLastLoginD.Text = "This is your First Login";

                        DateTime today = System.DateTime.Now.Date;
                        LblToday.Text = today.ToString();
                        LblTime.Text = System.DateTime.Now.ToLongTimeString();

                        objc.LoggedInUser = LblLogdInUser.Text;
                        objc.AccessDate = Convert.ToDateTime(LblToday.Text);
                        objc.AccessTime = Convert.ToDateTime(LblTime.Text);
                        objc.AccessStatus = "New Login";
                        objc.AccessFlag = 1;

                        int accessinfo = obj.InsertAccessInfo(objc);
                    }

                    else
                    {
                        LblLastLoginD.Text =  Convert.ToDateTime(GrdLasLogin.Rows[0].Cells[0].Text).ToString("dd/MMM/yyyy");
                        LblLastLoginT.Text = GrdLasLogin.Rows[0].Cells[1].Text;

                        DateTime today = System.DateTime.Now.Date;
                        LblToday.Text = today.ToString();
                        LblTime.Text = System.DateTime.Now.ToLongTimeString();

                        objc.LoggedInUser = LblLogdInUser.Text;
                        objc.AccessDate = Convert.ToDateTime(LblToday.Text);
                        objc.AccessTime = Convert.ToDateTime(LblTime.Text);
                        objc.AccessStatus = "New Login";
                        objc.AccessFlag = 1;

                        int accessinfo = obj.InsertAccessInfo(objc);
                    }

                    LblFname.Visible = true;
                    LblAdd.Visible = true;
                    LblMnum.Visible = true;
                    LblMailID.Visible = true;
                    LblBGroup.Visible = true;
                }
            }

            catch (Exception ex)
            {
                Response.Redirect("ERROR.aspx");
                Session.Abandon();
            }

        }

        else
        {
            Response.Redirect("~/Login.aspx");
        }

        Response.CacheControl = "no-cache";

    }

2 个答案:

答案 0 :(得分:2)

错误消息表明您需要提供参数FullName的值。所以,如果你还没有这样做,那就去做吧。这里唯一的复杂因素是null值; string可以是null,但要在ADO.NET中指定您需要传递DBNull.Value;如果您使用null,则不包含参数 。这意味着您最终得到的代码如下:

cmd.Parameters.AddWithValue("FullName", (object)fullName ?? DBNull.Value);

丑陋,但确实有效。

或者,许多帮助程序实用程序将为您执行此操作。所以用“小巧玲珑”:

var lastAccess = conn.Query<AccessInfo>("GetLastLogin",
          new { LoggedInUser = cn, FullName = fullName, /* snipped */ },
          commandType: CommandType.StoredProcesdure).FirstOrDefault();

答案 1 :(得分:0)

问题不在你的SQL中。它在你的asp中的调用函数中。您没有正确地将fullname参数发送到SQL Server。查看此问题以获取有关如何发送参数的示例。 Call a stored procedure with parameter in c#