DataTable/DataSet

  • Thread starter Thread starter Jim Heavey
  • Start date Start date
J

Jim Heavey

Hello, I have done a little VB Programming and now I am giving C# a try.
Right now I am working with Dataset and DataTables and there are a couple
of compile errors that I would work in VB, but are apparently different in
C#.

If I want a table from a DataSet, in VB.Net I would enter the command as
follows:

Dim Table as DataTable = ds.Tables("MyTable")

I tried this in C#

DataTable tbl = ds.Tables("Events");


To get an item from a dataReader into a DataROW Field I would use a command
which looks like the following:

Row.Items("MyField") = dr.items("Field1")

IN C# I tried...
row(0) = dr.GetInt16(0);

When I create a column in a DataTable in VB.Net, I would enterer

Tbl.Columns.Add("MyField", getType(System.String))

In C# I tried

tbl2.Columns.Add("ClubID", GetType(System.Int16));

What am I doing wrong?
 
Jim,

The major problem I see is your syntax.

DataTable tbl = ds.Tables("Events");
DataTable tbl = ds.Tables["Events"]; //need to use square brackets

Row.Items("MyField") = dr.items("Field1");
Row.Items["MyField"] = dr.items["Field1"];//need to use square brackets

this one is a little more work:
Tbl.Columns.Add("MyField", getType(System.String))

DataColumn column = new DataColumn("myField");
column.DataType = Type.GetType("System.Int32");//Nedd to enclose the type in
"Quotes"
Tbl.Columns.Add(column);

If you are already comfortable with VB syntax, perhaps you should consider
coding in VB.NET. You might find the learning curve a little easier.

Hope this helps

Marco
 
Arrays in C# have square brackets, like this [ ] . I've stumbled myself over
these quite a few times... :-)

For example
DataTable tbl = ds.Tables["Events"];

The GetType method is in C# typeof(...):
tbl2.Columns.Add("ClubID", typeof(System.Int16));
 
Back
Top