0

I am not sure what I am doing wrong, I am trying to add a new row to a Gridview and have copied samples of code on this site to replicate but still not working and I am getting the attached Error jpeg, stating:

An exception of type 'System.NullReferenceException' occurred in The Cloud.dll but was not handled in user code" "Additional information: Object reference not set to an instance of an object."

Please help.

Code for the page I have included:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not (Page.IsPostBack) Then
        SetInitialRow()
        QuotaionGrid.Visible = False
        Buttons.Visible = False
        ClientDetails.Visible = False
        txtClientNumber.Enabled = False
        txtName.Enabled = False
        txtSurname.Enabled = False
        Search.Visible = True
    End If

End Sub

Private Sub SetInitialRow()

    Dim dt As New DataTable()

    dt.Columns.Add("RowId", GetType(Integer))
    dt.Columns.Add("Quantity", GetType(String))
    dt.Columns.Add("StockCode", GetType(String))
    dt.Columns.Add("Description", GetType(String))
    dt.Columns.Add("UnitPrice", GetType(String))
    dt.Columns.Add("TotalItemCost", GetType(String))

    Dim dr As DataRow = dt.NewRow()
    dr("RowId") = 1
    dr("Quantity") = String.Empty
    dr("StockCode") = String.Empty
    dr("Description") = String.Empty
    dr("UnitPrice") = String.Empty
    dr("TotalItemCost") = String.Empty

    dt.Rows.Add(dr)
    ViewState("Curtbl") = dt
    grdQuotation.DataSource = dt
    grdQuotation.DataBind()

End Sub

Public Sub AddNewRowToGrid()

    Dim rowIndex As Integer = 0

    If ViewState("Curtbl") IsNot Nothing Then
        Dim dt As DataTable = DirectCast(ViewState("Curtbl"), DataTable)
        Dim drCurrentRow As DataRow = Nothing
        If dt.Rows.Count > 0 Then
            For i As Integer = 1 To dt.Rows.Count
                Dim txtQuantity As TextBox = DirectCast(grdQuotation.Rows(rowIndex).Cells(1).FindControl("txtQuantity"), TextBox)
                Dim txtStockCode As TextBox = DirectCast(grdQuotation.Rows(rowIndex).Cells(2).FindControl("txtStockCode"), TextBox)
                Dim txtDescription As TextBox = DirectCast(grdQuotation.Rows(rowIndex).Cells(3).FindControl("txtDescription"), TextBox)
                Dim txtUnitPrice As TextBox = DirectCast(grdQuotation.Rows(rowIndex).Cells(4).FindControl("txtUnitPrice"), TextBox)
                Dim txtTotalItemCost As TextBox = DirectCast(grdQuotation.Rows(rowIndex).Cells(5).FindControl("txtTotalItemCost"), TextBox)
                drCurrentRow = dt.NewRow()
                drCurrentRow("RowId") = i + 1
                dt.Rows(i - 1)("Quantity") = txtQuantity.Text
                dt.Rows(i - 1)("StockCode") = txtStockCode.Text
                dt.Rows(i - 1)("Description") = txtDescription.Text
                dt.Rows(i - 1)("UnitPrice") = txtUnitPrice.Text
                dt.Rows(i - 1)("TotalItemCost") = txtTotalItemCost.Text
                'dt.Rows(i - 1)("TotalItemCost") = txtTotalItemCost.Text

                rowIndex += 1
            Next
            dt.Rows.Add(drCurrentRow)
            ViewState("Curtbl") = dt
            grdQuotation.DataSource = dt
            grdQuotation.DataBind()
        End If
    Else
        Response.Write("ViewState Value is Null")
    End If
    SetOldData()

End Sub

Private Sub SetOldData()
    Dim rowIndex As Integer = 0
    If ViewState("Curtbl") IsNot Nothing Then
        Dim dt As DataTable = DirectCast(ViewState("Curtbl"), DataTable)
        If dt.Rows.Count > 0 Then
            For i As Integer = 0 To dt.Rows.Count - 1

                Dim txtQuantity As TextBox = DirectCast(grdQuotation.Rows(rowIndex).Cells(1).FindControl("txtQuantity"), TextBox)
                Dim txtStockCode As TextBox = DirectCast(grdQuotation.Rows(rowIndex).Cells(2).FindControl("txtStockCode"), TextBox)
                Dim txtDescription As TextBox = DirectCast(grdQuotation.Rows(rowIndex).Cells(3).FindControl("txtDescription"), TextBox)
                Dim txtUnitPrice As TextBox = DirectCast(grdQuotation.Rows(rowIndex).Cells(4).FindControl("txtUnitPrice"), TextBox)
                Dim txtTotalItemCost As TextBox = DirectCast(grdQuotation.Rows(rowIndex).Cells(5).FindControl("txtTotalItemCost"), TextBox)

                txtQuantity.Text = dt.Rows(i)("Quantity").ToString
                txtStockCode.Text = dt.Rows(i)("StockCode").ToString()
                txtDescription.Text = dt.Rows(i)("Description").ToString()
                txtUnitPrice.Text = dt.Rows(i)("UnitPrice").ToString
                txtTotalItemCost.Text = dt.Rows(i)("TotalItemCost").ToString

                rowIndex += 1
            Next
        End If
    End If
End Sub

Protected Sub cmdAdd_Click(ByVal sender As Object, ByVal e As EventArgs)
    AddNewRowToGrid()
End Sub
Protected Sub grdQuotation_RowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)
    If ViewState("Curtbl") IsNot Nothing Then
        Dim dt As DataTable = DirectCast(ViewState("Curtbl"), DataTable)
        Dim drCurrentRow As DataRow = Nothing
        Dim rowIndex As Integer = Convert.ToInt32(e.RowIndex)
        If dt.Rows.Count > 1 Then
            dt.Rows.Remove(dt.Rows(rowIndex))
            drCurrentRow = dt.NewRow()
            ViewState("Curtbl") = dt
            grdQuotation.DataSource = dt
            grdQuotation.DataBind()

            For i As Integer = 0 To grdQuotation.Rows.Count - 2
                grdQuotation.Rows(i).Cells(0).Text = Convert.ToString(i + 1)
            Next
            SetOldData()
        End If
    End If
End Sub
ivpavici
  • 1,117
  • 2
  • 19
  • 30
Terence
  • 59
  • 2
  • 9
  • Which line does the exception occur on? Here's a guide from debugging null reference exceptions (written for c# but the principal is the same). http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it – FloatingKiwi Oct 13 '16 at 01:50
  • It occurs on this line " dt.Rows(i - 1)("Quantity") = txtQuantity.Text" – Terence Oct 13 '16 at 17:11

0 Answers0