Gridview Edit - System.NullReferenceException: Object reference not set to an instance of an object.

rwallacej
rwallacej used Ask the Experts™
on
Hi

I have created a gridview which will edit an XML file (the XML file is quite complicated).  

The data binds fine to gridview, code is following for doing this.

HOWEVER when clicking "Edit" I get the error

"System.NullReferenceException: Object reference not set to an instance of an object."

'code to bind to gridview   Public Sub FillGridview(ByVal XPath As String, ByVal GridView As GridView)
            Dim xml As New XmlDataSource
            xml.DataFile ="my file.xml"
            xml.XPath = XPath
            GridView.DataSource = xml
            GridView.DataBind()
End Sub

'code to edit gridview row
Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
            GridView1.EditIndex = e.NewEditIndex

...do some stuff here to get the XPath

            'rebind to data - does same as whatever button user clicked on
XPath=....
            FillGridview(XPath)
    End Sub

the error happens at line GridView.DataBind() of FillGridview

thanks in advance for help!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Seven priceFull Stack

Commented:
Did you add a row edit
Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
        GridView1.EditIndex = e.NewEditIndex
        BindGrid()
    End Sub
 
    Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
        Dim i As Integer = GridView1.Rows(e.RowIndex).DataItemIndex
        Dim strId As String = CType(GridView1.Rows(e.RowIndex).Cells(2).Controls(0), TextBox).Text
        Dim strName As String = CType(GridView1.Rows(e.RowIndex).Cells(3).Controls(0), TextBox).Text
        Dim strTel As String = CType(GridView1.Rows(e.RowIndex).Cells(4).Controls(0), TextBox).Text
 
        GridView1.EditIndex = -1
        BindGrid()
        Dim oDs As DataSet = GridView1.DataSource
 
        oDs.Tables(0).Rows(i).Item(0) = strId
        oDs.Tables(0).Rows(i).Item(1) = strName
        oDs.Tables(0).Rows(i).Item(2) = strTel
 
        oDs.WriteXml(Request.PhysicalApplicationPath + "XMLFile.xml")
        BindGrid()
 
    End Sub

Author

Commented:
yes, I have both of these

the user clicks "Edit" or "View", FillGridview is called, and the error above happens when Binding

the EditItemTemplate IS shown as required.

It has a custom user control on it which should be filled in when in Edit mode by RowUpdating, however it isn't getting to displaying data (I think) due to the exception happening

if they click Update yes the RowUpdating happens

I think the problem is exception in FillGridview, why it is giving this exception
Seven priceFull Stack

Commented:
sounds like something does not exists
Full Stack
Commented:
Let me know if this examples help, sorry its in C# but you should be able to fix it from here. If not I will try something else.

http://forums.asp.net/t/1230594.aspx

Author

Commented:
I'll try tomorrow at 9 BST thanks
Most Valuable Expert 2012
Top Expert 2014

Commented:
Can you show us gridview markup? Do you have a drop down list in edit template?

Author

Commented:
Thanks. I've tried this successfully

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial