mscorlib.dll 中发生 tpe 'System.ArgumentOutOfRangeException' 的未处理异常
An unhandled exception of tpe 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
'System.ArgumentOutOfRangeException' 类型的未处理异常发生在 mscorlib.dll
附加信息:索引超出范围。必须为非负数且小于集合的大小。
以上未处理的异常显示在我 运行 下面的代码 :
Private Sub chUser()
conn = New SqlConnection(conStr)
conn.Open()
myConn.clName = clNameDGV.SelectedRows.Item(0).Cells(0).Value //EXCEPTION SHOWS FOR THIS LINE
Dim comStr As String = "Select Count(*) from Login_Detail Where Clinic_Name = '" & clNameDGV.SelectedRows.Item(0).Cells(0).Value & "'"
Dim comm As New SqlCommand(comStr, conn)
Dim i As Integer = comm.ExecuteScalar()
If i = 0 Then
If MessageBox.Show("No User Information found for '" + clNameDGV.SelectedRows.Item(0).Cells(0).Value + "'." + vbNewLine + "Do you want to enter new user details ?", "No Users Found", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = vbYes Then
Dim nf As New CreateUser
nf.TopMost = True
nf.ShowDialog(Me)
End If
End If
Dim nf1 As New LoginForm
nf1.TopMost = True
nf1.ShowDialog(Me)
conn.Close()
End Sub
我的数据网格视图中只有一列多行。我在 datagridview 的双击事件上 运行 以上函数。
因为你有一个 one-column DGV,你只需要使用 .SelectedCells
属性,你应该检查是否只选择了一个单元格,像这样:
Private Sub ChUser()
If clNameDGV.SelectedCells.Count <> 1 Then
' not exactly one row was selected
Exit Sub
End If
Dim clinicName As String = CStr(clNameDGV.SelectedCells.Item(0).Value)
Dim nFoundRecords As Integer
Dim conn As SqlConnection = Nothing
Try
conn = New SqlConnection(conStr)
conn.Open()
myConn.clName = clinicName
Dim sql As String = "SELECT COUNT(*) FROM Login_Detail WHERE Clinic_Name = @ClinicName"
Using cmd As New SqlCommand(sql, conn)
'TODO: Set the .SqlDbType parameter correctly.
'TODO: Add the .Size parameter to match the setting in the database.
cmd.Parameters.Add(New SqlParameter With {.ParameterName = "@ClinicName", .SqlDbType = SqlDbType.NVarChar, .Value = clinicName})
nFoundRecords = CInt(cmd.ExecuteScalar())
End Using
conn.Close()
Finally
If conn IsNot Nothing Then
conn.Dispose()
End If
End Try
If nFoundRecords = 0 Then
Dim message As String = "No User Information found for '" & clinicName & "'." & vbNewLine & "Do you want to enter new user details?"
Dim userChoice As DialogResult = MessageBox.Show(message, "No Users Found", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If userChoice = DialogResult.OK Then
Dim nf As New CreateUser
nf.TopMost = True
nf.ShowDialog(Me)
End If
End If
Dim nf1 As New LoginForm
nf1.TopMost = True
nf1.ShowDialog(Me)
End Sub
请注意我是如何在适当的地方使用 CStr
和 CInt
的。如果您使用 Option Strict On 那么它会为您指出您应该在哪里进行显式转换(除其他外)。另外,我将一些行分成几行,以便于阅读,从而更易于编辑。
'System.ArgumentOutOfRangeException' 类型的未处理异常发生在 mscorlib.dll
附加信息:索引超出范围。必须为非负数且小于集合的大小。
以上未处理的异常显示在我 运行 下面的代码 :
Private Sub chUser()
conn = New SqlConnection(conStr)
conn.Open()
myConn.clName = clNameDGV.SelectedRows.Item(0).Cells(0).Value //EXCEPTION SHOWS FOR THIS LINE
Dim comStr As String = "Select Count(*) from Login_Detail Where Clinic_Name = '" & clNameDGV.SelectedRows.Item(0).Cells(0).Value & "'"
Dim comm As New SqlCommand(comStr, conn)
Dim i As Integer = comm.ExecuteScalar()
If i = 0 Then
If MessageBox.Show("No User Information found for '" + clNameDGV.SelectedRows.Item(0).Cells(0).Value + "'." + vbNewLine + "Do you want to enter new user details ?", "No Users Found", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = vbYes Then
Dim nf As New CreateUser
nf.TopMost = True
nf.ShowDialog(Me)
End If
End If
Dim nf1 As New LoginForm
nf1.TopMost = True
nf1.ShowDialog(Me)
conn.Close()
End Sub
我的数据网格视图中只有一列多行。我在 datagridview 的双击事件上 运行 以上函数。
因为你有一个 one-column DGV,你只需要使用 .SelectedCells
属性,你应该检查是否只选择了一个单元格,像这样:
Private Sub ChUser()
If clNameDGV.SelectedCells.Count <> 1 Then
' not exactly one row was selected
Exit Sub
End If
Dim clinicName As String = CStr(clNameDGV.SelectedCells.Item(0).Value)
Dim nFoundRecords As Integer
Dim conn As SqlConnection = Nothing
Try
conn = New SqlConnection(conStr)
conn.Open()
myConn.clName = clinicName
Dim sql As String = "SELECT COUNT(*) FROM Login_Detail WHERE Clinic_Name = @ClinicName"
Using cmd As New SqlCommand(sql, conn)
'TODO: Set the .SqlDbType parameter correctly.
'TODO: Add the .Size parameter to match the setting in the database.
cmd.Parameters.Add(New SqlParameter With {.ParameterName = "@ClinicName", .SqlDbType = SqlDbType.NVarChar, .Value = clinicName})
nFoundRecords = CInt(cmd.ExecuteScalar())
End Using
conn.Close()
Finally
If conn IsNot Nothing Then
conn.Dispose()
End If
End Try
If nFoundRecords = 0 Then
Dim message As String = "No User Information found for '" & clinicName & "'." & vbNewLine & "Do you want to enter new user details?"
Dim userChoice As DialogResult = MessageBox.Show(message, "No Users Found", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If userChoice = DialogResult.OK Then
Dim nf As New CreateUser
nf.TopMost = True
nf.ShowDialog(Me)
End If
End If
Dim nf1 As New LoginForm
nf1.TopMost = True
nf1.ShowDialog(Me)
End Sub
请注意我是如何在适当的地方使用 CStr
和 CInt
的。如果您使用 Option Strict On 那么它会为您指出您应该在哪里进行显式转换(除其他外)。另外,我将一些行分成几行,以便于阅读,从而更易于编辑。