Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Fill a datagrid from another class

Posted on 2004-09-15
4
Medium Priority
?
197 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
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 …
Introduction to Processes

650 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