Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

vb.net - object reference not set to an instance of an object Inserting database sql array

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...