Solved

Fill a datagrid from another class

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
for loop with Set 4 48
create an incrementing variable name AutoHotKey 5 75
Counting documents in a Domino View 3 60
Basic Java Case or If-Else statement... 3 43
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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…

929 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

9 Experts available now in Live!

Get 1:1 Help Now