[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Fill a datagrid from another class

Posted on 2004-09-15
4
Medium Priority
?
199 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 200 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
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 …
Starting up a Project

872 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