Solved

Fill a datagrid from another class

Posted on 2004-09-15
4
189 Views
Last Modified: 2010-04-17
Hi Everybody
I hope somebody can show me how to do this please.
I need to use a dataset and not a datareader for this part of my application and I'm using OO techniques for my web application.

I currently have a page_load event that goes off the a class and returns the results of a datareader as follows:-

namespace BMRC3
{
      /// <summary>
      /// Summary description for Scores.
      /// </summary>
      public class Scores : System.Web.UI.Page
      {

                .....

public void Page_Load(object sender, System.EventArgs e)
{                                    
          //Set the title of the page to the member name

      string FName = Request.QueryString["Name"];
      string LastName = Request.QueryString["LastName"];
      string MemberID = Request.QueryString["MemberID"];
      lblName.Text = FName + " " + LastName;

      lblFname.Text = FName;
      lblLname.Text = LastName;
      lblMemberID.Text = MemberID;

      BMRC3.GetScores scores = new BMRC3.GetScores();
      dgScores.DataSource = scores.myScores(LastName);
      dgScores.DataBind();

Here is the class that gets called from the code above:-

public class GetScores
{
      public string LastName;

      public SqlDataReader myScores(string LastName)
      {
      //This section retrieves the scores in the database and returns them
      //to the calling procedure which displays them in the datagrid dgScores
      //This event is called from the scores page_load event

      //Create The Connection String
      SqlConnection myConnection = new SqlConnection(ConfigurationSettings.              AppSettings["ConnectionString"]);

      //Create the SQL command
      SqlCommand command = new SqlCommand("MemberSpecific", myConnection);
      // Mark the Command as a SPROC
      command.CommandType = CommandType.StoredProcedure;

      //Set the input parameters
      SqlParameter parameter1 = new SqlParameter("@Lastname", SqlDbType.NVarChar, 50);
      parameter1.Value = LastName;
      command.Parameters.Add(parameter1);
                        
      myConnection.Open();
      SqlDataReader result = command.ExecuteReader(CommandBehavior.CloseConnection);
      return result;

HERE'S WHAT I NEED...............

I need to do the same sort of thing to fill my datagrid but I need to use a dataset and not a datareader...

COuld somebody please show me how to code this up correctly so the LastName parameter is passed to my stored procedure, a data adapter is filled and then on my calling page, I can bind to my datagrid from the dataset

Thanks heaps
0
Comment
Question by:gjm762
  • 3
4 Comments
 
LVL 3

Expert Comment

by:nhewitt21
ID: 12063523
Hi, i usually do something like this:

<CODE>

// Get data from DB and put into a Dataset, pass dataset back
public static System.Data.DataSet GetScores(string LastName)
{
      DataSet ds = new DataSet();
      SqlConnection oCn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
      SqlCommand oCm = new SqlCommand("MemberSpecific", oCn);
      oCm.CommandType = CommandType.StoredProcedure;

      oCm.Parameters.Add("@Lastname", SqlDbType.VarChar, 50);
      oCm.Parameters["@Lastname"].Value = LastName;

      SqlDataAdapter da = new SqlDataAdapter();
      da.SelectCommand = oCm;  

      try
      {
            da.Fill(ds, "Scores");
            oCn.Open();
            oCm.ExecuteNonQuery() ;
      }
      catch (Exception ex)
      {
            ds.Tables.Add("Error");
      }
      finally
      {
            oCm.Dispose();
            oCn.Dispose();
      }

      return ds;
}

// On your codebehind that has the datagrid
DataSet ds = GetScores("LASTNAME_TO_PASS_GOES_HERE");

if (ds.Tables["Group_Grid"].Rows.Count > 0)
{
      Your_DataGrid.DataSource = ds;
      Your_DataGrid.DataBind();
}

</CODE>

Hope it helps
0
 
LVL 3

Accepted Solution

by:
nhewitt21 earned 50 total points
ID: 12063533
sorry this:
if (ds.Tables["Group_Grid"].Rows.Count > 0)

should be:
if (ds.Tables["Scores"].Rows.Count > 0)
0
 

Author Comment

by:gjm762
ID: 12082808
NHewitt21
Thanks heaps...This is right on the mark and I can use this technique in so mnay other apps.   It's just a matter of being shown once
Thanks again for your help
0
 
LVL 3

Expert Comment

by:nhewitt21
ID: 12082859
No problem, glad it sorted you out.

Its good to share knowledge :)
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

759 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

21 Experts available now in Live!

Get 1:1 Help Now