Tarak Bhavsar
Tarak Bhavsar

Reputation: 145

An unhandled exception of tpe 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

Additional information: Index was out of range. Must be non-negative and less than the size of the collection.

The above unhandled exception shows when i run below code :

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

I have only one column with multiple rows in my datagridview. I run above function on doubleclick event of datagridview.

Upvotes: 0

Views: 1864

Answers (1)

Andrew Morton
Andrew Morton

Reputation: 25023

As you have a one-column DGV, you only need to use the .SelectedCells property, and you should check that only one cell is selected, something like this:

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

Please note how I have used CStr and CInt in appropriate places. If you use Option Strict On then it will point out for you where you should make explicit conversions (among other things). Also, I split up some lines into several lines to make it easier to read, and hence easier to edit.

Upvotes: 1

Related Questions