Click here to Skip to main content
12,819,619 members (34,063 online)
Rate this:
Please Sign up or sign in to vote.
See more: VB9.0 C# VB Visual-Studio
In VB.Net a package called
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!
Posted 21-Dec-12 22:31pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Replace it with
using System.Data;
And the DataTable class will be avaialble:
DataTable dt = new DataTable();
Chiranthaka Sampath 23-Dec-12 3:12am
Ok pal I will try that! Thanks anyway!
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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[^]:
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";

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

    // 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;
    // Set to DataGrid.DataSource property to the table.
    dataGrid1.DataSource = table;

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

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170308.1 | Last Updated 22 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100