Kristina
Kristina

Reputation: 271

Unable to cast object of type 'System.Int32' to type 'System.Byte[]' in get Image data from SQL Server

I saved image into my SQL Server database in a column of data type image (Binary) and it's successfully saved. Now I need to retrieve it in my datalist, but I got this error :

Unable to cast object of type 'system.int32' to type 'system.Byte'

I will be appreciated any one check my code that where i have mistake

    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            string sql = "SELECT Name,[Last Name],[Father Name],[Passport Number],Sh_ID ,I_ID,Email,[Phone Number], [Address], [Attorney In Iran], [Application Text] ,[Application Title Code] ,[Total Payment] ,[Total Paid],[Iran Case Status],[USA Case Status],Balance,[Customer picture] from permanentCustomer where (I_ID='"+textBoxIDN.Text+"' and [Last Name]='"+textBoxLN.Text+"') or (Sh_ID='"+textBoxBCN.Text+"' and [Last Name]='"+textBoxLN.Text+"') or Sh_ID='"+textBoxBCN.Text+"' or I_ID='"+textBoxIDN.Text+"' ";
            if (conn.State != ConnectionState.Open)
                conn.Open();
           // MessageBox.Show("1");
            command = new SqlCommand(sql, conn);
           // MessageBox.Show("2");
            SqlDataReader reader = command.ExecuteReader();
            //MessageBox.Show("3");
            reader.Read();
           // MessageBox.Show("4");
            if (reader.HasRows)
            {
                textBoxFN.Text = reader[0].ToString();
                textBoxLN.Text = reader[1].ToString();
                textBoxFatherN.Text = reader[2].ToString();
                textBoxPN.Text = reader[3].ToString();
                textBoxBCN.Text = reader[4].ToString();
                textBoxIDN.Text = reader[5].ToString();
                textBoxEmail.Text = reader[6].ToString();
                textBoxPhoneN.Text = reader[7].ToString();
                textBoxAddress.Text = reader[8].ToString();
                textBoxAI.Text = reader[9].ToString();
                richTextBoxAT.Text = reader[10].ToString();

                textBoxTPayments.Text = reader[11].ToString();
                textBoxTPaid.Text = reader[12].ToString();
                textBoxICS.Text = reader[13].ToString();
                textBoxUCS.Text = reader[14].ToString();
                textBoxbalance.Text = reader[15].ToString();
                byte[] img = (byte[])(reader[16]);
                if (img == null) { picCustomer.Image = null; }
                else { MemoryStream ms = new MemoryStream(img); }


            }
            else { MessageBox.Show("This Record is not exist!"); }
            conn.Close();
        }
        catch(Exception ex)
        {
            conn.Close();
            MessageBox.Show(ex.Message);
        }
    }

Upvotes: 0

Views: 1077

Answers (1)

Alexei - check Codidact
Alexei - check Codidact

Reputation: 23078

You are receiving this error because the index does not seem correct:

byte[] img = (byte[])(reader[17]);      // instead of 16, if I counted correctly

should work. However, using string indexer instead of integer one is strongly recommended to avoid this kind of errors and also be able to add/remove columns in your query without changing a lot of indexes:

var img = (byte[])(reader["Customer picture"]);

Upvotes: 3

Related Questions