vb.net : There is no row at position 1.

The following line of code in the attached function crashes with the error "There is no row at position 1"
TextBox_Sent.Text= dt.Rows[1]["Sent"].ToString();

I do not understand this since the value of dt.rows[0] has a value and the datatable gets populated with 1 row....

Any ideas..why this crashes?

Thanks
private void SetAgreement()
    {
        DataTable dt = GetProviderAssignmentAggreementData(AssignmentId);
 
        if (dt.Rows.Count == 0)
        {
            hdn_AgreementFiledId.Value = TextBox_Sent.Text = TextBox_Received.Text = string.Empty;
 
        }
        else
        {
          hdn_AgreementFiledId.Value = dt.Rows[0]["AgreementFiledID"].ToString();
          TextBox_Sent.Text= dt.Rows[1]["Sent"].ToString();
          TextBox_Received.Text = dt.Rows[2]["Received"].ToString();
        }
    }

Open in new window

LVL 11
Robb HillSenior .Net DeveloperAsked:
Who is Participating?
 
samtran0331Connect With a Mentor Commented:
>>understand this since the value of dt.rows[0] has a value and the datatable gets populated with 1 row....

rows[1]

would actually be the second row of data since the index starts at zero...if your datatable is populated with 1 row, then there is only rows[0] available to reference...
...so when your code steps into the "else"...you're certain you have 3 rows of data?


0
 
Robb HillSenior .Net DeveloperAuthor Commented:
duh..thanks..idiot moment:)
0
 
ororioleCommented:
The datatable index starts at zero. So the first row goes into Rows[0]. You said you are only getting one row so there is no row to put into Row[1], which would be the second row.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.