Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explain ds.Tables[0].Rows.Count?

Tags:

c#

.net

asp.net

I am a beginner, so don't be amused by my questions. I think if (ds.Tables[0].Rows.Count > 0) is used to check if the dataset is empty. But what exactly [0] signify in this case? Can you explain this statement in a little more detail? And this one too.. ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());

like image 636
Arbaaz Avatar asked Dec 24 '12 15:12

Arbaaz


2 Answers

It gives you access to the first table of the DataSet. A DataSet holds an array of DataTables, and it can have 0, 1 or many of these DataTables. You access them just like any other array - by indexing into them.

If there were 2 DataTables in this DataSet, you could access the first one by using ds.Tables[0], and the second one by ds.Tables[1]

The ds.Tables[0].Rows.Add(ds.Tables[0].NewRow()); statement is adding a new row to the first DataTable in the DataSet. By calling ds.Tables[0].NewRow(), you are creating a new row that is associated to the first DataTable in the array.

like image 111
Dave Zych Avatar answered Nov 15 '22 06:11

Dave Zych


Here ds is an instance of DataSet. A DataSet may contain multiple instances of Table.

ds.Tables[0] is accessing the first table in the Tables collection. And ds.Tables[0].Rows.Count is counting the rows on that first table.

Rows accesses the DataRow collection. The Add method creates one more row; however, you need to pass an instance of DataRow as the parameter. This DataRow object must have the same structure (columns) that the table, for this reason, you use the same table to create a new DataRoq: ds.Tables[0].NewRow()

like image 39
Ulises Avatar answered Nov 15 '22 06:11

Ulises