Hi, i am trying to open a form from selected data in an earlier from. I use cust_id = grd_customer(0, current_row).Value to hold the value so that cust_id will open up a from that has a customer's details.

But i keep on getting Unhandled Exception of type 'System.IndexOutOfRangeException' that says, there is no row at position 0. And i dont understand the problem. Can anyone explain this?

command on frm_cust_details - > lbl_cid.Text = run_sql_query("SELECT * FROM TBL_CUSTOMER WHERE CUST_IC = '" & cust_id & "'").Rows(0).Item("CUST_ID")

Dani AI

Generated

As and already hinted, the exception comes from trying to read Rows(0) when there are no rows. Two common root causes beyond "no matching record" are: the value pulled from the grid is empty or the wrong cell/column is being read, and a type/whitespace mismatch (or quoting) causes the WHERE to fail. Short, practical checks and a safe pattern are below.

Confirm the grid value and use the grid API that matches the control (DataGridView vs DataGrid). Prefer column names over numeric indices and trim the value before sending it to the database.

' safe read from a DataGridView (adjust to the actual grid type and column name)
Dim custIC As String = ""
If grd_customer.SelectedRows.Count > 0 Then
    custIC = Convert.ToString(grd_customer.SelectedRows(0).Cells("CUST_IC").Value).Trim()
ElseIf grd_customer.CurrentRow IsNot Nothing Then
    custIC = Convert.ToString(grd_customer.CurrentRow.Cells(0).Value).Trim()
End If

If String.IsNullOrWhiteSpace(custIC) Then
    lbl_cid.Text = ""    ' nothing to query; handle or log as appropriate
    Return
End If

Use a parameterized, minimal SELECT and always check the DataTable row count before accessing Rows(0). Example (SqlClient):

Dim dt As New DataTable()
Using cn As New SqlConnection(connectionString)
    Using cmd As New SqlCommand("SELECT CUST_ID FROM TBL_CUSTOMER WHERE CUST_IC = @ic", cn)
        cmd.Parameters.Add("@ic", SqlDbType.NVarChar, 50).Value = custIC
        Using da As New SqlDataAdapter(cmd)
            da.Fill(dt)
        End Using
    End Using
End Using

If dt.Rows.Count > 0 Then
    lbl_cid.Text = Convert.ToString(dt.Rows(0)("CUST_ID"))
Else
    lbl_cid.Text = ""    ' or show "not found" / log custIC for debugging
End If

Extra tips: run the same SELECT/@ic manually in the DB to confirm results; match parameter type/size to the column; avoid SELECT *; handle DBNull when reading fields; wrap DB calls in try/catch and log the parameter value when a lookup fails. These steps close the loop between the grid value and the database and remove the immediate cause of the IndexOutOfRangeException.

Recommended Answers

All 2 Replies

That error would generally happen because the select statement has returned an empty set, which is why the row count is zero. Double check your sql statement is working as expected by running it directly against the database.

hericles is correct, it is returning an empty set, try to check "cust_id", it might be that the value of the cust_id is not in the database.

And could you do me a favor, please use parameterized query, that could solve many issue, that might be you will encounter in the future.

i will give you an example
rather that using like this

public DataTable run_sql_query(string query)
{
    ///code
}

do it like this

public DataTable run_sql_query(string parameterizedQuery, Dictionary<strin, object> paramters)
{
    ///code
    ///you can iterate on the dictionary
    ///and add the parameter value
}

that is just an example, you can make it better to fit in your preference.

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.