Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB Visual-Studio VB.NET
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
    Prjda.Fill(Prjdt)
    Project_IDTextBox.Text = Prjdt.Rows(0).Item("Trade")
Posted 18-Nov-12 2:08am
Rate this: bad
good
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.
  Permalink  
Comments
armarzook at 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 at 18-Nov-12 9:01am
   
And do you get a "1" when you get the error? I don't think you do...
armarzook at 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 at 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 at 18-Nov-12 9:42am
   
So. Whats the solution???
OriginalGriff at 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
good
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
  Permalink  
v2

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



Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 20 Nov 2012
Copyright © CodeProject, 1999-2014
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