mehmet çelik
mehmet çelik

Reputation: 19

vb.net fatal error encountered during command execution

 Public Sub StokKartiKayit()
        Try
            BaglantiKontrol()


            Dim StokAd As String = StokKartiTanimForm.txtStokAdi.Text.ToUpper
            Dim ms As New MemoryStream

            StokKartiTanimForm.PictureBox1.Image.Save(ms, StokKartiTanimForm.PictureBox1.Image.RawFormat)


            cmd = New MySqlCommand("INSERT INTO stok_karti_tbl (KategoriID,ParcaID,StokAdi,Resim) Values(@Kategori,@Parca,@Stok,@img)", con)
            cmd.Parameters.Add("@Kategori", MySqlDbType.Int32).Value = UrunKategoriID
            cmd.Parameters.Add("@Parca", MySqlDbType.Int32).Value = ParcaID
            cmd.Parameters.Add("@Stok", MySqlDbType.VarChar).Value = StokAd
            cmd.Parameters.Add("@img", MySqlDbType.Blob).Value = ms.ToArray()

            con.Open()

            If cmd.ExecuteNonQuery() = 1 Then
                MsgBox("Stok Kartı Bilgileri Kayıt Edilmiştir...", vbInformation, "MoTap")
            Else
                MsgBox("Lütfen Resim Formatını Kontrol Ediniz. Kayıt İşleminde Sorun Oluştu...", vbCritical, "MoTap")
            End If

            con.Close()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

I have a function like this. When I try to add an image, it gives me a Fatal Error. But when I remove the image column, I don't have any problems. I couldn't solve the problem, I would be grateful if you could help.

Upvotes: 0

Views: 98

Answers (1)

rainan16
rainan16

Reputation: 44

Without the exact exception, it is hard to guess, but the memory stream could be handled like this:

private byte[] imageAsByteArray(Image img)
{
    using (var ms = new MemoryStream())
    {
        img.Save(ms, System.Drawing.Imaging.ImageFormat.RawFormat);
        return ms.ToArray();
    }
}

Then store the image into the database using this:

var data = imageAsByteArray(StokKartiTanimForm.PictureBox1.Image);
MySqlParameter blobParam = new MySqlParameter("@img", MySqlDbType.Blob, data.Length);
blobParam.Value = data;

Upvotes: 1

Related Questions