Click here to Skip to main content
14,739,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a datatable to which I add many columns with differing datatypes.
When, after setting up the datatable I check the datatype of columns they are all string.
My code is:
public partial class Form1 : Form
        int i; 
        string SelectString;
        string MyConnection = "sssss;database=Workbench;UID=sa;password=xxxx";
        string ParentNumber;
        int dtcounter;
        int dt1counter;

        DataTable dt = new DataTable("MyJobTransactions");
        DataRow NewRow;
        DataRow row;
        DataColumn dc1 = new DataColumn();
        DataColumn dc2 = new DataColumn();
        DataColumn dc3 = new DataColumn();
        DataColumn dc4 = new DataColumn();
        DataColumn dc5 = new DataColumn();
        DataColumn dc6 = new DataColumn();
        DataColumn dc7 = new DataColumn();
        DataColumn dc8 = new DataColumn();
        DataColumn dc9 = new DataColumn();
        DataColumn dc10 = new DataColumn();
	etc etc....
        DataColumn dc108 = new DataColumn();
		private void Populate_Columns()
		dc1.ColumnName = "JobTranID";
		dc1.Caption = "JobTranID";
		dc1.DataType = System.Type.GetType("System.Int32");
		dc2.ColumnName = "JobCode";
		dc2.Caption = "JobCode";
		dc2.DataType= System.Type.GetType("System.String");
		dc3.ColumnName = "ActivityCode";
		dc3.Caption = "ActivityCode";
		dc3.DataType = System.Type.GetType("System.String");
		dc4.ColumnName = "WorkCentreCode";
		dc4.Caption = "WorkCentreCode";
		dc4.DataType = System.Type.GetType("System.String");
		dc5.ColumnName = "TranDate";
		dc5.Caption = "Transaction Date";
		dc5.DataType= System.Type.GetType("System.DateTime");
		dc6.ColumnName = "YearNo";
		dc6.Caption = "Year No";
		dc6.DataType = System.Type.GetType("System.Int32");
		dc7.ColumnName = "PeriodNo";
		dc7.Caption = "Period No";
		dc7.DataType = System.Type.GetType("System.Int32");
		dc8.ColumnName = "TranType";
		dc8.Caption = "Transaction Type";
		dc8.DataType = System.Type.GetType("System.String");
		dc9.ColumnName = "SiteID";
		dc9.Caption = "Site ID";
		dc9.DataType = System.Type.GetType("System.Int32");
		dc10.ColumnName = "BatchNo";
		dc10.Caption = "Batch No";
		dc10.DataType = System.Type.GetType("System.Int32");
		dc107.ColumnName ="JobManagerApprovedDate";
		dc107.Caption = "Job ManagerDate"
		;dc107.DataType = System.Type.GetType("System.DateTime");
		public Form1()
		     private void Form1_Load(object sender, EventArgs e)
		     dtcounter = 1;
		     string dcnumber;
		     while (dtcounter < 108)
		         dcnumber = "dc" + dtcounter.ToString();
		         dtcounter += 1;

The final messagebox tells me that dt.Columns[0] is of type string, rather than Int32.

How do I correctly assign a datatype?

What I have tried:

I've tried all the alternatives including typeof().
I've tried listing the type of all dt.Columns - they are all string.
Updated 14-Nov-20 21:01pm

1 solution

What you appear to be trying to do is generate the name of a variable and then use that variable's content to add a column to a DB:
while (dtcounter < 108)
    dcnumber = "dc" + dtcounter.ToString();
    dtcounter += 1;
That won't work: you cannot access variables via a string (without using Reflection, and that gets pretty hairy for a beginner like you).

So what you add to the dataTable as a column is a string "dc1", "dc2", etc., instead of the column you are trying to add.
If you must add columns like that (and I don't recommend it) create an array of DataColumns:
private DataColumn[] columns = new DataColumn[108];
And add a new DataColumn object instance to each:
private void Populate_Columns()
    DataColumn dc = new DataColumn();
    dc.ColumnName = "JobTranID";
    dc.Caption = "JobTranID";
    dc.DataType = System.Type.GetType("System.Int32");
    columns[0] = dc;
    DataColumn dc = new DataColumn();
    dc.ColumnName = "JobCode";
    dc.Caption = "JobCode";
    dc.DataType= System.Type.GetType("System.String");
    columns[1] = dc;
    dc = new DataColumn();
    dc.ColumnName = "ActivityCode";
Then you can access them via your index.

But to be honest, that's a pretty poor way to do it all. I'd use a configuration file and load the DataTable directly from that: CSV, XML, JSON - any of those would do.
ormonds 15-Nov-20 2:54am
Thank you.
OriginalGriff 15-Nov-20 3:19am
You're welcome!

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