Click here to Skip to main content
11,714,776 members (75,321 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: array
cmd = new SqlCommand("select RefferBy from UsersIncome where UserName='" + Label1.Text + "'", con);
 
    dtr = cmd.ExecuteReader();
        if (dtr.Read())
    {
       Label2.Text = dtr[0].ToString();
       Label3.Text = dtr[1].ToString();
        Label4.Text = dtr[2].ToString();
       Label5.Text = dtr[3].ToString();
    }


it is showing Index was outside the bounds of the array why ?????
Posted 24-Dec-11 8:05am
Pr!y@1.2K
Edited 24-Dec-11 8:18am
v2
Comments
Richard MacCutchan at 24-Dec-11 14:31pm
   
Are you sure that dtr contains 4 elements?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Because dtr doesn't have enough columns to support your code.
  Permalink  
Comments
Monjurul Habib at 24-Dec-11 14:39pm
   
excellent,5!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

cmd = new SqlCommand("select RefferBy from UsersIncome where UserName='" + Label1.Text + "'", con);
 
    dtr = cmd.ExecuteReader();
        if (dtr.Read())
    {
       Label2.Text = dtr[0].ToString();
       /*Label3.Text = dtr[1].ToString();
        Label4.Text = dtr[2].ToString();
       Label5.Text = dtr[3].ToString();*/
    }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Check out SQL Tutorial[^] for basic understanding of SQL syntax.

The problem with your code is, that there is only 1 column in your SELECT statement. "RefferBy".

If you want to read all rows, you have to loop the DataReader.
Check out Retrieving Data Using a DataReader[^]

Hope it can help you.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 570
1 Richard MacCutchan 275
2 Maciej Los 274
3 OriginalGriff 140
4 CHill60 135
0 Sergey Alexandrovich Kryukov 545
1 CHill60 375
2 Maciej Los 324
3 Richard MacCutchan 310
4 OriginalGriff 260


Advertise | Privacy | Mobile
Web01 | 2.8.150819.1 | Last Updated 24 Dec 2011
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