Solved

Fill a datagrid from another class

Posted on 2004-09-15
4
192 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is an explanation of a simple data model to help parse a JSON feed
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.

685 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