Skip to main content

Posts

Showing posts from April, 2011

c# - How to use DataTable AcceptChanges() method

Using DataTable AcceptChanges() Method The DataTable class represents one table of in-memory data. The DataTable objects are conditionally case-sensitive. To create a DataTable programmatically the asp.net developers must first define its schema by adding DataColumn objects to the DataColumnCollection. To add rows to a DataTable, the developers must use the NewRow() method to return a new DataRow object at first. The DataTable also contains a collection of Constraint objects that can be used to ensure the integrity of the data. The following asp.net c# tutorial code demonstrates how we use the DataTable class AcceptChanges() method in an asp.net web application. The DataTable class AcceptChanges() method commits all the changes made to this table since the last time AcceptChanges() method was called. So the developers use the AcceptChanges() method to commit the changes they made on a DataTable instance. When the asp.net web developers ...

c# - How to set all the values for a DataRow through an array

DataRow ItemArray Property .NET framework's DataRow class represents a row of data in a DataTable. DataRow class exists in the System.Data namespace. DataRow class’s ItemArray property allows us to get or set all the values for this row through an Array. ItemArray property value type is System.Object[] which represents an Array of type Object. If we use the ItemArray property to set a row value, the Array must have the same size and order as the DataTable's column collection. Passing null in ItemArray indicates that no value was specified. DataRow ItemArray property throws an ArgumentException exception if the Array is larger than the table columns number. It throws InvalidCastException if a value in the Array does not match its DataType in its respective DataColumn. The property throws ConstraintException if an edit broke a constraint. It throws ReadOnlyException if an edit tried to change the value of a read-only column. ...

c# - How to select rows from a DataTable

Select rows from a DataTable The DataTable class represents one table of in-memory data. The DataTable objects are conditionally case-sensitive. To create a DataTable programmatically the asp.net developers must first define its schema by adding DataColumn objects to the DataColumnCollection. To add rows to a DataTable, the developers must use the NewRow() method to return a new DataRow object at first. The DataTable also contains a collection of Constraint objects that can be used to ensure the integrity of the data. The following asp.net c# tutorial code demonstrates how we can select rows from a DataTable instance. So we will get all the rows from a DataTable. The DataTable class Select() method gets an array of DataRow objects. The DataTable Select() method returns an array of DataRow objects. The asp.net web developers don’t have to pass any parameter to this Select() method to get all the rows from a DataTable object. UsingDataTableSel...

c# - Select rows that match filter criteria from a DataTable

Select rows that match filter criteria from a DataTable The DataTable class represents one table of in-memory data. The DataTable objects are conditionally case-sensitive. To create a DataTable programmatically the asp.net developers must first define its schema by adding DataColumn objects to the DataColumnCollection. To add rows to a DataTable, the developers must use the NewRow() method to return a new DataRow object at first. The DataTable also contains a collection of Constraint objects that can be used to ensure the integrity of the data. The following asp.net c# tutorial code demonstrates how we can select data from a DataTable instance with the specified condition. So we will get the rows from the DataTable which match the filter criteria. The DataTable class Select() method gets an array of DataRow objects. The Select(String) method overload gets an array of all DataRow objects that match the filter criteria. The Select(string?...

c# - How to count rows in a DataTable

Count rows in a DataTable The DataTable class represents one table of in-memory data. The DataTable objects are conditionally case-sensitive. To create a DataTable programmatically the asp.net developers must first define its schema by adding DataColumn objects to the DataColumnCollection. To add rows to a DataTable, the developers must use the NewRow() method to return a new DataRow object at first. The DataTable also contains a collection of Constraint objects that can be used to ensure the integrity of the data. The following asp.net c# tutorial code demonstrates how we can count rows in a DataTable. So we have to count the DataRow objects within a DataTable instance. Here we used the DataTable class Rows property to get its rows collection as a DataRowCollection object. Then we used DataRowCollection’s Count property to count the rows within the rows collections. The DataTable class Rows property gets the collection of rows that belong to th...

c# - How to delete a row from a DataTable

Delete a row from the DataTable The DataTable class represents one table of in-memory data. The DataTable objects are conditionally case-sensitive. To create a DataTable programmatically the asp.net developers must first define its schema by adding DataColumn objects to the DataColumnCollection. To add rows to a DataTable, the developers must use the NewRow() method to return a new DataRow object at first. The DataTable also contains a collection of Constraint objects that can be used to ensure the integrity of the data. The following asp.net c# tutorial code demonstrates how we can delete a row from a DataTable. So we will remove a DataRow object from a DataTable instance at the specified index position. In this example code, we will remove a row by using its index value from a DataTable object. Here we used the DataTable class Rows property to get its rows collection as a DataRowCollection object. Then we used DataRowCollection’s RemoveAt() me...

c# - How to insert a new row into DataTable

Insert a new row into the DataTable The DataTable class represents one table of in-memory data. The DataTable objects are conditionally case-sensitive. To create a DataTable programmatically the asp.net developers must first define its schema by adding DataColumn objects to the DataColumnCollection. To add rows to a DataTable, the developers must use the NewRow() method to return a new DataRow object at first. The DataTable also contains a collection of Constraint objects that can be used to ensure the integrity of the data. The following asp.net c# tutorial code demonstrates how we can insert a new row into a DataTable. So we have to insert a new DataRow object into the DataTable’s specified index position. Here we used the DataTable class Rows property to get its rows collection as a DataRowCollection object. Then we used DataRowCollection’s InsertAt() method to insert a new DataRow object at a specified index position of the rows collection...

c# - How to change DataTable columns order

Change the order of the columns in a DataTable The DataTable class represents one table of in-memory data. The DataTable objects are conditionally case-sensitive. To create a DataTable programmatically the asp.net developers must first define its schema by adding DataColumn objects to the DataColumnCollection. To add rows to a DataTable, the developers must first use the NewRow() method to return a new DataRow object. The DataTable also contains a collection of Constraint objects that can be used to ensure the integrity of the data. The following asp.net c# tutorial code demonstrates how we can change the order of the columns within a DataTable. So we have to change a specified DataColumn object’s order/position in a DataTable instance. Here we used the DataTable class Columns property to get its columns collection as a DataColumnCollection object. Then we used DataColumnCollection’s Item[Int32] property to get the specified DataColumn instan...

c# - How to add a new row into DataTable

Add a new row to the DataTable The DataTable class represents one table of in-memory data. The DataTable objects are conditionally case-sensitive. To create a DataTable programmatically the asp.net developers must first define its schema by adding DataColumn objects to the DataColumnCollection. To add rows to a DataTable, the developers must use the NewRow() method to return a new DataRow object at first. The DataTable also contains a collection of Constraint objects that can be used to ensure the integrity of the data. The following asp.net c# tutorial code demonstrates how we can add a new row into a DataTable. So we have to add a new DataRow object into the DataTable. Here we used the DataTable class Rows property to get its rows collection as a DataRowCollection object. Then we used DataRowCollection’s Add() method to add a new DataRow object to the rows collections. The DataTable class Rows property gets the collection of rows that be...

c# - How to get column maximum length in a DataTable

Get the maximum length of a column in a DataTable The DataTable class represents one table of in-memory data. The DataTable objects are conditionally case-sensitive. To create a DataTable programmatically the asp.net developers must first define its schema by adding DataColumn objects to the DataColumnCollection. To add rows to a DataTable, the developers must use the NewRow() method to return a new DataRow object at first. The DataTable also contains a collection of Constraint objects that can be used to ensure the integrity of the data. The following asp.net c# tutorial code demonstrates how we can get the maximum size/length of columns from a DataTable. So we have to get the maximum length of each DataColumn object within a DataTable instance. In this example, we used the DataTable class Columns property to get its columns collection as a DataColumnCollection object. Then we loop through the columns collection. In the looping time, we used...

c# - How to get a DataTable columns data type

Get DataColumn’s DataType from DataTable The DataTable class represents one table of in-memory data. The DataTable objects are conditionally case-sensitive. To create a DataTable programmatically the asp.net developers must first define its schema by adding DataColumn objects to the DataColumnCollection. To add rows to a DataTable, the developers must first use the NewRow() method to return a new DataRow object. The DataTable also contains a collection of Constraint objects that can be used to ensure the integrity of the data. The following asp.net c# tutorial code demonstrates how we can get the data type of columns from a DataTable. So we have to get the data type of each DataColumn object within a DataTable instance. Here we used the DataTable class Columns property to get its columns collection as a DataColumnCollection object. Then we loop through the columns collection. In the looping time, we used DataColumnCollection’s Item[Int32] pro...

c# - How to get index of a column by name in a DataTable

Get column index by name from a DataTable The DataTable class represents one table of in-memory data. The DataTable objects are conditionally case-sensitive. To create a DataTable programmatically the asp.net developers must first define its schema by adding DataColumn objects to the DataColumnCollection. To add rows to a DataTable, the developers must first use the NewRow() method to return a new DataRow object. The DataTable also contains a collection of Constraint objects that can be used to ensure the integrity of the data. The following asp.net c# tutorial code demonstrates how we can get the index of a column by the column name from a DataTable. So we have to find the index of a specified DataColumn object by its name within a DataTable instance. Here we used the DataTable class Columns property to get its columns collection as a DataColumnCollection object. Then we used DataColumnCollection’s IndexOf() method to get the specified column’s index u...

c# - How to get column names from a DataTable

Get all DataColumns name from DataTable .NET framework's DataTable represents one table of in-memory data. The DataTable class exists in the System.Data namespace. The DataTable Columns property allows us to get the collection of columns that belong to this table. DataTable Columns property value type is System.Data.DataColumnCollection which represents a DataColumnCollection that contains the collection of DataColumn objects for the table. If DataTable contains no DataColumn objects, then this property returns an empty collection. .NET framework's DataColumnCollection class represents a collection of DataColumn objects for a DataTable. DataColumnCollection determines the schema of a table by defining the data type of each column. We can loop through a DataTable's DataColumnCollection to get all DataColumn's names from the DataTable. In the below example code, we applied a foreach loop through the DataColumnCollection to get all column...

c# - How to count columns in a DataTable

Count columns in a DataTable The DataTable class represents one table of in-memory data. The DataTable objects are conditionally case-sensitive. To create a DataTable programmatically the asp.net developers must first define its schema by adding DataColumn objects to the DataColumnCollection. To add rows to a DataTable, the developers must first use the NewRow() method to return a new DataRow object. The DataTable also contains a collection of Constraint objects that can be used to ensure the integrity of the data. The following asp.net c# tutorial code demonstrates how we can count columns in a DataTable. So we have to count the DataColumns within a DataTable instance. Here we used the DataTable class Columns property to get its columns collection as a DataColumnCollection object. Then we used DataColumnCollection’s Count property to count the columns within the columns collections. The DataTable class Columns property gets the collection of columns that...

c# - How to check if a column exists in a DataTable

Determine whether the DataTable contains a specified DataColumn The DataTable class represents one table of in-memory data. The DataTable objects are conditionally case-sensitive. To create a DataTable programmatically the asp.net developers must first define its schema by adding DataColumn objects to the DataColumnCollection. To add rows to a DataTable, the developers must first use the NewRow() method to return a new DataRow object. The DataTable also contains a collection of Constraint objects that can be used to ensure the integrity of the data. The following asp.net c# tutorial code demonstrates how we can determine whether a DataTable contains a specific DataColumn. So we have to check whether a specified DataCoumn exists or not within a DataTable. Here we used the DataTable class Columns property to get its columns collection then we find the specified column within the DataTable’s columns collection. The DataTable class Columns property gets the...

c# - How to set maximum size of a column in a DataTable

Set the maximum size of a DataColumn in a DataTable The DataTable class represents one table of in-memory data. The DataTable objects are conditionally case-sensitive. To create a DataTable programmatically the asp.net developers must first define its schema by adding DataColumn objects to the DataColumnCollection. To add rows to a DataTable, the developers must first use the NewRow() method to return a new DataRow object. The DataTable also contains a collection of Constraint objects that can be used to ensure the integrity of the data. The following asp.net c# tutorial code demonstrates how we can set the maximum size of a DataColumn in a DataTable instance. Here we used the DataColumn class MaxLength property to set the maximum size/length of a DataColumn in a DataTable object. The DataColumn class MaxLength property gets or sets the maximum length of a text column. The MaxLength property value is an Int32 which is the maximum length of th...

c# - How to create a DataColumn with default value

DataColumn DefaultValue Property The .NET framework's DataColumn represents the schema of a column in a DataTable. DataColumn class exists in the System.Data namespace. We can initialize a new DataColumn class by many constructors such as DataColumn(), DataColumn(String), DataColumn(String, Type), etc. DataColmun() constructor initializes a new DataColumn class instance as a type String. The DataColumn DefaultValue property allows us to get or set the default value for the column when we are creating new rows. DefaultVlaue property value data type is a String which represents a value appropriate to the column's DataType. DataColumn DefaultVlaue property throws an InvalidCastException exception when we are adding a row and the default value is not follow the column's data type. When we create a new DataRow, a default value is automatically assigned to the column in which the DefaultVlaue property is set. If we set AutoIncr...