参数超出范围参数名称索引

时间:2019-07-12 10:55:15

标签: sql-server vb.net textbox arguments flowlayoutpanel

因为我具有自动生成的文本框的代码

Insert data from auto generated textbox to SQL Server database

我正在尝试编写代码以确保FlowLayoutPanel1中自动生成的文本框包含数据

自动生成的文本框的生成取决于实部的 Val(Label2.Text)中的数字

,如果任何文本框为null或为空,则该过程将停止直到填充所有字段

如果 Val(Label2.Text),则自动生成的文本框将是这样的 等于 3

enter image description here

所以我可以在保存按钮

中尝试
  Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click


    For k As Integer = 1 To Val(Label2.Text)

            If String.IsNullOrEmpty(FlowLayoutPanel1.Controls(i).Text) Then
            MsgBox("Error , fill all text box")

                Return
            Else
                UpdateUsers()
                i += 1

            End If


    Next



    MsgBox("Done , add all data to database ")
    Button3.Enabled = False
End Sub

所以我在离开一些文本框并再次填充它后出现错误

enter image description here

所有形式的代码都是

Imports System.Data.SqlClient
'library for create folders
Imports System.IO

Public Class part

    Dim cLeft As Integer = 1
    Dim top1 As Integer = 5
    Dim i As Integer = 0
    Dim path1 As String

    Dim cmd As SqlCommand



    Public Sub AddNewTextBox()
        Dim txt As New System.Windows.Forms.TextBox()

        txt.Top = cLeft * 30
        txt.Left = 100
        'txt.Text = "TextBox " & Me.cLeft.ToString
        cLeft = cLeft + 1
        txt.ForeColor = Color.White
        txt.BackColor = Color.Gray
        txt.Font = New Font("Arial", 14.0, FontStyle.Regular)
        txt.Size = New Size(350, 31)
        txt.Location = New Point(156, 130 + top1)
        txt.TextAlign = HorizontalAlignment.Center

        FlowLayoutPanel1.Controls.Add(txt)
    End Sub



    Private Sub part_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'college part number
        Label2.Visible = False
        'college path folder
        Label3.Visible = False


        For m As Integer = 1 To Val(Label2.Text)
            AddNewTextBox()
            top1 = top1 + 35
        Next

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()

    End Sub

    Private Sub UpdateUsers()
        Using cn As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Hazim M\Desktop\stud_project\stud_project\Database1.mdf;Integrated Security=True;User Instance=True")
            Using cmd As New SqlCommand("INSERT INTO college_part ([name],[coll_of_part],[part_path]) Values (@name,@coll_of_part,@part_path);", cn)
                If i < Val(Label2.Text) Then
                    path1 = Label3.Text & "\" & FlowLayoutPanel1.Controls(i).Text.Trim & Date.Now.Year
                    cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = FlowLayoutPanel1.Controls(i).Text
                    cmd.Parameters.Add("@coll_of_part", SqlDbType.NVarChar).Value = Label1.Text
                    cmd.Parameters.Add("@part_path", SqlDbType.NVarChar).Value = path1
                    cn.Open()
                    cmd.ExecuteNonQuery()
                    cn.Close()

                    If Not Directory.Exists(path1) Then
                        Directory.CreateDirectory(path1)
                    Else
                        MsgBox("folder is existing")
                    End If

                End If

            End Using
        End Using
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click


        For k As Integer = 1 To Val(Label2.Text)

                If String.IsNullOrEmpty(FlowLayoutPanel1.Controls(i).Text) Then
                MsgBox("Error , fill all text box")

                    Return
                Else
                    UpdateUsers()
                    i += 1

                End If


        Next



        MsgBox("Done , add all data to database ")
        Button3.Enabled = False
    End Sub

End Class

谢谢你

1 个答案:

答案 0 :(得分:0)

我认为您需要在Button3_click方法中将i Integer = 0的dim移至局部变量。将i传递给UpdateUser调用(将参数添加到UpdateUser)。当前的全局i会一直加1,最终超出范围。