Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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
Comments
Member 9626721 21-Jan-13 6:39am    
TextAge.Text = dr[8].ToString(); I think This line return error.Check this Line

Hello,

It looks like the SQL statement is not correct, and the code is not correct too...

You wrote:
C#
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:

C#
Select FirstName, LastName, Age, EmailId From Newuser where FirstName ='" + Label1.Text + "'"


and replace
C#
TextAge.Text = dr[8].ToString();
with
C#
TextAge.Text = dr["Age"].ToString();


Assuming there is a column called "Age".

Valery.
 
Share this answer
 
Comments
Kishor Deshpande 21-Jan-13 6:50am    
This answer looks correct :)
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 + "'";
 
Share this answer
 
Hi,


In your Select statement you call only FirstName alone

C#
"select FirstName from Newuser where FirstName ='" + Label1.Text + "'";

C#
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..
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900