Reputation: 275
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
Upvotes: 0
Views: 4004
Reputation: 363
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();
}
Upvotes: 0