Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
ERROR/////////////////

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 54:         OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\learningdb\\learningdb.accdb");
Line 55:         conn.Open();
Line 56:         String str = "UPDATE emp SET marks='" + textmarks.Text + "',name='" + textname.Text + "' where rowid=" + lbl.Text + " ";
Line 57:         OleDbCommand cmd = new OleDbCommand(str, conn);
Line 58:         //SqlCommand cmd = new SqlCommand("update emp set marks=" + textmarks.Text + " , name='" + textname.Text + "' where rowid=" + lbl.Text + "", conn);  


Source File: c:\LIBR\n3.aspx.cs    Line: 56

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
   n3.GridView1_RowUpdating(Object sender, GridViewUpdateEventArgs e) in c:\LIBR\n3.aspx.cs:56
   System.Web.UI.WebControls.GridView.OnRowUpdating(GridViewUpdateEventArgs e) +133
   System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation) +776
   System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +837
   System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) +95
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +35
   System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +117
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +35
   System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +115
   System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +132
   System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +177
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1746


Version Information: Microsoft .NET Framework Version:2.0.50727.1433; ASP.NET Version:2.0.50727.1433 

//////////////////////////////////////////////////////////////////////////////////////
C#
using System;
using System.Collections;
using System.Configuration;

using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Data;
using System.Data.OleDb;

public partial class n3 : System.Web.UI.Page
{
    OleDbConnection conn;
    protected void Page_Load(object sender, EventArgs e)
    {
       
        conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\learningdb\\learningdb.accdb");
        if (!IsPostBack)
        {
            bind();
        }


    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        bind();
    }

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        bind();
    }


    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int index = GridView1.EditIndex;
        GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
        Label lbl = (Label)row.FindControl("lblid");
        TextBox textname = (TextBox)row.FindControl("textbox1");
        TextBox textmarks = (TextBox)row.FindControl("textbox2");
        GridView1.EditIndex = -1;
        OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\learningdb\\learningdb.accdb");
        conn.Open();
        String str = "UPDATE emp SET marks='" + textmarks.Text + "',name='" + textname.Text + "' where rowid=" + lbl.Text + " ";
        OleDbCommand cmd = new OleDbCommand(str, conn);
        //SqlCommand cmd = new SqlCommand("update emp set marks=" + textmarks.Text + " , name='" + textname.Text + "' where rowid=" + lbl.Text + "", conn);  
        cmd.ExecuteNonQuery();
        conn.Close();
        bind();
    }

    public void bind()
    {
        conn.Open();
        OleDbDataAdapter da = new OleDbDataAdapter("select * from emp", conn);
        //SqlDataAdapter da = new SqlDataAdapter("select * from emp", conn);  
        DataSet ds = new DataSet();
        da.Fill(ds, "emp");


        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();
        conn.Close();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
        Label lbldeleteID = (Label)row.FindControl("lblid");

        conn.Open();
        OleDbCommand cmd = new OleDbCommand("delete from emp where rowid=" + lbldeleteID.Text + "", conn);
        //SqlCommand cmd = new SqlCommand("delete  emp where rowid=" + lbldeleteID.Text + "", conn);  
        cmd.ExecuteNonQuery();
        conn.Close();
        bind();
    }

    protected void GridView1_PageIndexChanging1(object sender, GridViewPageEventArgs e)
    {

        GridView1.PageIndex = e.NewPageIndex;
        GridView1.EditIndex = -1;
        bind();
    }
    

}
Posted
Updated 17-Feb-12 0:32am
v2
Comments
Eduard Keilholz 17-Feb-12 6:24am    
And yet another SQL Injection victim was born...

On or more of the lines below result in a null :
C#
Label lbl = (Label)row.FindControl("lblid");
TextBox textname = (TextBox)row.FindControl("textbox1");
TextBox textmarks = (TextBox)row.FindControl("textbox2");
 
Share this answer
 
I suspect the problem you are having is because one of the lines

TextBox textname = (TextBox)row.FindControl("textbox1");
TextBox textmarks = (TextBox)row.FindControl("textbox2");


is not finding the control, so textname or textmarks is null (or both of then) hence a null reference exception when you try to use them.

use the debugger with a breakpoint on the

GridView1.EditIndex = -1;


line and examine the textname and textmarks - see if they have a a value or not.
 
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