how image from Picturebox is stored into sql

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • veer
    New Member
    • Jul 2007
    • 198

    how image from Picturebox is stored into sql

    Hi
    i want to save image from picturebox into sql but i got the error 'ARTHEMATIC OPERATIONS RESULTED IN AN OVERFLOW' On line
    bytBLOBData = New Byte() {ms.Length}

    here is my code
    sqlConnection = New SqlClient.SqlCo nnection(sConne ctionString)
    sqlCommand = New SqlClient.SqlCo mmand("INSERT INTO BLOBTest (BLOBData) VALUES (@BLOBData)", sqlConnection)

    '//Save image from PictureBox into MemoryStream object.
    ms = New MemoryStream()
    PictureBox1.Ima ge.Save(ms, ImageFormat.Jpe g)

    '//Read from MemoryStream into Byte array.
    bytBLOBData = New Byte() {ms.Length} // ERROR IS HERE
    ms.Position = 0
    ms.Read(bytBLOB Data, 0, Convert.ToInt32 (ms.Length))

    '//Create parameter for insert statement that contains image.
    prm = New SqlClient.SqlPa rameter("@BLOBD ata", SqlDbType.VarBi nary, bytBLOBData.Len gth, ParameterDirect ion.Input, False, 0, 0, "BLOBData", DataRowVersion. Current, bytBLOBData)

    sqlCommand.Para meters.Add(prm)
    sqlConnection.O pen()
    sqlCommand.Exec uteNonQuery()
    sqlConnection.C lose()

    PLEASE GIVE ME SOME SOLUTION
    VARINDER
  • ganeshvkl
    New Member
    • Jul 2008
    • 50

    #2
    hi,
    The Soln is Below , it's Working Fine..
    Dim msImage As New MemoryStream
    Dim strSQL As String
    Dim Cn As New SqlConnection(S TR_CURRENT_CONN ECTION_STRING)
    Dim cmd As SqlCommand

    Try
    If Not (imgStyle.Image Is Nothing) Then
    imgStyle.Image. Save(msImage, imgStyle.Image. RawFormat)
    Dim arrImage() As Byte = msImage.GetBuff er

    ' Close the stream object to release the resource.
    msImage.Close()
    strSQL = "INSERT INTO tableName (imgDesign)" _
    & " VALUES (@Picture)"

    '' A SqlCommand object is used to execute the SQL statement.
    cmd = New SqlCommand(strS QL, Cn)
    With cmd
    .Parameters.Add (New SqlParameter("@ Picture", SqlDbType.Image )).Value = arrImage
    End With

    Try it out.......
    bye
    Ganesh

    Comment

    Working...