Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 33: protected void BindGrid()
Line 34: {
Line 35: string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
Line 36: using (SqlConnection con = new SqlConnection(constr))
Line 37:
Source File: c:\AppServ\www\pmsnet\Default2.aspx.cs Line: 35
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
Default2.BindGrid() in c:\AppServ\www\pmsnet\Default2.aspx.cs:35
Default2.Page_Load(Object sender, EventArgs e) in c:\AppServ\www\pmsnet\Default2.aspx.cs:28
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
System.Web.UI.Control.OnLoad(EventArgs e) +95
System.Web.UI.Control.LoadRecursive() +59
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +678
hye sir. I used your code to retrieve data from database inside a gridview, and fyi i have nested gridview. when I run the code I ve came to this error. May I know the reason. Below I have attached my full code. Thank you
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.IO;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Configuration;
using System.Globalization;
using System.Data.OleDb;
using System.Text.RegularExpressions;
using System.Net.Mail;
public partial class Default2 : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
protected void BindGrid()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM tblScoreCard WHERE kpiA_objective, kpiA_weightage, kpiA_measureid, kpiA_threshold, kpiA_base"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
gvParentGrid.DataSource = dt;
gvParentGrid.DataBind();
}
}
}
}
}
protected void gvUserInfo_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataTable dt = new DataTable();
dt.Columns.Add("Date Time", typeof(string));
dt.Columns.Add("Logged By", typeof(string));
dt.Columns.Add("Comment", typeof(string));
dt.Columns.Add("Status", typeof(string));
dt.Columns.Add("Attachments", typeof(string));
DataRow dr = dt.NewRow();
dr["Date Time"] = "SELECT * FROM tblProgressScoreCard WHERE ph_datecreated";
dr["Logged By"] = "SELECT * FROM tblProgressScoreCard WHERE ph_kpiAid AS ";
dr["Comment"] = "SELECT * FROM tblProgressScoreCard WHERE ph_description ";
dr["Status"] = "SELECT * FROM tblProgressScoreCard WHERE ph_status";
dr["Attachments"] = "SELECT * FROM tblProgressScoreCard WHERE ph_attachment1 AND ph_attachment2";
dt.Rows.Add(dr);
GridView gv = (GridView)e.Row.FindControl("gvChildGrid");
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
if (string.IsNullOrEmpty(dt.Rows[i][j].ToString()))
{
dt.Rows[i][j] = "Data Not Available";
}
}
}
gv.DataSource = dt;
gv.DataBind();
}
}
}