Click here to Skip to main content
15,355,659 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more: , +
i design desktop application for Customer
i coding Edit Form
My Error is
Unable to cast object of type 'System.Int32' to type 'System.Byte[]'.
i clicked f10 to debug my Application
My Error Here
||
||
||
__
\/
C#
byte[] pa_img = (byte[])Customer_Function.Customer_Operation.Show_Customer_Image(Convert.ToInt32(this.dataGridView_ShowData.CurrentRow.Cells[0].Value )).Rows[0][0];  my Error Here


/\__
||
||
||
Error Is
Unable to cast object of type 'System.Int32' to type 'System.Byte[]'.

What I have tried:

C#
private void btn_Edit_Click(object sender, EventArgs e)
        {
            Add_New_FORM add_New_FORM = new Add_New_FORM();
            add_New_FORM.btn_Add.Visible = false;
            add_New_FORM.btn_Delete.Visible = false;


            add_New_FORM.ID_TextBox.Text = this.dataGridView_ShowData.CurrentRow.Cells[0].Value.ToString();
            add_New_FORM.Name_TextBox.Text = this.dataGridView_ShowData.CurrentRow.Cells[1].Value.ToString();
            add_New_FORM.phone_TextBox.Text = this.dataGridView_ShowData.CurrentRow.Cells[2].Value.ToString();
            add_New_FORM.kind_TextBox.Text = this.dataGridView_ShowData.CurrentRow.Cells[3].Value.ToString();
            add_New_FORM.textBox4.Text = this.dataGridView_ShowData.CurrentRow.Cells[4].Value.ToString();
            add_New_FORM.FirstDate_TextBox.Text = this.dataGridView_ShowData.CurrentRow.Cells[5].Value.ToString();
            add_New_FORM.End_Date.Text = this.dataGridView_ShowData.CurrentRow.Cells[6].Value.ToString();
            add_New_FORM.Cost_TextBox.Text = this.dataGridView_ShowData.CurrentRow.Cells[7].Value.ToString();
            add_New_FORM.Paid_TextBox.Text = this.dataGridView_ShowData.CurrentRow.Cells[8].Value.ToString();
            add_New_FORM.remain_TextBox.Text = this.dataGridView_ShowData.CurrentRow.Cells[9].Value.ToString();
            add_New_FORM.NotesAboutTRAINING_TextBox.Text = this.dataGridView_ShowData.CurrentRow.Cells[10].Value.ToString();
           


  byte[] pa_img = (byte[])Customer_Function.Customer_Operation.Show_Customer_Image(Convert.ToInt32(this.dataGridView_ShowData.CurrentRow.Cells[0].Value )).Rows[0][0];
            MemoryStream ms = new MemoryStream(pa_img);
            add_New_FORM.pictureBox_ForTRAINER.Image = Image.FromStream(ms);
            add_New_FORM.ShowDialog();
        }
Posted
Updated 4-Apr-22 5:55am
v2
Comments
Afzaal Ahmad Zeeshan 3-Apr-22 20:32pm
   
The exception says that you are trying to convert a number to an array of bytes.

The main I see in your code is that it is confusing to read what is happening. You are doing a lot of things in a single statement, and thus, losing a track of what should happen where.

For example, this link (byte[]) Customer_Function.Customer_Operation.Show_Customer_Image(Convert.ToInt32(this.dataGridView_ShowData.CurrentRow.Cells[0].Value)).Rows[0][0];. I would write it differently. Reading the source and assignment to a destination should be done in two-fold; unless you know what you're doing.

Try to break these statements into two, and debug to see what is the output.

Oh, and if you are converting a variable of type int to a byte[] for an image. I'm guessing the input is not an image in its integer form either.
ahmedbelal 4-Apr-22 11:47am
   
Dear Afzaal Ahmed
I want to design Edit Form in windows form
Customer_Function CLass that has all Funtion i will need
Show_Customer_Image function to showimage in datagridView
want to equal data in textbox or picturebox with Row in DataGridView
Thanks my Dear Afzaal Ahmed

1 solution

In exception, error showing you are converting byte[] to int. Below line of code is throwing this exception Where you are converting
byte[] pa_img =(byte[])Customer_Function.Customer_Operation.Show_Customer_Image(Convert.ToInt32(this.dataGridView_ShowData.CurrentRow.Cells[0].Value )).Rows[0][0];

Use the following code but not sure what type of Show_Customer_Image() is expecting, Pass your value according to the expected data type, that should be byte[].
byte[] pa_img = (byte[])Customer_Function.Customer_Operation.Show_Customer_Image((byte[])this.dataGridView_ShowData.CurrentRow.Cells[0].Value).Rows[0][0];
   
Comments
ahmedbelal 4-Apr-22 11:54am
   
Dear M Imran Ansari
iam sorry i tried your solution
but error is
Can not Convert from byte To Integer

I want to design Edit Form in windows form
Customer_Function CLass that has all Funtion i will need
Show_Customer_Image function to showimage in datagridView
want to equal data in textbox or picturebox with Row in DataGridView

thanks Dear M Imran Ansari

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