Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I want to display data from an SQL database to a chart control on my view layer. I want to display data based on an item that I selected and get the corresponding data to display on a chart control. Below is the code that I used but nothing is being displayed and it does not give me any errors.

This is my method in the business logic layer that I use to return the data.
C#
public List<durbanvotes> Get_DBNVotes(string year,string region)
        {
            csDAL objdal = new csDAL();
            List<durbanvotes> objvotes = new List<durbanvotes>();
            List<csparameterlisttype> parlist = new List<csparameterlisttype>();
            IDataReader dr = null;
            parlist.Add(new csParameterListType("@standingyear", SqlDbType.VarChar, year));
            parlist.Add(new csParameterListType("@region", SqlDbType.VarChar, region));
            dr = objdal.executespreturndr("Get_region_latest_Votes", parlist);
            while (dr.Read())
            {
                DurbanVotes objp = new DurbanVotes();
                populate_reader(dr, objp);
                objvotes.Add(objp);
            }

            return objvotes;

        }


  private void populate_reader(System.Data.IDataReader dr, DurbanVotes objp)
        {
            objp.Candidate_Name = dr.GetString(0);
            objp.Total_Votes_perc =Convert .ToString ( dr.GetInt32(1)+" %");
        }       
    }</csparameterlisttype></csparameterlisttype></durbanvotes></durbanvotes></durbanvotes>

This is the code behind my form that I am using to return the data.
C#
protected void btnsearch_Click(object sender, EventArgs e)
      {
          string year = ddyear.SelectedItem.Text .ToString ();
          string region = ddregion.SelectedValue.ToString();
          {
          Business_Logic.DurbanVotes objc = new DurbanVotes();
          Chart1.Titles.Add("Latest Results");
          Chart1 .DataSource = objc.Get_DBNVotes(year, region);
          Chart1.Series[0].XValueMember = "Candidates";
          Chart1.Series[0].YValueMembers = "Votes";
          Chart1.DataBind();
          }
      }
Posted
Updated 30-Sep-11 7:54am
v2
Comments
Tejas Vaishnav 1-Oct-11 2:22am    
Can you show your sp or query through which you select data from database..

and also display sample data which you want to show...

I'm not sure about no error. Why don't you try with try..catch block.
C#
public List<durbanvotes> Get_DBNVotes(string year,string region)
        {
        try {
            csDAL objdal = new csDAL();
            List<durbanvotes> objvotes = new List<durbanvotes>();
            List<csparameterlisttype> parlist = new List<csparameterlisttype>();
            IDataReader dr = null;
            parlist.Add(new csParameterListType("@standingyear", SqlDbType.VarChar, year));
            parlist.Add(new csParameterListType("@region", SqlDbType.VarChar, region));
            dr = objdal.executespreturndr("Get_region_latest_Votes", parlist);
            while (dr.Read())
            {
                DurbanVotes objp = new DurbanVotes();
                populate_reader(dr, objp);
                objvotes.Add(objp);
            }

            return objvotes;
        }
        catch (Exception ex){
        }
        }
 
Share this answer
 
Minor point: that method should be static, as it doesn't use any instance information. Then you could remove the creation of a pointless object in the binding process.

At first glance it looks ok. Make sure that there actually are no errors in Get_DBNVotes, and that it is returning a non-empty list (putting a stop on the 'return objvotes' line should be a good way to achieve that if your IDE supports debugging).

Make sure that the column names you're asking for in the chart are the same as the property names on the object. It looks like they're not: try
Chart1.Series[0].XValueMember = "Candidate_Name";
Chart1.Series[0].YValueMembers = "Total_Votes_perc";


I'd expect that to throw an exception if that were the case, though.
 
Share this answer
 
I'm not sure about no error. Why don't you try with try..catch block.
C#
public List<durbanvotes> Get_DBNVotes(string year,string region)
        {
        try {
            csDAL objdal = new csDAL();
            List<durbanvotes> objvotes = new List<durbanvotes>();
            List<csparameterlisttype> parlist = new List<csparameterlisttype>();
            IDataReader dr = null;
            parlist.Add(new csParameterListType("@standingyear", SqlDbType.VarChar, year));
            parlist.Add(new csParameterListType("@region", SqlDbType.VarChar, region));
            dr = objdal.executespreturndr("Get_region_latest_Votes", parlist);
            while (dr.Read())
            {
                DurbanVotes objp = new DurbanVotes();
                populate_reader(dr, objp);
                objvotes.Add(objp);
            }

            return objvotes;
        }
        catch (Exception ex){
        }
        }
 
Share this answer
 
Comments
BobJanova 3-Oct-11 11:45am    
Just swallowing the exception is BAD PRACTICE.

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