Home > Software design >  Converting int32 to int64 C#
Converting int32 to int64 C#

Time:09-08

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?)

CodePudding user response:

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.

CodePudding user response:

Replace

int StudentID = ...

with

long StudentID = ...

CodePudding user response:

You can use the code below. It will save you from getting errors.

int64 StudentID = (int64)(INT32ID);

CodePudding user response:

Your conversion is in wrong manner, Try it with below code

var _studenID = long.Parse(_grdStudentDetails.CurrentRow.Cells[2].Value.ToString()); 
  • Related