can anyone,plz
send m the code for retreiving & updating images onto the sql-server using vb.net...ASAP...
regards,
preetham......

Dani AI

Generated

Short note for the thread — practical fixes and a minimal, safer pattern to save/retrieve images from SQL Server with VB.NET.

— that UnauthorizedAccessException almost always means a filesystem permissions/path problem (ASP.NET process account can't open C:\pic, or the code uses FileMode.OpenOrCreate with FileAccess.Read which is a mismatch). If this is a web app use Server.MapPath to point inside the app (for example App_Data) and grant read/write to the app pool identity. Always use Using blocks so streams and connections are closed.

A concise upload pattern (different from a DataAdapter/DataSet approach):

Dim bytes() As Byte = File.ReadAllBytes(Server.MapPath("~/App_Data/uploads/myimage.jpg"))
Using cn As New SqlConnection(connString)
  Using cmd As New SqlCommand("UPDATE table2 SET logo=@img, MimeType=@mime WHERE Id=@id", cn)
    cmd.Parameters.Add("@img", SqlDbType.VarBinary, -1).Value = bytes
    cmd.Parameters.Add("@mime", SqlDbType.NVarChar, 50).Value = "image/jpeg"
    cmd.Parameters.Add("@id", SqlDbType.Int).Value = 1
    cn.Open()
    cmd.ExecuteNonQuery()
  End Using
End Using

Serve the image (ASP.NET) or load into a WinForms PictureBox:

' ASP.NET: write to response
Response.ContentType = "image/jpeg"
Response.BinaryWrite(CType(bytes, Byte()))
Response.End()

' WinForms: load from DB bytes
Using ms As New IO.MemoryStream(bytes)
  PictureBox1.Image = Image.FromStream(ms)
End Using

Practical tips: use varbinary(max) (the old IMAGE type is deprecated); store MIME type; avoid storing huge files directly in rows for web-scale sites — either store paths on disk, use SQL FILESTREAM/FileTable for large binary content, or stream large blobs. Add parameterized queries to avoid SQL injection. Credit to and for pointing to examples earlier in the thread; the key missing pieces here are folder permissions, correct FileMode/FileAccess, and using parameterized SqlCommand with proper disposal.

Recommended Answers

All 7 Replies

Check this thread

Next time try searching the forum's before posting your queries. :)

Check this thread

Next time try searching the forum's before posting your queries. :)

--------------
hello boss,Only after rigrously searching for long time-i posted on this forum...
hello boss,first try to answer the queries(don't copy&paste 4m some others post)...

commented: have some respect for those who help you. -3

Hello boss, did you check the link?

hello boss, i wasn't copying and pasting the code.(:D). I am just guiding you to the thread which already existed. looks like you haven't even taken a bit of an effort to search it in this forum.

Hi,

u can also check this

Regards
Veena

hai veena,

I tried even that also-it gives an error& now im trying this stuff-but gives an error called-"unauthorisedAccess Exception was unhandled by user code"
----------------
And below is m code for button1_click(to save image )


Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim con As String = ConfigurationSettings.AppSettings("preeconn1")
Dim str As String = "select * from table2"
Dim da As New SqlDataAdapter(str, con)
Dim cmd As New SqlCommand(str, New SqlConnection(con))
Dim MyCB As SqlCommandBuilder = New SqlCommandBuilder(da)
Dim ds As New DataSet()
da.MissingSchemaAction = MissingSchemaAction.AddWithKey
cmd.Connection.Open()
da.Fill(ds, "table2")
Dim fs As New FileStream("c:/pic/imgad[2].jpg", FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite)
Dim MyData(fs.Length) As Byte
fs.Read(MyData, 0, fs.Length)
fs.Close()
ds.Tables("table2").Rows(0)("logo") = MyData
da.Update(ds, "table2")
fs = Nothing
MyCB = Nothing
ds = Nothing
da = Nothing
cmd.Connection.Close()
con = Nothing
Response.Write("File saved to database")
End Sub

commented: Use code tags! +0

Do you want it in Asp.net or Windows application?

Check the link that i posted and check the last post. I have attached a sample application. All you have to do is change the connection string to the Sql Server and it should work.

l am grateful for ur help l really appreciate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.