Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
dtTable.Column[0].ColumnName=STUDENTS_INFORMATION.ID.ToString(); //This error is:
Cannot find column 0.



Excel file:

ID   NAME    CLASS

  86    Kelvin    4  

  59    Johny     7



DATABASE TABLE: STUDENTS_INFORMATION


ID NAME CLASS


I'm trying to read Excel values.
I read the values in foreach condition, but I can't write datatable.And so here I get the error.
Cannot find column 0.

What I have tried:

C#
//select column and set

DataTable dtTable = new DataTable();

dtTable.Column[0].ColumnName=STUDENTS_INFORMATION.ID.ToString();
dtTable.Column[1].ColumnName=STUDENTS_INFORMATION.Name.ToString();
dtTable.Column[2].ColumnName=STUDENTS_INFORMATION.Class.ToString();
}
Posted
Updated 2-Jun-19 21:34pm
v4
Comments
Nirav Prabtani 3-Jun-19 1:27am    
Put, breakpoint and try to find the values in the dataSet

Why your dataset doesn't contains the values ?

If you don't have a column 0, then you don't have a column - you have a completely empty DataTable, the the most likely reason for that is that your sheet is also empty.

We can't check that - we have no access to your spreadsheet - so we can't check that for you.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
I think it might be because your table doesn't have any rows.

Here you're creating a new instance of the DataTable and immediately trying to access the columns

C#
DataTable dtTable = new DataTable();

dtTable.Column[0].ColumnName=STUDENTS_INFORMATION.ID.ToString();


You need to add a row to that DataTable first.

C#
DataTable dtTable = new DataTable();
dtTable.NewRow(); // Add a new row
dtTable.Column[0].ColumnName=STUDENTS_INFORMATION.ID.ToString();
 
Share this answer
 
Comments
Simon_Whale 3-Jun-19 14:48pm    
+5 :thumpsup:
Dylvh 4-Jun-19 3:03am    
Much appreciated

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