.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> VB.Net >> Post New QuestionBookmark and Share Subscribe to Forum

update an image

Posted By: aamir     Posted Date: December 30, 2008    Points:2   Category :VB.Net
i have seen many article which show how to save an image into sql database but i haven't seen such an article which tell me how to update an opened image from database after applying an effect on it
so i have written small program to do this task
i create a table in Northwind database using tis code

Dim cmd As String = "create table Picture" & "(" & "PictureID int IDENTITY(0,1) NOT NULL PRIMARY KEY, " & "PictureName varchar(40) NOT NULL, " & "Picture Image NOT NULL" & ");"
Dim c As SqlConnection = New SqlConnection(c_string)
Dim cm As SqlCommand = New SqlCommand(cmd, c)

after creating table in the database,i save into that table using this code

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim dlg As OpenFileDialog = New OpenFileDialog()
dlg.Filter = "All Pictures|*.bmp;*.gif;*.jpg|Bitmaps|*.bmp|GIFs|*.gif|JPEGs|*.jpg"
If dlg.ShowDialog() = Windows.Forms.DialogResult.OK Then

PictureBox1.Image = New Bitmap(dlg.FileName)
Dim name As String = dlg.FileName.Substring(dlg.FileName.LastIndexOf("\") + 1, dlg.FileName.Length - dlg.FileName.LastIndexOf("\") - 1)

End If
Dim mstr As IO.MemoryStream = New IO.MemoryStream()
PictureBox1.Image.Save(mstr, PictureBox1.Image.RawFormat)
Dim arrImage As Byte() = mstr.GetBuffer()
Dim cmd As String = "insert into Picture (PictureName, Picture) values (@PName, @Pic)"
Dim c As SqlConnection = New SqlConnection(c_string)
Dim comm As SqlCommand = New SqlCommand(cmd, c)
comm.Parameters.Add(New SqlParameter("@PName", SqlDbType.VarChar, 40)).Value = Name
comm.Parameters.Add(New SqlParameter("@Pic", SqlDbType.Image)).Value = arrImage
Try

c.Open()
comm.ExecuteNonQuery()

Catch err As SqlException

MessageBox.Show(err.Message)

Finally

c.Close()

End Try

listBox1.Items.Add(Name)
End Sub

above code saves an image into the database
i have another button for Update
so i have written this code under its click event

Dim mstr As IO.MemoryStream = New IO.MemoryStream()
PictureBox1.Image.Save(mstr, PictureBox1.Image.RawFormat)
Dim arrImage As Byte() = mstr.GetBuffer()
Dim cmd As String = "insert into Picture (PictureName, Picture) values (@PName, @Pic)"
Dim c As SqlConnection = New SqlConnection(c_string)
Dim comm As SqlCommand = New SqlCommand(cmd, c)
comm.Parameters.Add(New SqlParameter("@PName", SqlDbType.VarChar, 40)).Value = Name
comm.Parameters.Add(New SqlParameter("@Pic", SqlDbType.Image)).Value = arrImage
Try

c.Open()
comm.ExecuteNonQuery()

Catch err As SqlException

MessageBox.Show(err.Message)

Finally

c.Close()

End Try


but it is not updating my image and comes this error
Value cannot be null.
Parameter name: encoder
on the following line

PictureBox1.Image.Save(mstr, PictureBox1.Image.RawFormat)


any body can help me??

Responses
Author: Amit Mehra             
Posted Date: December 30, 2008     Points: 5   

Hi Aamir,
Your statement showing you are doing insert operation and your requirement Update operation..am i correct?

Thanks,
Amit
Please "Mark As Answer " if this post help you.
Author: aamir             
Posted Date: December 31, 2008     Points: 5   

yes u right
i replaced insert with update,but still not succeeded

Dim mstr As IO.MemoryStream = New IO.MemoryStream()
PictureBox1.Image.Save(mstr, System.Drawing.Imaging.ImageFormat.Bmp)
Dim arrImage As Byte() = mstr.GetBuffer()
Dim cmd As String = "Update Picture SET PictureName,Picture=@Picture WHERE PictureID =@PictureID" & ListBox1.SelectedIndex & ";"
') values (@PName, @Pic)"
Dim c As SqlConnection = New SqlConnection(c_string)
Dim comm As SqlCommand = New SqlCommand(cmd, c)
'comm.Parameters.Add(New SqlParameter("@PName", SqlDbType.VarChar, 40)).Value = Name
comm.Parameters.Add(New SqlParameter("@Pic", SqlDbType.Image)).Value = arrImage
Try

c.Open()
comm.ExecuteNonQuery()

Catch err As SqlException

'MessageBox.Show(err.Message)

Finally

c.Close()

End Try


Am i doing right??



Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend