Click here to Skip to main content
15,569,845 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a problem referencing a datatable field. I set up the datatable and fill it with data like this:-
public partial class Form1 : Form
     public DataTable dtStaff = new DataTable("StaffList");
     DataColumn dc400 = new DataColumn();
     DataColumn dc401 = new DataColumn();
 public Form1()
     private void Form1_Load(object sender, EventArgs e)
         dc400.ColumnName = "StaffID"; dc400.Caption = "Person ID"; dc400.DataType = System.Type.GetType("System.Int32");
         dc401.ColumnName = "FullName"; dc401.Caption = "Name"; dc401.DataType = System.Type.GetType("System.String");
         string SelectString = "SELECT PersonID, FullName FROM People INNER JOIN [Workbench].[dbo].[TimesheetGroups] ON People.TimesheetGroupID=TimeSheetGroups.GroupID";
         SelectString += " WHERE (TimeSheetGroups.Approver1 = " + WBID + " OR TimeSheetGroups.Approver2 = " + WBID + ")";
         using (SqlConnection con7 = new SqlConnection(WorkbenchConnection))
             SqlCommand command07 = new SqlCommand(SelectString, con7);
                 SqlDataAdapter da1 = new SqlDataAdapter(command07);

I know the data fill works because in a Watch window I can see expected results for dtStaff.Rows[0]["FullName"] with various row numbers.
However dtStaff.Rows[0]["StaffID"] gives "... threw an exception of type 'System.ArgumentException'
Attempting to use it elsewhere gives an error 'Column 'StaffID' does not belong to table 'StaffList'
Is there something special about the first column of a Datatable?

What I have tried:

I've tried renaming the field, debugging other places in the code where a datatable works, etc.
Updated 2-Feb-21 11:34am

1 solution

No need for Datacolumns at top. Do like below
dtStaff.Columns.Add("StaffID", typeof(Int32));
dtStaff.Columns.Add("FullName", typeof(String));

Or Create a Datacolumn at top & use it below for adding columns.
DataColumn dtColumn; 

    // Create StaffID column  
    dtColumn = new DataColumn();  
    dtColumn.DataType = typeof(Int32);  
    dtColumn.ColumnName = "StaffID";  
    dtColumn.Caption = "Staff ID";
    //Add Column to Table

And learn about SQL Injection[^]. Change your code ASAP.
Share this answer
ormonds 2-Feb-21 16:35pm    
This seems to be just a different way of arriving setting up the DataTable. Certainly it gives the same result - no value for StaffID.
ormonds 2-Feb-21 17:10pm    
..and yes, the app is for use by a small group of people in house, but I am reading up about SQL injection right now.
ormonds 2-Feb-21 18:34pm    
Thank you, Kelly, that's it. Appreciated. For some reason your answer hasn't turned up on the page.
thatraja 2-Feb-21 23:34pm    
Are you getting any error? This should be working one.
ormonds 3-Feb-21 0:34am    
No, all working now, Kelly had the answer. Thanks.

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