Reputation: 1
I have problems with converting these two mentioned types. could somebody give me the solution please?
This is the code:
int INT32ID = int.Parse(dataGridView1.CurrentRow.Cells[2].Value.ToString());
int StudentID = **Convert.ToInt64(INT32ID);**(here is the error)
var Student = DB.Students.Find(StudentID);
DB.Entry(Student).State = EntityState.Deleted;
And here is what the error says:
Cannot implicitly convert type 'long' to 'int'. An explicit conversion exists (are you missing a cast?)
Upvotes: 0
Views: 3694
Reputation: 36649
int StudentID = Convert.ToInt64(INT32ID);
You are converting an int32 to an int64, and then assigning it to a int32. Since a int64 do not fit into a int32, you get an error. You should be able to just write:
long StudentID = INT32ID ;
or
var StudentID = long.Parse(dataGridView1.CurrentRow.Cells[2].Value.ToString());
int32 is implicitly convertable to int64 since every 32 bit int can exactly be expressed as a 64 bit int, so there is no risk for data loss.
Upvotes: 6
Reputation: 40
Your conversion is in wrong manner, Try it with below code
var _studenID = long.Parse(_grdStudentDetails.CurrentRow.Cells[2].Value.ToString());
Upvotes: 0