Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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.

VB
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
    Prjda.Fill(Prjdt)
    Project_IDTextBox.Text = Prjdt.Rows(0).Item("Trade")
Posted

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.
 
Share this answer
 
Comments
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    
FYI

1) http://tinypic.com/r/34xrfdg/6
2) http://tinypic.com/r/v48co9/6
3) http://tinypic.com/r/2ccm5oy/6

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???
Just Added then it worked... Anyways thanks for your support coders.

VB
If dt.rows.count > 0 then
Project_IDTextBox.Text = Prjdt.Rows(0).Item("Trade")
End If
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900