Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i design desktop application for Customer
i coding Edit Form
Class Customer_Operation has my Functions
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#

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#
public static DataTable Show_Customer_Image(int MyId) 

        {
            DataTable dt = new DataTable();
            SqlParameter[] p = new SqlParameter[1];
            p[0] = new SqlParameter("@Parameter_Id_FromForm", SqlDbType.Int);
            p[0].Value = MyId;
            ob.open();
            dt = ob.Reader("Show_Customer_Image", p);
            ob.close();
            return dt;


        }



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 10-Apr-22 6:22am
v2

1 solution

As you have already been told in your previous question at Unable to cast object of type 'system.int32' to type 'system.byte[]'.[^], you cannot cast an integer value into a byte array. Whatever you are trying to do with that code, it will never work.
 
Share this answer
 
v2
Comments
ahmedbelal 10-Apr-22 7:35am    
thanks Richard
But,
i repeat my question because it already worked in another project
So i want to Know why doesn't work
ahmedbelal 10-Apr-22 7:37am    
it worked Here

private void btn_Update_FromGrid_Click(object sender, EventArgs e)
{
Forms.F_Patient f_Patient = new F_Patient();
f_Patient.btn_Add_NewPatient.Visible = false;
f_Patient.btn_Delete.Visible = false;
f_Patient.btn_Search.Visible = false;
label1.Text = "Update Patient";
f_Patient.PatientIDTextBox.Text = this.DGV.CurrentRow.Cells[0].Value.ToString();
f_Patient.PatientTCTextBox.Text = this.DGV.CurrentRow.Cells[1].Value.ToString();
f_Patient.PatientNameTextBox.Text = this.DGV.CurrentRow.Cells[2].Value.ToString();
f_Patient.PatientAgeTextBox.Text = this.DGV.CurrentRow.Cells[3].Value.ToString();
if (this.DGV.CurrentRow.Cells[4].Value.ToString() == "Male")
{
f_Patient.radioButton_Male.Checked = true;
}
else
{

f_Patient.radioButton_Female.Checked = true;

}
f_Patient.PatientPhoneTextBox.Text = this.DGV.CurrentRow.Cells[5].Value.ToString();
f_Patient.comboBox_ShowState.SelectedItem = (this.DGV.CurrentRow.Cells[6].Value);
f_Patient.dateTimeNOW.Value = Convert.ToDateTime(this.DGV.CurrentRow.Cells[7].Value.ToString());
f_Patient.EmailTextBox.Text = this.DGV.CurrentRow.Cells[8].Value.ToString();
f_Patient.PatientCostTextBox.Text = this.DGV.CurrentRow.Cells[9].Value.ToString();
f_Patient.PayedTextBox.Text = this.DGV.CurrentRow.Cells[10].Value.ToString();
f_Patient.RemindTextBox.Text = this.DGV.CurrentRow.Cells[11].Value.ToString();
byte[] pa_img = (byte[])ShowImage.Show_Patient_Image(Convert.ToInt32(this.DGV.CurrentRow.Cells[0].Value)).Rows[0][0];
MemoryStream ms = new MemoryStream(pa_img);
f_Patient.pictureBox1.Image = Image.FromStream(ms);
f_Patient.NOTES_TextBox.Text = this.DGV.CurrentRow.Cells[12].Value.ToString();
f_Patient.ShowDialog();
}
Richard MacCutchan 10-Apr-22 7:49am    
The Show_Customer_Image method returns a DataTable. You are trying to convert the value in Rows[0][0] to a byte array, but it is obviously an integer. You need to look at the type of information that is being stored in the DataTable, especially the cell at location Rows[0][0].
ahmedbelal 10-Apr-22 8:45am    
Mr Richard
Do you want to write my StoredProcedure and More Codes ?
Richard MacCutchan 10-Apr-22 11:29am    
No thanks.

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