Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
con = new SqlConnection("Data Source=MNISH-PC\\SQLEXPRESS;Initial Catalog=ecommerce;Integrated Security=True");
 
       con.Open();
       string str = "select FirstName from Newuser where FirstName ='" + Label1.Text + "'";
       cmd = new SqlCommand(str, con);
       dr = cmd.ExecuteReader();
       while (dr.Read())
       {
         Textname.Text = dr["FirstName"].ToString();
         TextLastName.Text = dr["LastName"].ToString();
           TextAge.Text = dr[8].ToString();
           TextEmailId.Text = dr["EmailId"].ToString();
         TextLastName.Text = dr["LastName"].ToString();
 

       }
       con.Close();
Posted 21-Jan-13 0:35am
M@anish1.3K
Comments
Member 9626721 at 21-Jan-13 6:39am
   
TextAge.Text = dr[8].ToString(); I think This line return error.Check this Line
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hello,
 
It looks like the SQL statement is not correct, and the code is not correct too...
 
You wrote:
Textname.Text = dr["FirstName"].ToString();
         TextLastName.Text = dr["LastName"].ToString();
           TextAge.Text = dr[8].ToString();
           TextEmailId.Text = dr["EmailId"].ToString();
         TextLastName.Text = dr["LastName"].ToString();
 
This means that the columns FirstName, LastName, EmailId must be fields present in your SQL statement.
And dr[8] means that there are at least 9 elements in your SQL select.
 
As you wrote "select FirstName from Newuser... " that means there is only 1 element in your SQL statement and it won't work as there are plenty of missing data.
 
so a way to solve it is to rework your statement:
 
Select FirstName, LastName, Age, EmailId From Newuser where FirstName ='" + Label1.Text + "'"
 
and replace
TextAge.Text = dr[8].ToString();
with
TextAge.Text = dr["Age"].ToString();
 
Assuming there is a column called "Age".
 
Valery.
  Permalink  
Comments
Kishor Deshpande at 21-Jan-13 6:50am
   
This answer looks correct :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You are trying to access an array element which is at an index greater than the size of the array itself.
 
You dont have an an eighth column in your result set.
Infact, you have just one column FirstName in your query.
 
string str = "select FirstName from Newuser where FirstName ='" + Label1.Text + "'";
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Hi,
 

In your Select statement you call only FirstName alone
 
"select FirstName from Newuser where FirstName ='" + Label1.Text + "'";
   Textname.Text = dr["FirstName"].ToString();
         TextLastName.Text = dr["LastName"].ToString();
           TextAge.Text = dr[8].ToString();
           TextEmailId.Text = dr["EmailId"].ToString();
         TextLastName.Text = dr["LastName"].ToString();
 
why are you calling the highlighted one's...
in your DataReader having one column only. But in your code you call more than one.
 
That's the issue on here..
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 445
1 CHill60 410
2 Sergey Alexandrovich Kryukov 354
3 BillWoodruff 190
4 PIEBALDconsult 160


Advertise | Privacy | Mobile
Web03 | 2.8.141015.1 | Last Updated 21 Jan 2013
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