con.Open()
        cmd.Connection = con
        dataGrid.SelectedIndex = dataGrid.CurrentItem(0)
        MessageBox.Show("User Has Selected " & dataGrid.SelectedIndex)
        Dim id As Integer = dataGrid.SelectedIndex(0).cells("Appid").value.ToString()
        Dim fname As String = dataGrid.SelectedRows(0).cells("Fname").value.ToString()
        Dim mname As String = dataGrid.SelectedRows(0).cells("Mname").value.ToString()
        Dim lname As String = dataGrid.SelectedRows(0).cells("Lname").value.ToString()
        Dim fthname As String = dataGrid.SelectedRows(0).cells("Fthname").value.ToString()
        Dim mthname As String = dataGrid.SelectedRows(0).cells("Mthname").value.ToString()
        Dim occp As String = dataGrid.SelectedRows(0).cells("Occupation").value.ToString()
        Dim dob As Date = dataGrid.SelectedRows(0).cells("DOB").value.ToString()
        Dim mbno As String = dataGrid.SelectedRows(0).cells("Mbno").value.ToString()
        Dim emid As String = dataGrid.SelectedRows(0).cells("Emailid").value.ToString()
        Dim addr As String = dataGrid.SelectedRows(0).cells("Address").value.ToString()
        Dim nat As String = dataGrid.SelectedRows(0).cells("Nationality").value.ToString()

Dani AI

Generated

Short answer: the error almost always comes from using the wrong API or indexing a selection that does not exist. is trying to show fields from the selected row, but MessageBox.Show is a WinForms call and the WebForms GridView uses a different pattern. Also, trying to read a cell when no row is selected (or the cell is DBNull) throws an error — check selection and nulls first.

A safe WinForms (DataGridView) pattern — check selection, guard for DBNull, then build the message:

If dataGridView1.SelectedRows.Count > 0 Then
    Dim r As DataGridViewRow = dataGridView1.SelectedRows(0)
    Dim id As Integer = 0
    Integer.TryParse(Convert.ToString(r.Cells(0).Value), id)
    Dim phone As String = If(r.Cells(3).Value IsNot Nothing, r.Cells(3).Value.ToString(), String.Empty)
    MessageBox.Show(String.Format("ID: {0}, Phone: {1}", id, phone))
End If

If this is ASP.NET GridView, do not use MessageBox.Show. Use server-side SelectedIndexChanged and either DataKeys (recommended for IDs) or the selected row cells, then emit a client alert or update a label:

Protected Sub GridView1_SelectedIndexChanged(...) Handles GridView1.SelectedIndexChanged
    Dim id = GridView1.DataKeys(GridView1.SelectedIndex).Value.ToString()
    Dim name = GridView1.SelectedRow.Cells(2).Text
    ClientScript.RegisterStartupScript(Me.GetType(), "sel", "alert('Selected: " & id & " - " & name & "');", True)
End Sub

Troubleshooting checklist: confirm whether this is WinForms or WebForms; check SelectedRows.Count or GridView.SelectedIndex before accessing cells; handle DBNull/Null values; prefer DataKeys for primary keys; and ensure column indexes/names match the grid.

            This is the Message Box that should contains the fields of the selected row, but it gives the error on datagrid.selectedRows(0).cells("Mbno").value.ToString()
            MessageBox.Show(id & "," & fname & "," & mname & "," & lname & "," & fthname & "," & mthname & "," & occp & "," & dob & "," & mbno & "," & emid & "," & addr & "," & nat)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.