Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: arrays
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 9:05am
Pr!y@1.1K
Edited 24-Dec-11 9: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)



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