Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB9.0 C# Visual-Studio
In VB.Net a package called
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 21-Dec-12 21:31pm
Rate this: bad
good
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();
  Permalink  
Comments
Chiranthaka Sampath at 23-Dec-12 3:12am
   
Ok pal I will try that! Thanks anyway!
Rate this: bad
good
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";
    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;
}
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 570
1 Sergey Alexandrovich Kryukov 354
2 BillWoodruff 299
3 PIEBALDconsult 290
4 George Jonsson 225
0 Sergey Alexandrovich Kryukov 10,615
1 Maciej Los 5,354
2 OriginalGriff 5,254
3 CPallini 3,719
4 BillWoodruff 3,476


Advertise | Privacy | Mobile
Web03 | 2.8.141029.1 | Last Updated 22 Dec 2012
Copyright © CodeProject, 1999-2014
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