简体   繁体   中英

How to read the date that is return from a stored procedure which have no parameter using asp.net?

ALTER PROCEDURE [dbo].[usp_currentDate]
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    SELECT CONVERT(VARCHAR(10), GETDATE(), 101) 
END

This is my stored procedure code. I got the correct date, but I am unable to read the values in asp.net.

public DateTime getCurrentDate()
{
    try
    {
        DateTime dt;
        SqlDataReader reader;

        SqlConnection objSqlConn = new SqlConnection(ConnString);

        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "usp_currentDate";
        cmd.Connection = objSqlConn;

        objSqlConn.Open();
        me(1);
        reader = cmd.ExecuteReader();
        dt = reader.GetDateTime(1);  
        return dt;  
    }
    catch (Exception ex)
    {
        throw ex;
    }
} 

but it's not working. It throws an exception

Invalid attempt to read when no data is present

What changes do I have to make?

Try something like this, using ExecuteScalar to fetch a single row / single column value:

public DateTime getCurrentDate()
{
    try
    {
        DateTime dt;

        // set up connection and command in *using* blocks
        using (SqlConnection objSqlConn = new SqlConnection(ConnString))
        using (SqlCommand cmd = new SqlCommand("dbo.usp_currentDate", objSqlConn))
        {
            cmd.CommandType = CommandType.StoredProcedure;

            objSqlConn.Open();

            // since the query returns exactly one row, one column -> use ExecuteScalar
            object result = cmd.ExecuteScalar();

            // if something was returned .....
            if (result != null)
            {
                // try to convert to DateTime
                if (DateTime.TryParse(result.ToString(), out dt)
                {
                    return dt;
                }
            }

            // return default value, if no data was read or could not be converted 
            return DateTime.MinValue;
        }
    }
    catch (Exception ex)
    {
        throw;
    }
} 

Besides on why you return local time with GETDATE as a character, you can't read the data reader without move the cursor to it's first line with Read method .

From documentation;

The default position of the SqlDataReader is before the first record. Therefore, you must call Read to begin accessing any data.

string s;
reader = cmd.ExecuteReader();
if(reader.Read())
   s = reader.GetString(0);

Or better, use ExecuteScalar method since you return one row with one column like;

string s = (string)cmd.ExecuteScalar();

Remember, CONVERT(VARCHAR(10), GETDATE(), 101) returns varchar which is preferable used with string in CLR side, that's why you can't assign this value to a DateTime variable.

Also use using statement to dispose your database connections and commands.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM