Gmac
Gmac

Reputation: 169

Adding datatable to dataset creates 2 identical tables in dataset, is there another way to add?

Just an FYI, I know there are different ways to handle this, but I need it to work in this particular way for my project.

I create a DataSet(DS) and a DataTable(DT) from a DataSet I created with the DataSetGenerator in VS2010, I then add a row to the DT with some data. I then try to add the DT (which contains data in a row) to the DS. At this point in Visual Studio when I step into the next line of code, the DataSet Visualizer shows I have a DataSet with 2 identically named tables, HOWEVER, one of them has data and the other does not. This is probably an oversight on my behalf but I can't identify it. Still learning C# here so that doesn't help either. Thanks for any help!

OLD CODE

private string getStorageKey(string strStorageAccount)
    {
        DataSetStorageKeyDetails ds = new DataSetStorageKeyDetails();
        DataSetStorageKeyDetails.StorageKeyDetailsDataTable dt = new DataSetStorageKeyDetails.StorageKeyDetailsDataTable();
        string strStorageKey = "";

        dt.Rows.Add(strStorageAccount);
        ds.Tables.Add(dt);

        DataSet dsOut = ServiceEnclosed.InterfaceService("GetStorageKey", ds);

        DataTable dtr = dsOut.Tables[0];
        DataSetStorageKeyDetails.StorageKeyDetailsRow dr = dt.First();
        strStorageKey = dr.StorageName;            

        return strStorageKey;
    }

NEW CODE

private string getStorageKey(string strStorageAccount)
    {
        DataSetStorageKeyDetails ds = new DataSetStorageKeyDetails();
        string strStorageKey = "";

        ds.StorageKeyDetails.Rows.Add(strStorageAccount);
        DataSet dsOut = ServiceEnclosed.InterfaceService("GetStorageKey", ds);

        DataTable dtr = new DataTable();
        dtr = dsOut.Tables[0];
        strStorageKey = dtr.Rows[0]["StorageKey"].ToString();

        return strStorageKey;
    }

Upvotes: 0

Views: 227

Answers (1)

Josh Part
Josh Part

Reputation: 2164

When you create a DataSet with the DataSetGenerator, the tables you specify are automatically created when the DataSet is created. You don't have to add a new table, just access the one it already has.

private string getStorageKey(string strStorageAccount)
    {
        DataSetStorageKeyDetails ds = new DataSetStorageKeyDetails();
        string strStorageKey = "";

        ds.StorageKeyDetails.Rows.Add(strStorageAccount);

        DataSet dsOut = ServiceEnclosed.InterfaceService("GetStorageKey", ds);

        DataTable dtr = dsOut.Tables[0];
        strStorageKey = ds.StorageKeyDetails.Rows[0].StorageName;            

        return strStorageKey;
    }

Untested code, but I think it points you to te right direction.

Upvotes: 1

Related Questions