0
int P=0;
int A=0; 
int L=0;
int E=0;

foreach ( DataRow row in dt_Students.Rows )
{
     P = (int)ada.GetDataByReport(dateTimePicker2.Value.Month, row[1].ToString(),"present").Rows[0][6]; // System.InvalidCastException
     A = (int)ada.GetDataByReport(dateTimePicker2.Value.Month,row[1].ToString(),"absent").Rows[0][6];  
     L = (int)ada.GetDataByReport(dateTimePicker2.Value.Month,row[1].ToString(),"late").Rows[0][6];    
     E = (int)ada.GetDataByReport(dateTimePicker2.Value.Month,row[1].ToString(),"execused").Rows[0][6]; 
}

On the 4th line, I get an error

System.InvalidCastException

Anu K
  • 1
  • 1
  • Using the debugger and/or immediate evaluation, what is `ada.GetDataByReport(dateTimePicker2.Value.Month,row[1].ToString(),"execused").Rows[0][6].GetType().Name`? Is it `Int32 (int)` ? –  Jun 12 '21 at 08:18

2 Answers2

0

As Olivier Rogier suggested, you need to find out the type of ada.GetDataByReport(dateTimePicker2.Value.Month,row[1].ToString(),"execused").Rows[0][6].

System.InvalidCastException is thrown when the conversion of an instance of one type to another type is not supported.

If ada.GetDataByReport(dateTimePicker2.Value.Month,row[1].ToString(),"execused").Rows[0][6] is actually a string, you need to use the methods Nels King suggested.

Until you get the type of what you're trying to cast as int, then we can only make guesses as to what you should to do this

-1

If it's a string or a char, you can try using System.Convert.ToInt32(string), int.Parse(string), or int.TryParse(string, out int). Unless you tell us the type, unfortunately, we will not be able to solve it.


Edit: it seems that you just want to use this:

int.TryParse(ada.GetDataByReport(dateTimePicker2.Value.Month,row[1].ToString(),"present" /* status here */).Rows[0][6], out P /* or A, L, E */)
bg117
  • 354
  • 4
  • 13
  • I tried all these ways but still getting the same error – Anu K Jun 12 '21 at 10:25
  • @AnuK: What is the type of `.Rows[][]`? – bg117 Jun 12 '21 at 10:27
  • p = int.Parse((string)(ada.GetDataByReport(dateTimePicker2.Value.Month, row[1].ToString(), "present").Rows[0][6])); tried using System.Convert.ToInt32(string) as well,still not working – Anu K Jun 12 '21 at 10:27
  • What is the type of `.Rows[0][6]`? – bg117 Jun 12 '21 at 10:29
  • @ Nels King .Rows[][] is of type string – Anu K Jun 12 '21 at 10:29
  • What is the value of it? If it doesn't contain digits, then no method will work. – bg117 Jun 12 '21 at 10:31
  • table has attributes studentid,classid,dateatt,status,studentname,classname here this report is used to display the count of present,absent,late and excuse of a particular student in a month – Anu K Jun 12 '21 at 10:33
  • What is the **value** of `.Rows[0][6]`? – bg117 Jun 12 '21 at 10:35
  • not sure how to handle those cases – Anu K Jun 12 '21 at 10:36
  • Assign `.Rows[0][6]` to a string and print it to the output window. Tell me the value. – bg117 Jun 12 '21 at 10:38
  • .Rows[0][6] is that it is fetching data from the studentsrecordattendance table – Anu K Jun 12 '21 at 10:44
  • I don't know much about csharp, just did this by seeing a video... Basically while I'm viewing report there are 4 colums like present, absent, late and excuse on selecting the date and subject we should get the number of absentees, present, late and excuse in that month.. So kindly help how to do this – Anu K Jun 12 '21 at 11:11
  • The three methods I listed should work... if the `.Rows[0][6]` **only** contains digits. If it contains just one letter, none of the methods would work. Sorry – bg117 Jun 12 '21 at 14:51
  • Wait a minute. You filled up the `int.Parse()` with the string keyword. Incorrect! You should actually pass the `.Rows[0][6]` in there, not the string keyword. – bg117 Jun 12 '21 at 14:53
  • Can you please send how exactly the code should be here ada.GetDataByReport(dateTimePicker2.Value.Month,row[1].ToString(),"execused").Rows[0][6] – Anu K Jun 12 '21 at 16:30
  • I don't know, because I don't know your project. – bg117 Jun 13 '21 at 01:14
  • this method is showing some error int.TryParse(ada.GetDataByReport(dateTimePicker2.Value.Month, row[1].ToString(), "present").Rows[0][6]); which says no overload for method TryParse takes 1 argument – Anu K Jun 13 '21 at 03:49
  • System.Convert.ToInt32(ada.GetDataByReport(dateTimePicker2.Value.Month, row[1].ToString(), "absent").Rows[0][6]); this line shows error saying System.invalidCastException object cannot be cast from DBNull to other types – Anu K Jun 13 '21 at 03:53
  • Did you read the signature for `TryParse`? It's `int.TryParse(string, out int /* use P, A, L, or E here*/)` – bg117 Jun 13 '21 at 04:32
  • Yes I read that but did not know how to modify the code accordingly – Anu K Jun 13 '21 at 04:40
  • int.TryParse(string, out int (ada.GetDataByReport(dateTimePicker2.Value.Month,row[1].ToString(),"execused").Rows[0][6]) ) is this right? – Anu K Jun 13 '21 at 04:43
  • Nope, it should be `int.TryParse(ada.GetDataByReport(dateTimePicker2.Value.Month,row[1].ToString(),"execused").Rows[0][6], out E /* or P, A, L, depending on the context */ )` – bg117 Jun 13 '21 at 04:53
  • p = int.TryParse(ada.GetDataByReport(dateTimePicker2.Value.Month, row[1].ToString(), "present").Rows[0][6],out p); // Absence A = int.TryParse(ada.GetDataByReport(dateTimePicker2.Value.Month, row[1].ToString(), "absent").Rows[0][6],out A); this is displaying an error: cannot convert from object to string – Anu K Jun 13 '21 at 05:51
  • This is the query i have added in the dataset with FillMethodName as FillByReport and and GetMethodName as GetDataByReport ----SELECT COUNT(Status) AS Expr1 FROM AttendanceREcordsTBL2 WHERE (MONTH(DateAtt) = @month) AND (StudentName = @student) AND (Status = @stat) – Anu K Jun 13 '21 at 06:04