Solved

ListView Control Help!

Posted on 2011-09-26
10
294 Views
Last Modified: 2012-08-14
Hello Experts,

I have two ListView Controls. The first ListView Control is usded to retrieve all Course Names in my DB. The second ListView Control is needed to view the Master / Detail information for the Selected Course Names.

The problem I'm having is getting the Master / Detail information for the second ListView Control.

I'm using ASP.NET 4, ADO.NET for retrieving the data from DB and C# as the programming language. Below is my current setup.

STEP 1: Retrieve data to first ListView Control (this works)
STEP 2: Display Master / Detail information from selected value from STEP 1 (need help with this step).



HTML:
<asp:ListView ID="lvCourses" GroupItemCount="2" runat="server">
                    <GroupTemplate>
                        <div class="listview">
                            <asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
                        </div>
                    </GroupTemplate>
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" Text='<%# Eval("ghaco_name") %>' CssClass="label"
                            Width="280px" runat="server"></asp:LinkButton>
                    </ItemTemplate>
                    <AlternatingItemTemplate>
                        <asp:LinkButton ID="LinkButton1" Text='<%# Eval("ghaco_name") %>' CssClass="label"
                            Width="280px" runat="server"></asp:LinkButton>
                        <%--<asp:Label ID="Label2" Text='<%# Eval("ghaco_name") %>' CssClass="label" runat="server" Width="280px"></asp:Label>--%>
                    </AlternatingItemTemplate>
                    <SelectedItemTemplate>                    
                        <asp:HyperLink ID="ProductLink" runat="server" Text='<% #Eval("Name")%>' NavigateUrl='<%#"index.aspx?ghaoc_id=" + Eval("ghaoc_id") %>' />
                    </SelectedItemTemplate>
                </asp:ListView>
                <br />
                <asp:ListView ID="lvCoursesDesc" runat="server">
                    <ItemTemplate>
                        <asp:Label ID="Label4" Text='<%# Eval("ghaco_desc") %>' CssClass="label" runat="server"
                            Width="500px"></asp:Label>
                    </ItemTemplate>
                </asp:ListView>

Open in new window



CODEBEHIND:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;

public partial class programinfo_ghap_index : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        RetrieveCourses();
    }

    protected void RetrieveCourses()
    {
        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["HealthCourses"].ConnectionString))
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "HealthCourses_RetrieveCourses";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = conn;

            try
            {
                conn.Open();

                SqlDataReader rdr = cmd.ExecuteReader();

                lvCourses.DataSource = rdr;
                lvCourses.DataBind();
            }

            catch (Exception ex)
            {
                ex.Message.ToString();
            }

            finally
            {
                conn.Close();
            }
        }
    }

    protected void RetrieveCoursesDesc()
    {
        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["HealthCourses"].ConnectionString))
        {
            int ghaoc_id = Convert.ToInt32(Request.QueryString["ghaoc_id"]);

            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "HealthCourses_RetrieveCoursesDesc";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = conn;

            cmd.Parameters.AddWithValue("@ghaoc_id", SqlDbType.Int).Value = ghaoc_id;

            try
            {
                conn.Open();

                SqlDataReader rdr = cmd.ExecuteReader();

                lvCoursesDesc.DataSource = rdr;
                lvCoursesDesc.DataBind();
            }

            catch (Exception ex)
            {
                ex.Message.ToString();
            }

            finally
            {
                conn.Close();
            }
        }
    }

    protected void LinkButton1_Click(object sender, CommandEventArgs e)
    {
        RetrieveCoursesDesc();
    }
}

Open in new window

0
Comment
Question by:asp_net2
  • 5
  • 2
10 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 36598402

try this

  protected void RetrieveCoursesDesc()
    {

if (Request.QueryString["ghaoc_id"] != "")
{
        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["HealthCourses"].ConnectionString))
        {
           

 int ghaoc_id = Convert.ToInt32(Request.QueryString["ghaoc_id"]);

            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "HealthCourses_RetrieveCoursesDesc";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = conn;

            cmd.Parameters.AddWithValue("@ghaoc_id", SqlDbType.Int).Value = ghaoc_id;

            try
            {
                conn.Open();

                SqlDataReader rdr = cmd.ExecuteReader();

                lvCoursesDesc.DataSource = rdr;
                lvCoursesDesc.DataBind();
            }

            catch (Exception ex)
            {
                ex.Message.ToString();
            }

            finally
            {
                conn.Close();
            }
        }
    }
}
0
 
LVL 4

Author Comment

by:asp_net2
ID: 36598875
Hi pratima_mcs,

That did not work :(

I don't get any error messages when running the code,  I just don't get any results to appear in the ListView Control "lvCoursesDesc" after clicking on a link from the returned data in the ListView Control "lvCourses".

Could it have something to do with the way I have the links setup within the ListView Control "lvCourses" in regards to passing the parameter "ghaoc_id" in the link to the other ListView Control "lvCoursesDesc"?

I tested my Stored Procedures and they all work fine.
0
 
LVL 4

Author Comment

by:asp_net2
ID: 36600978
Hi pratima_mcs,

Are you still able to help with this post?
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 500 total points
ID: 36702803
check on this

 int ghaoc_id = Convert.ToInt32(Request.QueryString["ghaoc_id"]);

whether you are getting Request.QueryString["ghaoc_id"]
0
 
LVL 4

Author Comment

by:asp_net2
ID: 36707475
Ok, how does the rest of the code/markup look to you?
0
 
LVL 4

Author Comment

by:asp_net2
ID: 36707529
@pratima_mcs:

Ok, I set a breakpoint on int ghaoc_id = Convert.ToInt32(Request.QueryString["ghaoc_id"]); and it appears that the line never gets touched which is why I'm not getting anything returned based on the ghaoc_id value.

Now, the part that I need help with. I need to click on value in first ListView and then see the Master/Details of that value in the second ListView Control. In order for me to do that though I need to pass the value (ghaoc_id) from the value selected in the first ListView to the second ListView correct? If so, what am I doing wrong??
0
 
LVL 4

Author Closing Comment

by:asp_net2
ID: 36718752
I was not calling the Event in Page_Load. Once I did that it worked.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now