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")

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.