Click here to Skip to main content
15,896,450 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In VB.Net a package called
VB
System.Data.DataTable
can be imported to a working form or to a class. But I tried this by doing C# like
Using System.Data.DataTable
gives me an error, saying "A using a namespace directive can only be applied to namespaces;'System.Data.DataTable is a type not a namespace'.

Then how am I able to use DataTable in C# 2008 ?

Please help me if you guys have some time.

Thank You!
Chiransj
Posted

You cannot use a class with using which basically defines just a 'shortcut' to a namespace, like imports in VB.

Instead you define a variable which holds an instance of the class and assign an instance to it.

An example from MSDN[^]:
C#
private void MakeDataTableAndDisplay()
{
    // Create new DataTable.
    DataTable table = new DataTable();

    // Declare DataColumn and DataRow variables.
    DataColumn column;
    DataRow row;

    // Create new DataColumn, set DataType, ColumnName 
    // and add to DataTable.    
    column = new DataColumn();
    column.DataType = System.Type.GetType("System.Int32");
    column.ColumnName = "id";
    table.Columns.Add(column);

    // Create second column.
    column = new DataColumn();
    column.DataType = Type.GetType("System.String");
    column.ColumnName = "item";
    table.Columns.Add(column);

    // Create new DataRow objects and add to DataTable.     
    for(int i = 0; i < 10; i++)
    {
        row = table.NewRow();
        row["id"] = i;
        row["item"] = "item " + i;
        table.Rows.Add(row);
    }
    // Set to DataGrid.DataSource property to the table.
    dataGrid1.DataSource = table;
}
 
Share this answer
 
Replace it with
C#
using System.Data;
And the DataTable class will be avaialble:
C#
DataTable dt = new DataTable();
 
Share this answer
 
Comments
Chiranthaka Sampath 23-Dec-12 3:12am    
Ok pal I will try that! Thanks anyway!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900