user2509901
user2509901

Reputation:

Retrieving data from a dataset

I am sorry if this question has been asked before, i have tried using google but all the answers couldn't help me.

I am a total begginer in using datasets and connecting to database using c#. I have a dataset usersDataset gotten from an access 2007 Database that has two table Administrators and Users. Both tables have two columns UserName and Password. The Administrators Table has a row of data.

Now the problem is that i want to retrive the row in the dataset. I have tried many things but they all throw an Exception.

I have tried

    DataRow rows = usersDataset1.Administrators.Rows[0];
    MessageBox.Show(rows.ToString());

Also tried

   usersDataset data = new usersDataset();
   MessageBox.Show(data.Administrators.Rows[0].ToString());

Also tried

   MessageBox.Show(usersDataset1.Tables[0].Rows[0].ToString());

All of the following code snippets throw an IndexOutofRangeException with the message There is no Row at Index 0.

Then i tried

   MessageBox.Show(usersDataset1.Administrators.Rows.Count.ToString());

It shows '0'.

Pls what did i do wrong and how can i correct it?

EDIT : When i drag the Administrator from the DataSources Windom to the form and i run the Application. The row is displayed.

Upvotes: 1

Views: 322

Answers (1)

Xynariz
Xynariz

Reputation: 1243

Your current problem (as evidenced by your last example) is that you have no data in the Administrators table. Perhaps you aren't properly loading your data into your data structure?

On a side note, it's generally better to iterate through the rows in a table unless you will always know exactly how many rows it will contain. For example:

foreach (DataRow row in usersDataset1.Administrators.Rows)
{
  //Do stuff here...
}

Upvotes: 3

Related Questions