Link to home
Start Free TrialLog in
Avatar of Steve Endow
Steve EndowFlag for United States of America

asked on

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?
ASKER CERTIFIED SOLUTION
Avatar of Daniel Junges
Daniel Junges
Flag of Brazil image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Steve Endow

ASKER

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

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

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.