本文介绍了如何在Asp.net中的存储过程中将日期时间作为输出参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已编写程序以获取用户的最近登录日期时间,并且必须在标签的网页上显示。并且收到错误



I have Write a Program to get Recent Login date time of a User and have to display on th web page in the label. and getting error that

System.InvalidCastException: Unable to cast object of type 'System.DateTime' to type 'System.String'.










CREATE PROC uspGetRecentLogin
@UserID int,@RLoginDate datetime OUT
AS
BEGIN
SET @RLoginDate= (SELECT TOP 1 LoginDate FROM TblLoginDetails WHERE UserId=@UserID ORDER BY LoginDate DESC)
END










using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

public partial class CandMaster : System.Web.UI.MasterPage
{
    public String AppUserName;
    int AppUserID;
    DBClass db1 = new DBClass();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AppUserName"] != null & Session["AppUserID"] != null)
        {

            AppUserName = Convert.ToString(Session["AppUserName"]);
            AppUserID = Convert.ToInt32(Session["AppUserID"]);
        }
        else {  }
        LblUserLogin.Text = AppUserName;
        
        GetRecentLoginTime();
    }



    private void GetRecentLoginTime()
    {
    try
    {
             DBClass db1 = new DBClass();
             db1.sqlcmd = new SqlCommand("uspGetRecentLogin");

            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                db1.sqlcmd.CommandType = CommandType.StoredProcedure;
                db1.sqlcmd.Parameters.AddWithValue("@UserID", AppUserID);
                db1.sqlcmd.Parameters.Add("@RLoginDate", SqlDbType.DateTime);
                db1.sqlcmd.Parameters["@RLoginDate"].Direction = ParameterDirection.Output;
                db1.sqlcmd.Connection = db1.sqlcon;
                db1.sqlcon.Open();
                db1.sqlcmd.ExecuteScalar();
               LabelLastLogin.Text = (string) db1.sqlcmd.Parameters["@RLoginDate"].Value;
            }
        }
        catch (Exception ex) { Response.Write(ex);}
        finally { }
    
    
    
    
    }
}

推荐答案

这篇关于如何在Asp.net中的存储过程中将日期时间作为输出参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 19:08