Search code examples
c#sqldataset-designer

Syntax for Getting/Setting DataSet Row in Visual Studio


Using Microsoft's How to Guide, I have created a database within a word VSTO project. After this, I used another How to Guide to create a dataset from this database.

Now I am trying to get/set the data in the dataset's tables using the following Syntax from this Microsoft Concepts Page:

// This accesses the CustomerID column in the first row of the Customers table.
string customerIDValue = northwindDataSet.Customers[0].CustomerID;

Using this Syntax, my code is as follows:

string employeeName = _SOI_MasterDatabaseDataSet.JobPositionDataTable[0].EmployeeName;

This code gets an error saying that '_SOI_MasterDatebaseDataSet.JobPositionDataTable' is a type, which is not valid in the given context. (Syntax Error)

Here is a screenshot of my Data Sources as well.

This is the first time I've attempted to use databases/datasets. I'm not sure if this is a syntax error or if I've missed something while setting up the dataset in the Dataset Designer.

Thanks in advance


Solution

  • Looking at more Microsoft documentation, I found out I had to initialize the dataSet and dataTable and initialize a tableAdapter to fill the dataTable:

    _SOI_MasterDatabaseDataSet SOI_MasterDatabaseDataSet = new _SOI_MasterDatabaseDataSet();
    
    SOI_MasterDatabaseDataSet.JobPositionRow newJobPosition = SOI_MasterDatabaseDataSet.JobPosition.NewJobPositionRow();
    
    _SOI_MasterDatabaseDataSetTableAdapters.JobPositionTableAdapter jobPositionTableAdapter = new _SOI_MasterDatabaseDataSetTableAdapters.JobPositionTableAdapter();
    
    jobPositionTableAdapter.Fill(SOI_MasterDatabaseDataSet.JobPosition);
    
    string positionTitle = SOI_MasterDatabaseDataSet.JobPosition[0].PositionTitle;
    string employeeName = SOI_MasterDatabaseDataSet.JobPosition[0].EmployeeName;
    

    Here are the additional sources I used: Edit data in datasets, Fill datasets by using TableAdapters