• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 768
  • Last Modified:

Passing data between classes: ref DataSet?

(Context:  I'm relatively new to .NET and new to C#)

I have a UI that allows the user to perform a lookup of a record from the database.  Once they select a record from the lookup, I need to display all of the field values for that record on the UI.  The record contains around 45 fields.

I was thinking that I would pass a DataSet by Ref to my data access class which would retrieve the data for the record, fill the DataSet, and then return it back to the UI class to populate fields on the form.

Is this best practice?  Or is there a different, more proper way of handling this type of data retrieval?
0
Steve Endow
Asked:
Steve Endow
  • 3
1 Solution
 
Daniel JungesCommented:
Hi sendow,

all our data access we make with DataSet and for the UI we bind it on a DataGridView.

regards
Junges
0
 
Steve EndowMicrosoft MVP - Dynamics GPAuthor Commented:
Thanks Junges.

I'm giving it a try, but after I call my data access class, the data set object on my form seems to be empty.  Can you spot what I'm doing wrong?

I have done debugging, and see that my data access code is properly retrieving one record, but when the DataSet returns back to my UI procedure, the DataSet has no data tables and appears to be Nothing.


//***********************************************
//My User Interface Code
//***********************************************
//Call GP class to get device data and populate DataSet by ref
Classes.GP GP = new DeviceManagement.Classes.GP();
GP.GetDeviceData(deviceNumber, itemNumber, ref deviceSet);

DataTable deviceTable = deviceSet.Tables["Device"];

//Fill out form fields
uiItemDescription.Text = deviceTable.Columns["ItemDescription"].ToString();
uiCustomerID.Text = deviceTable.Columns["CUSTNMBR"].ToString();
uiCustomerName.Text = deviceTable.Columns["CUSTNAME"].ToString();
                


//***********************************************
//Key portion of my Data Access Class Code
//***********************************************

SqlDataAdapter gpDataAdapter = new SqlDataAdapter(gpQuery, gpConn);
DataSet gpDataSet = new DataSet("Device");

gpDataAdapter.SelectCommand.CommandType = CommandType.Text;
gpDataAdapter.SelectCommand.Parameters.Add("@DEVICE", System.Data.SqlDbType.VarChar, 30).Value = deviceNumber.Trim();
gpDataAdapter.SelectCommand.Parameters.Add("@ITEMNMBR", System.Data.SqlDbType.VarChar, 30).Value = itemNumber.Trim();

gpDataAdapter.TableMappings.Add("Table", "Device");
gpDataAdapter.Fill(gpDataSet);

return;

Open in new window

0
 
Steve EndowMicrosoft MVP - Dynamics GPAuthor Commented:
I forgot to include the declaration of the data set from my UI code.

//***********************************************
//My User Interface Code
//***********************************************
//Create data set to hold device fields
DataSet deviceSet = new DataSet("Device");

try
{

    //Call GP class to get device data and populate DataSet by ref
    Classes.GP GP = new DeviceManagement.Classes.GP();
    GP.GetDeviceData(deviceNumber, itemNumber, ref deviceSet);

    DataTable deviceTable = deviceSet.Tables["Device"];

    //Fill out form fields
    uiDeviceNumber.Text = deviceNumber;
    uiItemNumber.Text = itemNumber;

    uiItemDescription.Text = deviceTable.Columns["ItemDescription"].ToString();
    uiCustomerID.Text = deviceTable.Columns["CUSTNMBR"].ToString();
    uiCustomerName.Text = deviceTable.Columns["CUSTNAME"].ToString();                


//***********************************************
//Key portion of my Data Access Class Code
//***********************************************

SqlDataAdapter gpDataAdapter = new SqlDataAdapter(gpQuery, gpConn);
DataSet gpDataSet = new DataSet("Device");

gpDataAdapter.SelectCommand.CommandType = CommandType.Text;
gpDataAdapter.SelectCommand.Parameters.Add("@DEVICE", System.Data.SqlDbType.VarChar, 30).Value = deviceNumber.Trim();
gpDataAdapter.SelectCommand.Parameters.Add("@ITEMNMBR", System.Data.SqlDbType.VarChar, 30).Value = itemNumber.Trim();

gpDataAdapter.TableMappings.Add("Table", "Device");
gpDataAdapter.Fill(gpDataSet);

return;

Open in new window

0
 
Steve EndowMicrosoft MVP - Dynamics GPAuthor Commented:
I just noticed the error in my code.  I had copied and pasted the data access code from another procedure and forgot to update the dataset name in this new version to use the ref parameter.

Let me make that change and see if that fixes it.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now