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


alter procedure [dbo].[usp_currentdate] begin     -- set nocount on added prevent result sets     -- interfering select statements.     set nocount on;      -- insert statements procedure here     select convert(varchar(10), getdate(), 101)  end 

this stored procedure code. got correct date, unable read 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. throws exception

invalid attempt read when no data present

what changes have make?

try this, using executescalar fetch single row / single column value:

public datetime getcurrentdate() {     try     {         datetime dt;          // set connection , 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 query returns 1 row, 1 column -> use executescalar             object result = cmd.executescalar();              // if returned .....             if (result != null)             {                 // try convert datetime                 if (datetime.tryparse(result.tostring(), out dt)                 {                     return dt;                 }             }              // return default value, if no data read or not converted              return datetime.minvalue;         }     }     catch (exception ex)     {         throw;     } }  

Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

Nuget pack csproj using nuspec -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -