Click here to Skip to main content
15,892,059 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: How do I get a custom context menu to show up??? Pin
Tom Foswick4-Sep-10 7:50
Tom Foswick4-Sep-10 7:50 
GeneralRe: How do I get a custom context menu to show up??? Pin
thebiostyle4-Sep-10 7:53
thebiostyle4-Sep-10 7:53 
QuestionAn easy way to archive many files into one file? [Solved] Pin
Pamodh22-Aug-10 6:28
Pamodh22-Aug-10 6:28 
AnswerRe: An easy way to archive many files into one file? Pin
Eddy Vluggen22-Aug-10 9:57
professionalEddy Vluggen22-Aug-10 9:57 
GeneralRe: An easy way to archive many files into one file? Pin
Pamodh23-Aug-10 5:34
Pamodh23-Aug-10 5:34 
GeneralRe: An easy way to archive many files into one file? Pin
Eddy Vluggen23-Aug-10 9:40
professionalEddy Vluggen23-Aug-10 9:40 
GeneralRe: An easy way to archive many files into one file? Pin
Pamodh24-Aug-10 1:47
Pamodh24-Aug-10 1:47 
QuestionProblem in saving image to database from picturebox. VB.Net 2008. Framework 3.5. Pin
priyamtheone20-Aug-10 3:23
priyamtheone20-Aug-10 3:23 
Hi there,
I have a form containing a listbox showing a list of image names. It's bound
to the database table. When an image name is clicked it shows the image and
imagename in a picturebox and textbox respectively. When no image is selected
in the listbox, a new record can be inserted by browsing a new image in the
picturebox by an openfiledialog, writing the imagename in the textbox and
pressing the OK button. When an image is already selected, the record can be
updated by pressing the same OK button. The data is saved into MSSQL Server
2005. Corresponding table fields are Keycode int autono,
logoname nvarchar(50), logo image.
Now the problem, when I insert a new data with an image everything goes fine
but whenever I try to update an existing data with an image it throws an
exception- 'A generic error occurred in GDI+.' at the following line-
'pic.Image.Save(ms, pic.Image.RawFormat)'. Surprisingly when I update an
existing data without any image in the picturebox no exception is generated.
I have crossed checked it and seems that the problem is just at one point-
'Updating the image from the picturebox'.
I'm almost done all throughout but stuck to this particular point. Please help. Regards.

My code to insert/update the data by OK button and to populate it by listbox
doubleclick follows:

Private ms As MemoryStream
Private arrImage() As Byte
Private conn As SqlConnection
Private cmd As SqlCommand

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
	'Method to bind listbox.
	BindListBox(lst, "Select Keycode,LogoName from tbltest", "Logoname", "keycode")
        Tag = "Insert"
End Sub

Private Sub lst_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lst.DoubleClick
        Dim dr As SqlDataReader

        dr = CreateReader("Select LogoName,logo from tblTest where keycode=" & lst.SelectedValue)
        If dr.Read Then
            txtLogoName.Text = vbNullString & dr("Logoname")
            If Not IsDBNull(dr("Logo")) Then
                arrImage = CType(dr("Logo"), Byte())
                ms = New MemoryStream(arrImage)
                pic.Image = Image.FromStream(ms)
                ms.Close()
            Else
                pic.Image = Nothing
                pic.Invalidate()
            End If
            Tag = "Update"
        End If
        dr.Close()
        closeconnection()
        arrImage = Nothing
        ms = Nothing
End Sub

Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
        Dim com As SqlCommand
        Dim strSql As String

        If Tag = "Insert" Then
            strSql = "Insert into tbltest (logoname,logo) values ('" & Trim(txtLogoName.Text) & "',@Logo)"
        Else
            strSql = "Update tbltest set logoname='" & Trim(txtLogoName.Text) & "',Logo=@Logo Where keycode=" & lst.SelectedValue
        End If

        com = CreateCommand(strSql)
        com.Parameters.Add(New SqlParameter("@Logo", SqlDbType.Image))
        If Not pic.Image Is Nothing Then
            ms = New MemoryStream()
            pic.Image.Save(ms, pic.Image.RawFormat)
            arrImage = ms.GetBuffer
            ms.Close()
            com.Parameters("@Logo").Value = arrImage
        Else
            com.Parameters("@Logo").Value = DBNull.Value
        End If

        If com.ExecuteNonQuery = 1 Then
            closeconnection()
            BindListBox(lst, "Select Keycode,LogoName from tbltest", "Logoname", "keycode")
            pic.Image = Nothing
            pic.Invalidate()
            txtLogoName.Clear()
            Tag = "Insert"
        End If


        arrImage = Nothing
        ms = Nothing
        strSql = Nothing
End Sub

Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
        With dlg
            .Filter = "All Files|*.*|Bitmap|*.bmp|GIF|*.gif|Icon|*.ico|JPEG|*.jpg|PNG|*.png"
            .FilterIndex = 5
        End With

        If dlg.ShowDialog() = DialogResult.OK Then pic.Image = Image.FromFile(dlg.FileName)
End Sub

Public Sub setconnection()
        Try
            conn = New SqlConnection("Data Source=MyServer;Initial Catalog=TestDB;User Id=sa;Password=;")
            conn.Open()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
End Sub

Public Sub closeconnection()
    	conn.Close()
End Sub

Public Function CreateCommand(ByVal query As String) As SqlCommand
    	setconnection()
    	Dim command As New SqlCommand(query, conn)
    	Return command
End Function

Public Function CreateReader(ByVal query As String) As SqlDataReader
    	Dim reader As SqlDataReader
    	setconnection()
    	cmd = CreateCommand(query)
    	reader = cmd.ExecuteReader()
    	Return reader
End Function

AnswerRe: Problem in saving image to database from picturebox. VB.Net 2008. Framework 3.5. Pin
Luc Pattyn20-Aug-10 3:42
sitebuilderLuc Pattyn20-Aug-10 3:42 
QuestionRe: Problem in saving image to database from picturebox. VB.Net 2008. Framework 3.5. Pin
priyamtheone21-Aug-10 2:30
priyamtheone21-Aug-10 2:30 
AnswerRe: Problem in saving image to database from picturebox. VB.Net 2008. Framework 3.5. Pin
Luc Pattyn21-Aug-10 10:56
sitebuilderLuc Pattyn21-Aug-10 10:56 
QuestionHow Barcode Reader Read Image Pin
Anubhava Dimri19-Aug-10 19:07
Anubhava Dimri19-Aug-10 19:07 
AnswerRe: How Barcode Reader Read Image Pin
riced19-Aug-10 22:48
riced19-Aug-10 22:48 
QuestionDatagridview Column Pin
C#Coudou18-Aug-10 20:12
C#Coudou18-Aug-10 20:12 
AnswerRe: Datagridview Column Pin
Andy_L_J19-Aug-10 20:00
Andy_L_J19-Aug-10 20:00 
Questioninstalling to older versions of Windows Pin
RyJaBy18-Aug-10 8:25
RyJaBy18-Aug-10 8:25 
AnswerRe: installing to older versions of Windows Pin
Dave Kreskowiak18-Aug-10 9:08
mveDave Kreskowiak18-Aug-10 9:08 
AnswerRe: installing to older versions of Windows Pin
Tom Deketelaere19-Aug-10 2:30
professionalTom Deketelaere19-Aug-10 2:30 
GeneralRe: installing to older versions of Windows Pin
RyJaBy19-Aug-10 2:42
RyJaBy19-Aug-10 2:42 
QuestionDetecting selected text Pin
Pasan14817-Aug-10 7:21
Pasan14817-Aug-10 7:21 
AnswerRe: Detecting selected text Pin
Dave Kreskowiak17-Aug-10 14:41
mveDave Kreskowiak17-Aug-10 14:41 
AnswerRe: Detecting selected text Pin
Tom Foswick24-Aug-10 22:45
Tom Foswick24-Aug-10 22:45 
QuestionTextBox_Leave event only fired when move mouse to other control in vb.net 2005 Pin
Andraw Tang17-Aug-10 6:19
Andraw Tang17-Aug-10 6:19 
AnswerRe: TextBox_Leave event only fired when move mouse to other control in vb.net 2005 Pin
riced17-Aug-10 7:23
riced17-Aug-10 7:23 
GeneralRe: TextBox_Leave event only fired when move mouse to other control in vb.net 2005 Pin
Andraw Tang17-Aug-10 8:12
Andraw Tang17-Aug-10 8:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.