Home > Software engineering >  vb.net fatal error encountered during command execution
vb.net fatal error encountered during command execution

Time:12-14

 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.

CodePudding user response:

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;

CodePudding user response:

Public Sub StokKartiKayit()

    Try
        BaglantiKontrol()

        Dim ms As New MemoryStream

        Dim bmp As New Bitmap(StokKartiTanimForm.PictureBox1.Image)
        Dim thumbnail As Image = bmp.GetThumbnailImage(300, 200, Nothing, Nothing)
        thumbnail.Save(ms, ImageFormat.Jpeg)


        'StokKartiTanimForm.PictureBox1.Image.Save(ms, StokKartiTanimForm.PictureBox1.Image.RawFormat)
        Dim StokAdi As String = StokKartiTanimForm.txtStokAdi.Text.ToUpper

        cmd = New MySqlCommand("INSERT INTO stok_karti_tbl (KategoriID,ParcaID,StokAdi,Resim) Values(@KategoriID,@ParcaID,@StokAdi,@img)", con)
        cmd.Parameters.Add("@KategoriID", MySqlDbType.Int32).Value = UrunKategoriID
        cmd.Parameters.Add("@ParcaID", MySqlDbType.VarChar).Value = ParcaID
        cmd.Parameters.Add("@StokAdi", MySqlDbType.VarChar).Value = StokAdi
        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, "SMS Savunma")
        Else
            MsgBox("Stok Kartı Bilgileri Kayıt Edilmiştir...", vbInformation, "SMS Savunma")
            'MsgBox("Lütfen Resim Formatını Kontrol Ediniz. Kayıt İşleminde Sorun Oluştu...", vbCritical, "SMS Savunma")
        End If

        con.Close()
        ms.Close()
        bmp.Dispose()
        thumbnail.Dispose()

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

Thank you to anyone who can answer and solve my problem with these lines of code.

  • Related