Click here to Skip to main content
13,447,449 members (41,474 online)
Rate this:
Please Sign up or sign in to vote.
Dear Coders,

Please Help,

The code is below. When executing it works fine. but when exiting the program the Following Error Occurs. "There is no row at position 0"
Please Help.

Private Sub Mat_Sub_NoComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Mat_Sub_NoComboBox.SelectedIndexChanged

    TextBox1.Text = Mat_Sub_NoComboBox.SelectedValue

    con.ConnectionString = myconstring

    'Fetch Project_id to textbox
    Dim Prjda As New SqlDataAdapter("SELECT trade FROM Mat_e1_prj where mat_sub_no='" + Mat_Sub_NoComboBox.SelectedValue + "'", con)
    Dim Prjdt As New DataTable
    Project_IDTextBox.Text = Prjdt.Rows(0).Item("Trade")
Posted 18-Nov-12 2:08am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

As I said to your last question: your query returns no rows.
You need to check the DataTable.Rows.Count property before you try to access the returned values.
armarzook 18-Nov-12 8:58am
Already tried it by passing the code into a msgbox
the actual output is 1. This is why iam confused.
OriginalGriff 18-Nov-12 9:01am
And do you get a "1" when you get the error? I don't think you do...
armarzook 18-Nov-12 9:07am


and i actually get the error when exiting the program.
OriginalGriff 18-Nov-12 9:16am
Yes - and looking at your code, you show the message box after you have accessed the row and it has excepted (see your pic 3)
That means that there are no rows - which means that the SELECT is returning none, which means that there are no rows where mat_sub_no in the DB matches teh current contents of Mat_Sub_NoComboBox.SelectedValue
So, when you get the exception - use the debugger to look at the other variables.
What is the value of Mat_Sub_NoComboBox.SelectedValue? Just hover your mouse over it and it will tell you...And I'm just guessing "Nothing" here! :laugh:

armarzook 18-Nov-12 9:42am
So. Whats the solution???
OriginalGriff 18-Nov-12 9:49am
Check first.
Check your SelectedValue is ok.
Check your Rows.Count.
Do not try to get a value from a row if it isn't going to exist!
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Just Added then it worked... Anyways thanks for your support coders.

If dt.rows.count > 0 then
Project_IDTextBox.Text = Prjdt.Rows(0).Item("Trade")
End If

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.180318.3 | Last Updated 20 Nov 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100