Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Save Image in SQL Database

Save Image in SQL Database

Scheduled Pinned Locked Moved Database
databasehelp
43 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B bapu2889

    Hello sir thanks for promt rep. yes i have change the logic the way you said but still it's same problem i mean i can save or delete image from sql database but i have to close application to see the changes and one more thing i would like to tell you that i have created another application without image but it works fine if i add or delete first name and last name it shows the changes same time and i have used same method with this application but i am not sure but i sow one difference in it that project has one table with three columns ID, FirstName, LastName and this is button event to change enable = false

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Button2.Enabled = False
    Button3.Enabled = False : Button4.Enabled = False : Button5.Enabled = False
    Button1.Enabled = True
    TextBox1.Enabled = True : TextBox2.Enabled = True
    TextBox1.BackColor = Color.Aquamarine : TextBox2.BackColor = Color.Aquamarine
    Try
    Me.StudentBindingSource.EndEdit()
    Me.StudentBindingSource.AddNew()
    Catch ex As Exception
    MsgBox(ex.Message)
    End Try
    End

    Sub but with this there is only one difference

    Me.StudentBindingSource.EndEdit()
    Me.StudentBindingSource.AddNew()

    so do you think this is the issue waiting for your kind rep. have a nice time thanks again

    W Offline
    W Offline
    Wendelius
    wrote on last edited by
    #12

    If you have corresponding BeginInit somewhere, you should call EndInit. Trying that is a good idea. Also one possibility is that even if you delete the image from database, it isn't correctly refreshed in the dataset (dsImage). However you do call Clear-method so it should work. If calling EndInit doesn't help, can you re-create the dataset (set dsImage to Nothing and create a new dsImage) and fill it after that.

    B 1 Reply Last reply
    0
    • W Wendelius

      If you have corresponding BeginInit somewhere, you should call EndInit. Trying that is a good idea. Also one possibility is that even if you delete the image from database, it isn't correctly refreshed in the dataset (dsImage). However you do call Clear-method so it should work. If calling EndInit doesn't help, can you re-create the dataset (set dsImage to Nothing and create a new dsImage) and fill it after that.

      B Offline
      B Offline
      bapu2889
      wrote on last edited by
      #13

      hello again as you said that to refresh dsimage so for example if there is 4 images in database and if i delete image no 3 so after deleting image it's refreshing dataset because in picture box it shows first image but still you can go forward or backword up to image 4 but when i close application and rerun it then it shows 3 images in database so that means it refreshing dataset properly and there is no corresponding in begininit at all so i think i dont need to endinit so we have only last option so i think i will try tomorrow but any way i learn lot from you i realy appriciate that thanks a lot :) if i need any help i will contact you tomorrow good night

      W 1 Reply Last reply
      0
      • B bapu2889

        hello again as you said that to refresh dsimage so for example if there is 4 images in database and if i delete image no 3 so after deleting image it's refreshing dataset because in picture box it shows first image but still you can go forward or backword up to image 4 but when i close application and rerun it then it shows 3 images in database so that means it refreshing dataset properly and there is no corresponding in begininit at all so i think i dont need to endinit so we have only last option so i think i will try tomorrow but any way i learn lot from you i realy appriciate that thanks a lot :) if i need any help i will contact you tomorrow good night

        W Offline
        W Offline
        Wendelius
        wrote on last edited by
        #14

        You're welcome. :) I'll go and have some sleep also... :zzz: One thing you can try tomorrow is to add AcceptChanges()- call to dataset after deletion or fill. In case that the record is deleted but changes are still pending MIka

        B 1 Reply Last reply
        0
        • W Wendelius

          You're welcome. :) I'll go and have some sleep also... :zzz: One thing you can try tomorrow is to add AcceptChanges()- call to dataset after deletion or fill. In case that the record is deleted but changes are still pending MIka

          B Offline
          B Offline
          bapu2889
          wrote on last edited by
          #15

          Hello sir good morning how are you I have tried every thing but still it's same problem so now I don't know what to do I have also tried AcceptChanges() but still it's same waiting for your kind rep. :confused:

          A 1 Reply Last reply
          0
          • B bapu2889

            Hello sir good morning how are you I have tried every thing but still it's same problem so now I don't know what to do I have also tried AcceptChanges() but still it's same waiting for your kind rep. :confused:

            A Offline
            A Offline
            Ashfield
            wrote on last edited by
            #16

            I couldn't be bothered to read all the posts, but if the data is actually being deleted in the database (check from Management Studio) then you need to refresh your dataset and rebind it to your control. Hope this helps

            Bob Ashfield Consultants Ltd

            B 1 Reply Last reply
            0
            • A Ashfield

              I couldn't be bothered to read all the posts, but if the data is actually being deleted in the database (check from Management Studio) then you need to refresh your dataset and rebind it to your control. Hope this helps

              Bob Ashfield Consultants Ltd

              B Offline
              B Offline
              bapu2889
              wrote on last edited by
              #17

              hello sir could you please guide me how to do this because i have just started sql so i dont know much about it i mean i dont want code but just want to know where to start :confused: waiging for your kind rep. have a nice day

              A 1 Reply Last reply
              0
              • B bapu2889

                hello sir could you please guide me how to do this because i have just started sql so i dont know much about it i mean i dont want code but just want to know where to start :confused: waiging for your kind rep. have a nice day

                A Offline
                A Offline
                Ashfield
                wrote on last edited by
                #18

                Its not in sql, its in your code. I assume yuo load data from the database into a dataset and then bind the datatable(s) within the dataset to controls. When you add or delete data you need to probably (depending on how you update your database) refresh your dataset and/or rebind the datatable(s) to the controls.

                Bob Ashfield Consultants Ltd

                B 1 Reply Last reply
                0
                • A Ashfield

                  Its not in sql, its in your code. I assume yuo load data from the database into a dataset and then bind the datatable(s) within the dataset to controls. When you add or delete data you need to probably (depending on how you update your database) refresh your dataset and/or rebind the datatable(s) to the controls.

                  Bob Ashfield Consultants Ltd

                  B Offline
                  B Offline
                  bapu2889
                  wrote on last edited by
                  #19

                  hello again this is the code for add image

                  btnSave.Enabled = False
                  'get sql connection
                  Dim ImageAdd As Integer

                      Try
                          conImage = GetDBConnection()
                          dsImage.BeginInit()
                  
                          Dim sSQL As String = "INSERT INTO Images (Pic,Title, IType, Height, Width) VALUES(" & \_
                                         "@pic, @title, @itype, @iheight, @iwidth)"
                          commImage = New Data.SqlClient.SqlCommand(sSQL, conImage)
                          Call GetImage()
                          ImageAdd = commImage.ExecuteNonQuery()
                          Me.daImage.Update(Me.dsImage, "Images")
                  
                          dsImage.AcceptChanges()
                          MessageBox.Show(ImageAdd.ToString & "  Image successfuly saved in database", "Image Load")
                          dsImage.EndInit()
                  
                      Catch ex As Exception
                          MsgBox(ex.Message)
                  
                      End Try
                      commImage.Dispose()
                      ' commImage = Nothing
                      conImage.Close()
                      conImage.Dispose()
                      Call LoadImages()
                  End Sub
                  

                  and when ever i add or delete image it refresh the the dataset i mean if ther is 3 images in database and if i want to delete image no. 3 after deleting image 3 it shows image no. 1 in picture that means it's reloading image that is what i am thinking :confused:

                  W 1 Reply Last reply
                  0
                  • B bapu2889

                    hello again this is the code for add image

                    btnSave.Enabled = False
                    'get sql connection
                    Dim ImageAdd As Integer

                        Try
                            conImage = GetDBConnection()
                            dsImage.BeginInit()
                    
                            Dim sSQL As String = "INSERT INTO Images (Pic,Title, IType, Height, Width) VALUES(" & \_
                                           "@pic, @title, @itype, @iheight, @iwidth)"
                            commImage = New Data.SqlClient.SqlCommand(sSQL, conImage)
                            Call GetImage()
                            ImageAdd = commImage.ExecuteNonQuery()
                            Me.daImage.Update(Me.dsImage, "Images")
                    
                            dsImage.AcceptChanges()
                            MessageBox.Show(ImageAdd.ToString & "  Image successfuly saved in database", "Image Load")
                            dsImage.EndInit()
                    
                        Catch ex As Exception
                            MsgBox(ex.Message)
                    
                        End Try
                        commImage.Dispose()
                        ' commImage = Nothing
                        conImage.Close()
                        conImage.Dispose()
                        Call LoadImages()
                    End Sub
                    

                    and when ever i add or delete image it refresh the the dataset i mean if ther is 3 images in database and if i want to delete image no. 3 after deleting image 3 it shows image no. 1 in picture that means it's reloading image that is what i am thinking :confused:

                    W Offline
                    W Offline
                    Wendelius
                    wrote on last edited by
                    #20

                    Hi again, Few additional questions - about the binding. Have you bound the dsImage dataset to a BindingSource and again a picture box to that BindingSource? - in LoadImages sub, what is the difference between ImagesTableAdapter and daImage? - in INSERT statement, you don't specify a value for ImageID-column, but that column is used in DELETE statement. How the ImageID get's it's value

                    B 1 Reply Last reply
                    0
                    • W Wendelius

                      Hi again, Few additional questions - about the binding. Have you bound the dsImage dataset to a BindingSource and again a picture box to that BindingSource? - in LoadImages sub, what is the difference between ImagesTableAdapter and daImage? - in INSERT statement, you don't specify a value for ImageID-column, but that column is used in DELETE statement. How the ImageID get's it's value

                      B Offline
                      B Offline
                      bapu2889
                      wrote on last edited by
                      #21

                      hello again how are you thanks for your rep. yes i have dound picturebox to binding source , and i am trying so many things so i add imagestable adapter but daImage is i have drag and drop on form and went through adapter wizard and when i created Image table i have set primary key and identity specification set to yes so every time when i add new image to database it increment 1 by it self and in delete statement i have find record to delete it so it gets the ImageID from textbox all this is works fine but you know the problem so now i have no idea what to do :confused: waiting for your kind rep. and thanks again

                      W 1 Reply Last reply
                      0
                      • B bapu2889

                        hello again how are you thanks for your rep. yes i have dound picturebox to binding source , and i am trying so many things so i add imagestable adapter but daImage is i have drag and drop on form and went through adapter wizard and when i created Image table i have set primary key and identity specification set to yes so every time when i add new image to database it increment 1 by it self and in delete statement i have find record to delete it so it gets the ImageID from textbox all this is works fine but you know the problem so now i have no idea what to do :confused: waiting for your kind rep. and thanks again

                        W Offline
                        W Offline
                        Wendelius
                        wrote on last edited by
                        #22

                        Hmm, let's try to simplify this. Can you create a button and in that button call this sub (it may contain compilation errors since I wrote it in notepad). Let's see what you get with this:

                        Private Sub TestMethod()
                        Dim imageCount As Integer = dsImage.Images.Rows.Count

                        MsgBox("Original image count in dataset: " & CStr(imageCount))
                        conImage = GetDBConnection()
                        commImage = New Data.SqlClient.SqlCommand("DELETE FROM Images WHERE ImageID = " & txtImageFile.Text, conImage)
                        If commImage.ExecuteNonQuery() <> 1 Then
                        MsgBox("Delete failed", MsgBoxStyle.Information)
                        End If
                        dsImage.Images.Clear()
                        dsImage.Images.AcceptChanges()
                        If dsImage.Images.Rows.Count <> 0 Then
                        MsgBox("Datatable not empty", MsgBoxStyle.Information)
                        End If
                        Me.ImagesTableAdapter.ClearBeforeFill = True
                        Me.ImagesTableAdapter.Fill(Me.dsImage.Images)
                        If (imagecount - 1 <> dsImage.Images.Rows.Count) Then
                        MsgBox("Delete not done, rows in datatable: " & CStr(dsImage.Images.Rows.Count), MsgBoxStyle.Information)
                        End If
                        End Sub

                        What do you get as result (information in messageboxes)?

                        B 1 Reply Last reply
                        0
                        • W Wendelius

                          Hmm, let's try to simplify this. Can you create a button and in that button call this sub (it may contain compilation errors since I wrote it in notepad). Let's see what you get with this:

                          Private Sub TestMethod()
                          Dim imageCount As Integer = dsImage.Images.Rows.Count

                          MsgBox("Original image count in dataset: " & CStr(imageCount))
                          conImage = GetDBConnection()
                          commImage = New Data.SqlClient.SqlCommand("DELETE FROM Images WHERE ImageID = " & txtImageFile.Text, conImage)
                          If commImage.ExecuteNonQuery() <> 1 Then
                          MsgBox("Delete failed", MsgBoxStyle.Information)
                          End If
                          dsImage.Images.Clear()
                          dsImage.Images.AcceptChanges()
                          If dsImage.Images.Rows.Count <> 0 Then
                          MsgBox("Datatable not empty", MsgBoxStyle.Information)
                          End If
                          Me.ImagesTableAdapter.ClearBeforeFill = True
                          Me.ImagesTableAdapter.Fill(Me.dsImage.Images)
                          If (imagecount - 1 <> dsImage.Images.Rows.Count) Then
                          MsgBox("Delete not done, rows in datatable: " & CStr(dsImage.Images.Rows.Count), MsgBoxStyle.Information)
                          End If
                          End Sub

                          What do you get as result (information in messageboxes)?

                          B Offline
                          B Offline
                          bapu2889
                          wrote on last edited by
                          #23

                          hello again sorry for late i have test this and result are as bellow WITH 0 DATA COUNT FIRST MESSAGEBOX 1- Original Count in Database = 0 2- Error because there is no row in database so it shows error because of this

                          If commImage.ExecuteNonQuery() <> 1 Then
                          MsgBox("Delete failed", MsgBoxStyle.Information)
                          End If

                          after that i close application and rerun it and add one image in database so FIRST MSGBOX 1- Original Count in DB = 1 SECOND MSGBOX 2- Delete failed THIRD MSGBOX 3- Delete not done, Rows in datatable = 1 thanks again waiting for your rep.

                          W 1 Reply Last reply
                          0
                          • B bapu2889

                            hello again sorry for late i have test this and result are as bellow WITH 0 DATA COUNT FIRST MESSAGEBOX 1- Original Count in Database = 0 2- Error because there is no row in database so it shows error because of this

                            If commImage.ExecuteNonQuery() <> 1 Then
                            MsgBox("Delete failed", MsgBoxStyle.Information)
                            End If

                            after that i close application and rerun it and add one image in database so FIRST MSGBOX 1- Original Count in DB = 1 SECOND MSGBOX 2- Delete failed THIRD MSGBOX 3- Delete not done, Rows in datatable = 1 thanks again waiting for your rep.

                            W Offline
                            W Offline
                            Wendelius
                            wrote on last edited by
                            #24

                            No problem... So the problem in this case was that there wasn't a row to delete matching the condition. Try to change the delete statement:

                            commImage = New Data.SqlClient.SqlCommand("DELETE FROM Images WHERE ImageID = (SELECT MAX(ImageID) FROM Images)", conImage)

                            This should delete the last image in images. Any difference?

                            B 1 Reply Last reply
                            0
                            • W Wendelius

                              No problem... So the problem in this case was that there wasn't a row to delete matching the condition. Try to change the delete statement:

                              commImage = New Data.SqlClient.SqlCommand("DELETE FROM Images WHERE ImageID = (SELECT MAX(ImageID) FROM Images)", conImage)

                              This should delete the last image in images. Any difference?

                              B Offline
                              B Offline
                              bapu2889
                              wrote on last edited by
                              #25

                              hello again ok with empty DB 1 msgbox Original image count = 0 2 msgbox delete failed 3 msgbox Delete not done, rows in BD =0 and without closing application I add one image with original method and it shows msgbox that 1 image is saved, and then without closing application i click button to use your method still it shows that first msgbox= original image count = 0 and in second = Delete not done row in datatable=0, and when i click button second time without closing application so first msgbox is original image count =0 second msgbox is delete failed therd msgbox is row i datatable=0 so that means image has been saved in database and deleted when i click button to use your method and when i click button second so result was different and then add one image and rerun the application so it show one image in database and i can see the image in picturebox as well now i click button for your method so msgbox 1 Original Image count =1 msgbox2 = delete not done, row in datatable = 1 now i have click button second time without closing application so msgbox 1 Original Image count =1 msgbox 2 Delete Failed msgbox 3 delete not done, row in datatable = 1 so this is some thing else i think we are going to the right direction thanks waiting for your kind rep.

                              W 1 Reply Last reply
                              0
                              • B bapu2889

                                hello again ok with empty DB 1 msgbox Original image count = 0 2 msgbox delete failed 3 msgbox Delete not done, rows in BD =0 and without closing application I add one image with original method and it shows msgbox that 1 image is saved, and then without closing application i click button to use your method still it shows that first msgbox= original image count = 0 and in second = Delete not done row in datatable=0, and when i click button second time without closing application so first msgbox is original image count =0 second msgbox is delete failed therd msgbox is row i datatable=0 so that means image has been saved in database and deleted when i click button to use your method and when i click button second so result was different and then add one image and rerun the application so it show one image in database and i can see the image in picturebox as well now i click button for your method so msgbox 1 Original Image count =1 msgbox2 = delete not done, row in datatable = 1 now i have click button second time without closing application so msgbox 1 Original Image count =1 msgbox 2 Delete Failed msgbox 3 delete not done, row in datatable = 1 so this is some thing else i think we are going to the right direction thanks waiting for your kind rep.

                                W Offline
                                W Offline
                                Wendelius
                                wrote on last edited by
                                #26

                                This is confusing... I thought that now the delete operation would have deleted 1 row. I think I must get some sleep but in the mean time if you could test this variation with one picture in database (this should delete ALL pictures):

                                commImage = New Data.SqlClient.SqlCommand("DELETE FROM Images)", conImage)
                                ...
                                If (dsImage.Images.Rows.Count > 0) Then
                                MsgBox("Delete not done, rows in datatable: " & CStr(dsImage.Images.Rows.Count), MsgBoxStyle.Information)
                                End If

                                I'll get back to this sometime tomorrow afternoon so don't hurry with this. If I'm reading the clock correctly it's past 10pm at your timezone so you could use some rest too. :) Mika

                                B 1 Reply Last reply
                                0
                                • W Wendelius

                                  This is confusing... I thought that now the delete operation would have deleted 1 row. I think I must get some sleep but in the mean time if you could test this variation with one picture in database (this should delete ALL pictures):

                                  commImage = New Data.SqlClient.SqlCommand("DELETE FROM Images)", conImage)
                                  ...
                                  If (dsImage.Images.Rows.Count > 0) Then
                                  MsgBox("Delete not done, rows in datatable: " & CStr(dsImage.Images.Rows.Count), MsgBoxStyle.Information)
                                  End If

                                  I'll get back to this sometime tomorrow afternoon so don't hurry with this. If I'm reading the clock correctly it's past 10pm at your timezone so you could use some rest too. :) Mika

                                  B Offline
                                  B Offline
                                  bapu2889
                                  wrote on last edited by
                                  #27

                                  Hello again yes you are right it's 10.30 pm now so better to rest and get back to work tomorrow but thank you very much for your help take care :)

                                  B 1 Reply Last reply
                                  0
                                  • B bapu2889

                                    Hello again yes you are right it's 10.30 pm now so better to rest and get back to work tomorrow but thank you very much for your help take care :)

                                    B Offline
                                    B Offline
                                    bapu2889
                                    wrote on last edited by
                                    #28

                                    hello again yes i have tried new quary but still it's not working waiting for your kind rep.

                                    W 1 Reply Last reply
                                    0
                                    • B bapu2889

                                      hello again yes i have tried new quary but still it's not working waiting for your kind rep.

                                      W Offline
                                      W Offline
                                      Wendelius
                                      wrote on last edited by
                                      #29

                                      Hi again, What was the output from these (rowcount)?

                                      MsgBox("Original image count in dataset: " & CStr(imageCount))
                                      ...
                                      If (dsImage.Images.Rows.Count > 0) Then
                                      MsgBox("Delete not done, rows in datatable: " & CStr(dsImage.Images.Rows.Count), MsgBoxStyle.Information)
                                      End If

                                      B 2 Replies Last reply
                                      0
                                      • W Wendelius

                                        Hi again, What was the output from these (rowcount)?

                                        MsgBox("Original image count in dataset: " & CStr(imageCount))
                                        ...
                                        If (dsImage.Images.Rows.Count > 0) Then
                                        MsgBox("Delete not done, rows in datatable: " & CStr(dsImage.Images.Rows.Count), MsgBoxStyle.Information)
                                        End If

                                        B Offline
                                        B Offline
                                        bapu2889
                                        wrote on last edited by
                                        #30

                                        hello sir how are you in messagebox 1st original image count = 2 2nd msgbox delete not done, rows in datatable = 2 and when i click ok it shows SqlException was unhandled "Incorrect syntax near '*' wanting for your kind rep.

                                        W 1 Reply Last reply
                                        0
                                        • B bapu2889

                                          hello sir how are you in messagebox 1st original image count = 2 2nd msgbox delete not done, rows in datatable = 2 and when i click ok it shows SqlException was unhandled "Incorrect syntax near '*' wanting for your kind rep.

                                          W Offline
                                          W Offline
                                          Wendelius
                                          wrote on last edited by
                                          #31

                                          Where is that SqlException coming from? After the "Delete not done..." message box there should be no code to execute (only End If). Another thing, the dataset is defined in the project, am I correct? Could you post the part that selects the images or do you create the SELECT statement?

                                          B 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups