object reference not set to an instance of an object Inserting database sql array

那年仲夏 提交于 2019-12-26 06:04:33

问题


How can i insert array textbox in database?I have to save each newboxes in access and it should be in different row..It has an error object reference not set to an instance of an object when saving the data

Public Class Form1
    Dim boxes As New List(Of TextBox) 

Dim combo As New List(Of ComboBox)

    Private Sub Addbuttons(buttonCount As Integer)
        Dim newbox As TextBox   Dim newcombo As ComboBox

        For i As Integer = 1 To buttonCount
            newbox = New TextBox
            newbox.Size = New Drawing.Size(575, 35)
            newbox.Location = New Drawing.Point(10, 10 + 35 * (i - 1))
            newbox.Name = "TextBox" & i
            newbox.Text = newbox.Name
            'connect it to a handler, save a reference to the array and add it to the form controls
            boxes.Add(newbox)
            Me.Controls.Add(newbox)
        Next  For i As Integer = 1 To buttonCount
        newcombo = New ComboBox
        newcombo.Size = New Drawing.Size(57, 20)
        newcombo.Location = New Drawing.Point(864, 531 + 70 * (i - 1))
        combo.Add(newcombo)
        Me.Controls.Add(newcombo)
    Next
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Addbuttons(Val(TextBox1.Text))
    End Sub
    Private Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        addbuyer()
    End Sub
   Private Sub addbuyer()
    Dim newbox As TextBox
      Try
        datab = " Insert INTO sample (sample1,sample2) values ( '" & newbox.Text & "','" & newqty.Text & "')"
        connDB()
        cmd = New OleDbCommand(datab, conn)
        Dim i As Integer
        i = cmd.ExecuteNonQuery
        If i > 0 Then
            '  MsgBox("Added SUccesfully", MsgBoxStyle.Information, "Confirmation")


        Else
            MsgBox("Failed Adding", MsgBoxStyle.Information, "Alert!")
        End If
    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
        cmd.Dispose()
        conn.Close()

    End Try
End Sub

End Class

回答1:


In addbuyer, Dim newbox As TextBox is nothing and that's the cause of the error.

You added all the textbox controls to boxes so you need to loop thru that when you insert into the DB. One way to do it is by looping and passing each textbox by reference:

Private Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
  For Each t As TextBox In boxes
      addbuyer(t)
  Next
End Sub

Private Sub addbuyer(ByRef newbox As TextBox)       
    Try
        datab = " Insert INTO sample (sample1) values ( '" & newbox.Text & "')"
        connDB()
        cmd = New OleDbCommand(datab, conn)
        Dim i As Integer
        i = cmd.ExecuteNonQuery
        If i > 0 Then
            MsgBox("Added SUccesfully", MsgBoxStyle.Information, "Confirmation")


        Else
            MsgBox("Failed Adding", MsgBoxStyle.Information, "Alert!")
        End If
    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
        cmd.Dispose()
        conn.Close()

    End Try
End Sub


来源:https://stackoverflow.com/questions/35642088/object-reference-not-set-to-an-instance-of-an-object-inserting-database-sql-arra

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!