0

I am trying to retrieve column values from database here is my code

protected void LoadProfile()
{
 conn = new SqlConnection(connString);
 cmdProfile = new SqlCommand("SELECT Name, Father, Gender, UserName, City, Country, Department, Year, Degree, JobTittle, Organization, JobCity, JobCountry, JobTittle FROM UserProfile WHERE UserName=@UserName", conn);
 cmdProfile.Parameters.AddWithValue("@UserName", userName);
 conn.Open();
 reader = cmdProfile.ExecuteReader();
 if (reader.Read())
 {
 labelName.Text = reader["Name"].ToString();
 txtBoxFather.Text = reader["Father"].ToString();
 TextBoxGender.Text = reader["Gender"].ToString();
 TextBoxAge.Text = "";
 TextBoxCity.Text = reader["City"].ToString();
 TextBoxCountry.Text = reader["Country"].ToString();
 TextBoxDepartment.Text = reader["Department"].ToString();
 TextBoxDegree.Text = reader["Degree"].ToString();
 TextBoxYear.Text = reader["Year"].ToString();
 TextBoxJobTittle.Text = reader["JobTittle"].ToString();
 TextBoxJobCity.Text = reader["JobCity"].ToString();
 TextBoxJobCountry.Text = reader["JobCountry"].ToString();
 TextBoxOrganization.Text = reader["Organization"].ToString();
 }
 conn.Close();
 } 

But it is not retrieving anything from database.

Actually I am taking userName parameter as query string from different page using this line

 userName = Request.QueryString["Name"].ToString();

When I put break points the control is not going forward after this line

if (reader.Read())

This is the page from where I am taking query string. .

<asp:GridView ID="GridAllAlumni" runat="server" 
 onitemcommand="GridAllAlumni_ItemCommand">
 <Columns>
 <asp:TemplateField>
 <ItemTemplate>
 <asp:LinkButton ID="lnkname" runat="server"
 Text='<%#Eval("Name") %>'
 PostBackUrl='<%#"~/Profile/Profile.aspx?Name="+Eval("Name") %>'/>
 </ItemTemplate>
 </asp:TemplateField>
 </Columns>
</asp:GridView>

Where am I getting wrong?

Your help will be appreciated. Thanx

marc_s
759k185 gold badges1.4k silver badges1.5k bronze badges
asked Jun 24, 2013 at 19:34
13
  • What error? Post the stacktrace / exception. Commented Jun 24, 2013 at 19:36
  • try if(reader.HasRows()) -> reader.Read() Commented Jun 24, 2013 at 19:37
  • 1
    Does the query work if you execute it directly at the DB server? is this possibly simply because the query returns zero rows? perhaps due to case sensitivity on the UserName=@UserName check? Is userName what you expect it to be? Commented Jun 24, 2013 at 19:38
  • 1
    btw, it won't stop it working, but it would be a really good idea to a: separate the UI and data access, and b: use using aggressively (all of conn, cmdProfile and reader are IDisposable, and should make use of using blocks) Commented Jun 24, 2013 at 19:39
  • 1
    I think your end goal is unclear Commented Jun 24, 2013 at 19:50

2 Answers 2

1

It is while(reader.Read()) It is not looping.

answered Jun 24, 2013 at 19:45
0
0

Try this. I basically changed @UserName to :UserName in the query and removed the @ in the cmdProfile.Parameters.AddWithValue("@UserName", userName);. It worked for me. But, I may be wrong. So, feel free to correct me.

protected void LoadProfile()
{
 conn = new SqlConnection(connString);
 cmdProfile = new SqlCommand("SELECT Name, Father, Gender, UserName, City, Country, Department, Year, Degree, JobTittle, Organization, JobCity, JobCountry, JobTittle FROM UserProfile WHERE UserName=:UserName", conn);
 cmdProfile.Parameters.AddWithValue("UserName", userName);
 conn.Open();
 reader = cmdProfile.ExecuteReader();
 if (reader.Read())
 {
 labelName.Text = reader["Name"].ToString();
 txtBoxFather.Text = reader["Father"].ToString();
 TextBoxGender.Text = reader["Gender"].ToString();
 TextBoxAge.Text = "";
 TextBoxCity.Text = reader["City"].ToString();
 TextBoxCountry.Text = reader["Country"].ToString();
 TextBoxDepartment.Text = reader["Department"].ToString();
 TextBoxDegree.Text = reader["Degree"].ToString();
 TextBoxYear.Text = reader["Year"].ToString();
 TextBoxJobTittle.Text = reader["JobTittle"].ToString();
 TextBoxJobCity.Text = reader["JobCity"].ToString();
 TextBoxJobCountry.Text = reader["JobCountry"].ToString();
 TextBoxOrganization.Text = reader["Organization"].ToString();
 }
 conn.Close();
 } 
answered Jul 2, 2013 at 19:49

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.