Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have method gridbind which is shown below which is giving object reference error
so please any buddy tell me why it is giving error.
C#
public void Gridbind()
{
    StringBuilder var1 = new StringBuilder();
    var1.Append("SELECT tblpullsms.ID,Row_number() \n");
    var1.Append("         OVER ( \n");
    var1.Append("           ORDER BY tblpullsms.createddate DESC)           AS SrNo, \n");
    var1.Append("       fm.factoryname                                      AS FactoryName, \n");
    var1.Append("       ec.enumerationvalue                                 AS FactoryType, \n");
    var1.Append("       CONVERT(VARCHAR(10), tblpullsms.createddate, 103)   AS CreatedDate, \n");
    var1.Append("       tblpullsms.mobileno, \n");
    var1.Append("       CONVERT(VARCHAR(10), tblpullsms.createddate, 103) \n");
    var1.Append("       + '  ' \n");
    var1.Append("       + CONVERT(VARCHAR(10), tblpullsms.createddate, 108) AS CreatedDate1, \n");
    var1.Append("       cd.districtname, \n");
    var1.Append("       tblpullsms.code, \n");
    var1.Append("       Sum(tblpullsms.crush)                               AS Crush, \n");
    var1.Append("       Sum(tblpullsms.prod)                                AS Prod, \n");
    var1.Append("       Sum(tblpullsms.rate)                                AS Rate, \n");
    var1.Append("       Cast(Avg(tblpullsms.recov) AS NUMERIC(18, 2))       AS Recov, \n");
    var1.Append("       ev.enumerationvalue                                 AS Division \n");
    var1.Append("FROM   dbo.tblpullsms \n");
    var1.Append("       INNER JOIN factory_master fm \n");
    var1.Append("               ON fm.factorycode = tblpullsms.code \n");
    var1.Append("       INNER JOIN enumerationvalue ec \n");
    var1.Append("               ON ec.enumerationvalueid = fm.factorytype \n");
    var1.Append("       INNER JOIN census_district cd \n");
    var1.Append("               ON cd.districtcode = fm.districtid \n");
    var1.Append("       INNER JOIN enumerationvalue ev \n");
    var1.Append("               ON ev.enumerationvalueid = cd.divisionid \n");
    var1.Append("WHERE  cd.langid = 1 \n");
    var1.Append("       AND tblpullsms.isactive = 1 \n");
    var1.Append("       AND CONVERT(varchar(10), tblpullsms.createddate, 120) = convert(varchar(10),GetDate(),120) \n");
    var1.Append("GROUP  BY fm.factoryname, \n");
    var1.Append("          cd.districtname, \n");
    var1.Append("          tblpullsms.code, \n");
    var1.Append("          tblpullsms.createddate, \n");
    var1.Append("          ev.enumerationvalue, \n");
    var1.Append("          ec.enumerationvalue, \n");
    var1.Append("          tblpullsms.mobileno, \n");
    var1.Append("          tblpullsms.crush, \n");
    var1.Append("          tblpullsms.prod, \n");
    var1.Append("          tblpullsms.rate, \n");
    var1.Append("          tblpullsms.recov,tblpullsms.ID, \n");
    var1.Append("          ev.enumerationvalue \n");
    var1.Append("ORDER  BY tblpullsms.createddate DESC ");

    String strdailyreportsms = string.Empty;
    SqlCommand cmd = new SqlCommand();
    strdailyreportsms = var1.ToString();
    cmd.CommandType = CommandType.Text;
   
    cmd.CommandText = strdailyreportsms;
    DataSet ds = new DataSet();
    ds = MOLDBAccess.ExecuteDataSet(cmd);

    if (ds.Tables[0].Rows.Count > 0)
    {
        //grdupload.DataSource = ds;
        grdupload.DataSource = ds.Tables[0];
        grdupload.DataBind();


    }
    else
    {
        grdupload.DataSource = null;
        grdupload.DataBind();
    }
}
Posted
v2
Comments
On which line you are getting the error ? Please debug and let us know.
♥…ЯҠ…♥ 21-May-13 2:37am    
I would suggest you to debug the code,I believe this is why debugger is meant for in Visual studio.
kumar2413 21-May-13 2:39am    
I think u missed out the connection string.
Smanish87 21-May-13 3:13am    
Yes kumar you are right i missed to declare connection string
Thanks a lot
Smanish87 21-May-13 2:43am    
ds = MOLDBAccess.ExecuteDataSet(cmd); on this line it is giving error

1 solution

Without you telling us exactly which line the error is coming from, it's difficult to be precise, but probably you need to look at the return value from MOLDBAccess.ExecuteDataSet, then at grdupload. They are the only variables which you do not show being declared or initialised before you use them, so one or other of them will be null - but I can't tell which from here.

Run your code in the debugger, and either put a breakpoint on the
DataSet ds = new DataSet();
line and step through, looking at your variables at each step, or run it until the exception occurs and look at the variable it is complaining about.

You will then have to work out why it is null - but that is outside the scope of the code you have shown us.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900