Texpert 0 Newbie Poster

Hi,

can someone help me to code this ?
I have a dynamic datagrid and I want to insert only one column from selected [checkbox] rows in the datagrid into a SQL-Server table.
I've a working code where I call Add_to_DB function within the loop of Datagrid items, which works, but I don't like that, I know I can store the values in an ArrayList and then just make one DB call to insert all the rows in the table. But I am having hard time coding this, I keep on getting null reference error. I know I am doing something stupid.

please help.

TIA

Private Sub NextBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NextBtn.Click
        Dim objItem As DataGridItem
        For Each objItem In SKU_Search_DG.Items
            ' Ignore invalid items
            If objItem.ItemType <> ListItemType.Header And _
                objItem.ItemType <> ListItemType.Footer And _
                objItem.ItemType <> ListItemType.Pager Then
                ' Retrieve the value of the check box
                Dim blnChecked As Boolean
                blnChecked = CType(objItem.Cells(0).FindControl("grid_CheckBox1"), _
                    CheckBox).Checked
                If blnChecked = True Then
                    ' add the selected SKU_Id in the array
                    SKU_ID_Array.Add(objItem.Cells(1).Text.Trim())
                    'add_selectedSKU(objItem.Cells(1).Text.Trim())  --- commented out
                End If
            End If
        Next
        Response.Redirect("ApplyDiscount.aspx")
    End Sub

Dani AI

Generated

— the NullReference you see almost always comes from one of two causes: the collection was never instantiated, or the control lookup returned Nothing. Fix both and use a safe pattern (TryCast + checks) and a stable place to hold the IDs (Session or a DataTable) before redirecting.

Example: collect checked rows into a strongly-typed list, prefer DataKeys or a hidden field for the SKU (cell indexes are fragile), store the list in Session, then redirect.

' Collect checked SKUs and save to session
Protected Sub NextBtn_Click(sender As Object, e As EventArgs) Handles NextBtn.Click
    Dim selectedSKUs As New List(Of String)()

    For Each item As DataGridItem In SKU_Search_DG.Items
        If item.ItemType = ListItemType.Item OrElse item.ItemType = ListItemType.AlternatingItem Then
            Dim chk As CheckBox = TryCast(item.FindControl("chkSelect"), CheckBox)
            If chk IsNot Nothing AndAlso chk.Checked Then
                Dim sku As String = Nothing
                If SKU_Search_DG.DataKeys IsNot Nothing AndAlso SKU_Search_DG.DataKeys.Count > item.ItemIndex Then
                    sku = SKU_Search_DG.DataKeys(item.ItemIndex).ToString()
                Else
                    Dim hf As HiddenField = TryCast(item.FindControl("hidSKU"), HiddenField)
                    If hf IsNot Nothing Then sku = hf.Value
                End If
                If Not String.IsNullOrEmpty(sku) Then selectedSKUs.Add(sku)
            End If
        End If
    Next

    Session("SelectedSKUs") = selectedSKUs
    Response.Redirect("ApplyDiscount.aspx")
End Sub

On ApplyDiscount.aspx retrieve the list and do one DB call. One simple approach: build a DataTable, convert to XML and pass to a stored procedure; safer and far faster than looping inserts.

' ApplyDiscount.aspx.vb - pass SKUs as XML to a stored proc
Dim skus As List(Of String) = TryCast(Session("SelectedSKUs"), List(Of String))
If skus IsNot Nothing AndAlso skus.Count > 0 Then
    Dim dt As New DataTable()
    dt.Columns.Add("SKU", GetType(String))
    For Each s In skus : dt.Rows.Add(s) : Next
    Using sw As New System.IO.StringWriter()
        dt.WriteXml(sw, XmlWriteMode.IgnoreSchema)
        Dim xml = sw.ToString()
        ' Call a parameterized stored proc e.g. usp_InsertSKUsFromXml with @XmlSKUs
    End Using
End If

Quick checklist:

  • New the collection before Add.
  • Use TryCast and check for Nothing after FindControl.
  • Prefer DataKeyField or a HiddenField for the SKU, not cell index.
  • Ensure DataBind ran before reading Items (or keep values in a hidden input).
  • Use parameterized bulk insert (TVP/SqlBulkCopy or XML/stored proc) and wrap DB work in a transaction.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.