Click here to Skip to main content
11,496,146 members (677 online)
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
See more: ASP.NET C#4.0
Hi Guyz,

SqlCommand cmd = new SqlCommand("select itemname from newitem where itemid = '" + DropDownList4.Text + "'", con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read() == true)
{
    string a = dr["itemname"].ToString();
 
    if (porder.Rows[i] != null)
    {
        porder.Rows[i].Cells[0].Text = a.ToString();
        porder.Rows[i].Cells[2].Text = price();  
    }
}
porder.DataSource = dr;
porder.DataBind();   
when i run the program i got error i row[i] index, Index was out of range. Must be non-negative and less than the size of the collection.
Posted 29-Apr-13 4:21am
Coolsix460
Edited 29-Apr-13 4:27am
v2
Comments
Prasad Khandekar at 29-Apr-13 9:29am
   
Isn't the error is obvious. Value of i is less than 0. Your code does not tells us how i is getting initialized. Same is the case of porder. If you are binding to pOrder why do you think porder.Rows should return you a valid collection prior to the binding.
ryanb31 at 29-Apr-13 9:50am
   
Where are you stuck?
Rohan Leuva at 30-Apr-13 0:25am
   
@ryanb31:He has clearly mentioned the error messege and the code snippet as well then there is no way to ask "Where are you stuck?".the error msg itself points out where he is stuck.
chien.vh at 4-May-13 13:08pm
   
Try to add the below code before you do the while loop.
if (dr.HasRows)
{
while (dr.Read() == true)
.........................
}

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

  Print Answers RSS
0 RyanDev 230
1 Sergey Alexandrovich Kryukov 114
2 Richard Deeming 100
3 OriginalGriff 89
4 Mostafa Asaduzzaman 59
0 Sergey Alexandrovich Kryukov 10,401
1 OriginalGriff 8,910
2 Sascha Lefèvre 3,899
3 Maciej Los 3,422
4 Richard Deeming 2,600


Advertise | Privacy | Mobile
Web01 | 2.8.150520.1 | Last Updated 29 Apr 2013
Copyright © CodeProject, 1999-2015
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