0

Hi i am getting Object reference not set to an instance of an object error . while executing the Execute scalar command i am getting this error.

My table

 Create table Dashboard_Template
 (
 Dashboard_TemplateID int IDENTITY(1,1) primary key not null,
 FromDate datetime  null,
 ToDate datetime  null,
 Body nvarchar(max)null,
 CreatedBy varchar(100)  null,
 CreatedDate datetime null,
 ModifiedBy int null,
 ModifiedDate datetime null,
 Status varchar(100) null
 )

My Store Procedure

 CREATE PROCEDURE [dbo].[Dashboard_TemplateCreateUpdate]

  @FromDate DATEtime,
  @ToDate DATEtime,
  @Body nvarchar(max),
  @SavedBy varchar(100)='',
  @SavedDate DATEtime,
  @Status varchar(20)
 AS
 BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here


  Insert into Dashboard_template(FromDate,ToDate,Body,CreatedBy,CreatedDate,Status)values(@FromDate,@ToDate,@Body,@SavedBy,@SavedDate,@Status)
END
GO

My DAL code

  public string CreateDashboardTemplate(DashboardModel DashboardObj)
    {
        ISqlDBHelper sqlDBHelper = new SqlDBHelper();
        SqlParameter[] parameter = new SqlParameter[]{
            new SqlParameter("@FromDate",DashboardObj.FromDate),
            new SqlParameter("@ToDate",DashboardObj.ToDate),
            new SqlParameter("@Body",DashboardObj.Body),                
            new SqlParameter("@SavedBy",DashboardObj.SavedBy),
            new SqlParameter("@SavedDate",DashboardObj.SavedDate),
             new SqlParameter("@Status","AC")
        };
        return sqlDBHelper.ExecuteScalar("Dashboard_TemplateCreateUpdate", CommandType.StoredProcedure, parameter);
    }

In this above dal code i am getting Object reference not set to an instance of an object.error while executing this scalar command.i donno in which place i did mistake .Kindly any one understand and help me to resolve this issue.

freedomn-m
  • 27,664
  • 8
  • 35
  • 57
susan
  • 165
  • 4
  • 19
  • 1
    Could be that your SP doesn't return anything so there's nothing for the "scalar" to return https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlcommand.executescalar?view=dotnet-plat-ext-3.1 *Executes the query, and returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored.* - try `ExecuteNonQuery` if you're not going to return anything – freedomn-m May 23 '20 at 11:45
  • getting error in ExecuteNonQuery.. cannot implicity convert type bool to string return sqlDBHelper.ExecuteNonQuery("Dashboard_TemplateCreateUpdate", CommandType.StoredProcedure, parameter); – susan May 23 '20 at 11:49
  • Your SP doesn't return anything, so what do you think `string CreateDashboardTemplate` is/should be returning? – freedomn-m May 23 '20 at 14:08

0 Answers0