I neeed Solution for Error :Unable to cast object of type 'system.int32' to type 'system.byte[]'.

Ahmed Belal 1 Reputation point
2022-04-10T09:57:12.933+00:00

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#
Copy Code
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#
Copy Code
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();
}

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,828 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,706 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,238 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Karen Payne MVP 35,036 Reputation points
    2022-04-10T10:50:11.843+00:00

    It's much better to load data using the DataGridView.DataSource.

    Example, the following loads a DataTable with data where Picture is a Byte array when loaded into the DataTable.

    public static DataTable GetCategories()  
    {  
      
        DataTable table = new DataTable();  
        using (var cn = new SqlConnection(_connectionString))  
        {  
            using (var cmd = new SqlCommand() { Connection = cn })  
            {  
                cmd.CommandText = "SELECT CategoryID, CategoryName, Picture FROM dbo.Categories";  
                cn.Open();  
                table.Load(cmd.ExecuteReader());  
                table.Columns["CategoryID"].ColumnMapping = MappingType.Hidden;  
            }  
        }  
      
        return table;  
    }  
    

    In the form, create a BindingSource, set its DataSource to the method above.

    private readonly BindingSource bindingSource = new BindingSource();  
    public DataGridViewWithImageForm()  
    {  
        InitializeComponent();  
        Shown += OnShown;  
    }  
      
    private void OnShown(object sender, EventArgs e)  
    {  
        bindingSource.DataSource = DataOperations.GetCategories();  
        dataGridView1.DataSource = bindingSource;  
    }  
    

    Now to get at the current row's image.

    byte[] imageArray = ((DataRowView)bindingSource.Current).Row.Field<byte[]>("Picture");  
    

    You'll know it works by executing the code but let's show the current image in a picturebox.

    byte[] imageArray = ((DataRowView)bindingSource.Current).Row.Field<byte[]>("Picture");  
      
    ImageConverter converter = new ImageConverter();  
    pictureBox1.Image = (Image)converter.ConvertFrom(imageArray);  
    

    191601-f1.png

    1 person found this answer helpful.