ASP.NET - Error : Object reference not set to an instance of an object
Asked By vimalkumar panneerselvam
22-Nov-10 10:08 AM

Hi am trying to get the records from the gridivew which is checked using checkbox in datatable and passing that data table to a session to take to another page . but when am tryng to execute the code the above mentioned error shows
in the line
dataRow[i] = GridView1.SelectedRow.Cells[i2].Text;
Can anyone help me to sort out thz problem
protected void Button2008_Click(object sender, EventArgs e)
{
try
{
//DataTable dtMain =
//copy the schema of source table
DataTable dtClone = new DataTable();
dtClone.Columns.Add("Email_address");
DataRow dataRow;
dataRow = dtClone.NewRow();
int i2 = 1;
foreach (GridViewRow gv in GridView1.Rows)
{
CheckBox chk = gv.FindControl("chkSelect") as CheckBox;
HiddenField hdValue = gv.FindControl("hdValue") as HiddenField;
if (chk.Checked)
{
for (int i = 0; i < dataRow.Table.Columns.Count; i++)
{
dataRow[i] = GridView1.SelectedRow.Cells[i2].Text;
i2++;
}
dtClone.Rows.Add(dataRow);
}
}
HttpContext.Current.Session["keys"]=dtClone;
}
catch
{
FillClientInGrid();
}
Response.Redirect("About.aspx", true);
}
Danasegarane Arunachalam replied to vimalkumar panneerselvam
In which line you get the error ?
vimalkumar panneerselvam replied to Danasegarane Arunachalam
dataRow[i] = GridView1.SelectedRow.Cells[i2].Text;
Parag Satpute replied to vimalkumar panneerselvam
Look out for the common mistakes because of which the "Object reference not set to an instance of an object" error is generated on the below link,
http://forums.aspfree.com/net-development-11/common-causes-of-object-reference-not-set-to-an-instance-45799.html
Peter Bromberg replied to vimalkumar panneerselvam
Since you are starting out with i2 =1, I suspect that you are simply going farther than the number of cells in the Gridview Row.
dataRow[i] = GridView1.SelectedRow.Cells[i2].Text;
i2++;
If you go into debug/ Exceptions and check "Common Language Runtime Exceptions" the debugger will break at the exact instance in the loop and you can check the values of your variables there.
vimalkumar panneerselvam replied to Peter Bromberg
Hi Peter,
Thx for you suggestion.
Can i hav any sample code for the below requirement.
I have a gridivew with checkbox when the user selects the checkbox the records from the datasource should pass to the datatable and tat datatable should be assigned to a session to take to another webpage .
am using to bind the gridview like thz currently:
GridView1.DataSource = pds;
GridView1.DataBind();
Thanx in advance

Error in Checking checkbox Hi all, I have written Javascript function for checking and unchecking checkbox getElementById(id).checked; } } } radgrid checkbox <HeaderTemplate> <asp:CheckBox runat = "server" ID = "chkSelectall" / > < / HeaderTemplate> <FilterTemplate> <asp:HiddenField ID = "hdnDelete" runat = "server" / > < / FilterTemplate> <ItemTemplate> <asp:HiddenField runat = "server" ID = "hdnStudentHistoryPrintID" / > <asp:CheckBox runat = "server" ID = "chkSelect" / > serverside if (e.Item.ItemType System.Web.UI.Page { List<Person> personList = new List<Person> (); protected void Page_Load( object sender, EventArgs e) { if (!IsPostBack) { Person p = new Person(); personList = p.GetPersonList(); this .gv1.DataSource = personList; this .gv1.DataBind(); } } protected void btn_checked_Click( object sender, EventArgs e) { foreach (GridViewRow row in gv1.Rows) { CheckBox cb = (CheckBox)row.FindControl( "chckbxSel" ); if (cb ! = null && cb.Checked parts including databinding code. In fact, for most of the test purposes I do Create DataTable Programmatically and bind the gridview, formview or detailsview to that DataTable. Follow this link also = - http: / / dotnetspidor.blogspot.com / 2009 / 05 / check-uncheck-all-rows-of
obj.style.backgroundColor = 'WhiteSmoke'; } / / Shows DIV popup commands for gridview function ShowPopup(lbtn1, lbtn2, panel, gridviewRow) { var link1 = document.getElementById(lbtn1); var link2 = document.getElementById(lbtn2); var pnl = document.getElementById(panel); var row = document.getElementById(gridviewRow); pnl.style.display = "block"; / / row.style.backgroundImage = "url(. . / images / td_mouseover.gif)"; if(link1 ! = null) link1 pnl.style.backgroundColor = '#FFFFC0'; } / / Hides DIV popup commands for gridview function HidePopup(lbtn1, lbtn2, panel, gridviewRow) { var link1 = document.getElementById(lbtn1); var link2 = document.getElementById(lbtn2); var pnl = document.getElementById(panel); var row = document.getElementById(gridviewRow); / / row.style.backgroundImage = "url(. . / images / spacer.gif)"; pnl.style.display = "none"; if(link1 ! = null) link1 Xml.Linq; public partial class FullPage : System.Web.UI.MasterPage { protected void Page_Load(object sender, EventArgs e) { / / SessionValue objsessionvalue = new SessionValue(); / / objsessionvalue.sessionValue("UserName", "CI624"); / / Session["SessionValue"] = objsessionvalue; / / / / bind grid if ToString()); if (HstPermission["ACCESS"].ToString() = = "False") { Response.Redirect("PermissionDenied.aspx"); } } protected void lnkLogOut_Click(object sender, EventArgs e) { BasePage objBasePage = new BasePage(); string LoggedUserIn = ""; LoggedUserIn = objBasePage.getvalue("LoggedUserIN").ToString(); if (LoggedUserIn = = "MyApp asp:TemplateField> <asp:TemplateField HeaderText = "Check Contract" ItemStyle-HorizontalAlign = "Center" HeaderStyle-HorizontalAlign = "Center"> <ItemTemplate> <asp:HiddenField ID = "HiddenCheckContact" Value = '<%# Eval("Check_contract_id") %> ' runat = "server" / > <asp:TextBox ID = "TxtCheckContact" Text = '<%# Eval("Check_contract_id") %> ' runat ItemTemplate> <ItemStyle HorizontalAlign = "Center" / > <HeaderStyle HorizontalAlign = "Center" / > < / asp:TemplateField> <asp:TemplateField HeaderText = "Status"> <ItemTemplate> <asp:HiddenField ID = "hdnBLDate" runat = "server" Value = '<%# Eval("BL_DATE") %> ' / > <asp:HiddenField ID = "hdnAPCode" runat = "server" Value = '<%# Eval
early as possible along with a example performed by you. protected void btnCut_Click(object sender, EventArgs e) { Session["BACKUP"] = null; DataSet ds = (DataSet)Session["DataSet"]; Object[][] obj1 = new object[ds.Tables DataSource = ds.Tables[0].DefaultView; GridView1.DataBind(); Session["DataSet"] = ds; } protected void btnPaste_Click(object sender, EventArgs e) { Object[][] obj1 = null; if (Session["BACKUP"] ! = null) obj1 = ( Object[][])Session["BACKUP"]; if (obj1 ! = null row and click on the button then it will have to show in another gridview. DataTable objDt = Gridview1.DataSource as DataTable; DataTable objDtTmp = null; foreach(GridViewRow objGVR in Gridview1.Rows) { CheckBox objChk = (CheckBox) objGVR.FindControl("chk1"); if(objChk) { int iKeyValue = Convert requirement c. Filter the checked rows from the underlying dataset d. Add it to a datatable e. Bind it to the second grid [code snippet] / / copy the schema of main data table DataTable dtClone = dtSourceData.Clone(); foreach (GridViewRow gvr in gridView1.Rows) { CheckBox chk = gvr.FindControl("chkSelect") as
on functionalRolecode filling employees) its my c# code, I want in jquery, please help me GridViewRow gvr = ( GridViewRow )(( Control )sender).NamingContainer; DropDownList ddlTeamForBidPlan = ( DropDownList )gvr.FindControl( "ddlTeamForBidPlan" ); DropDownList ddlEmployeeForBidPlan = ( DropDownList )gvr.FindControl( "ddlEmployeeForBidPlan" ); HiddenField lblSlNo = ( HiddenField )gvr.FindControl( "hidSerialNo" ); DataTable dtf = ViewState[ "dtf" ] as DataTable ; DataTable dtAllFun = ViewState[ "dtAllFun" ] as DataTable ; dtBidTeamp = new DataTable (); dtBidTeamp = dtAllFun.Clone(); IEnumerable < DataRow > drBidTeamp = from Data in dtAllFun.AsEnumerable() where