对象引用未设置为对象的实例插入数据库sql数组 [英] object reference not set to an instance of an object Inserting database sql array

查看:69
本文介绍了对象引用未设置为对象的实例插入数据库sql数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在数据库中插入数组文本框?我必须在访问中保存每个新框,并且它应该在不同的行中.在保存数据时,它的错误对象引用未设置为对象的实例

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

推荐答案

addbuyer中,Dim newbox As TextBox无关紧要,这就是错误的原因.

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

您已将所有文本框控件添加到boxes,因此在插入数据库时​​需要遍历.一种方法是通过引用循环并传递每个文本框:

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

这篇关于对象引用未设置为对象的实例插入数据库sql数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆