storing an image
storing an image
storing an image
storing an image
storing an image
storing an image storing an image storing an image storing an image storing an image storing an image storing an image storing an image
storing an image storing an image
storing an image
Go Back  Xtreme Visual Basic Talk > > > storing an image


Reply
 
Thread Tools Display Modes
  #1  
Old 10-06-2004, 01:53 PM
brendalisalowe brendalisalowe is offline
Centurion
 
Join Date: Aug 2004
Posts: 159
Default storing an image


I am having touble storing an image to SQL Server. This is what I have right now:
Code:
Dim SqlConn As New SqlConnection(strConnectionString) Dim fs As New FileStream(Trim(txtImagePath.Text), FileMode.Open) Dim Data() As Byte = New [Byte](fs.Length) {} fs.Read(Data, 0, fs.Length) Dim SqlCommand As New SqlCommand("INSERT INTO tblCapRec (Contract) VALUES ([COLOR=Red]""[/COLOR]) " & _ "WHERE CaseNumber = '" & g_CaseNum & "' AND PartNumber = " & g_PartNum & "") If Trim(txtImagePath.Text) = "" Then MessageBox.Show("No Image Selected!", "Message Box", MessageBoxButtons.OK, MessageBoxIcon.Warning) Exit Sub End If Try SqlConn.Open() SqlCommand.Connection = SqlConn SqlCommand.ExecuteNonQuery() Catch ex As SqlException MessageBox.Show(ex.Message) Exit Sub Finally If SqlConn.State = ConnectionState.Open Then SqlConn.Close() End If fs.Close() End Try
I don't know what to put in the Value for my SQL statement. Does anyone know?
__________________
Brenda

If it weren't for you guys, where would I be?

Last edited by brendalisalowe; 10-07-2004 at 01:18 PM.
Reply With Quote
  #2  
Old 10-07-2004, 10:38 AM
brendalisalowe brendalisalowe is offline
Centurion
 
Join Date: Aug 2004
Posts: 159
Default

Does anyone know? I still haven't figured it out. Thanks!
__________________
Brenda

If it weren't for you guys, where would I be?
Reply With Quote
  #3  
Old 10-07-2004, 10:39 AM
GMan_NC's Avatar
GMan_NCstoring an image GMan_NC is offline
Proud Daddy

Forum Leader
* Expert *
 
Join Date: Sep 2002
Location: North Carolina, USA
Posts: 1,962
Default

Try putting Data as the value
__________________
A morning without coffee is like something without something else.
Reply With Quote
  #4  
Old 10-07-2004, 10:43 AM
brendalisalowe brendalisalowe is offline
Centurion
 
Join Date: Aug 2004
Posts: 159
Default

How? Like (" & Data & ")?
__________________
Brenda

If it weren't for you guys, where would I be?
Reply With Quote
  #5  
Old 10-07-2004, 10:46 AM
GMan_NC's Avatar
GMan_NCstoring an image GMan_NC is offline
Proud Daddy

Forum Leader
* Expert *
 
Join Date: Sep 2002
Location: North Carolina, USA
Posts: 1,962
Default

Yes
Code:
Dim SqlCommand As New SqlCommand("INSERT INTO tblCapRec (Contract) VALUES (" & Data & ") " & _ "WHERE CaseNumber = '" & g_CaseNum & "' AND PartNumber = " & g_PartNum & "")
__________________
A morning without coffee is like something without something else.
Reply With Quote
  #6  
Old 10-07-2004, 11:04 AM
brendalisalowe brendalisalowe is offline
Centurion
 
Join Date: Aug 2004
Posts: 159
Default

It doesn't like that. Any other ideas?
__________________
Brenda

If it weren't for you guys, where would I be?
Reply With Quote
  #7  
Old 10-07-2004, 12:54 PM
brendalisalowe brendalisalowe is offline
Centurion
 
Join Date: Aug 2004
Posts: 159
Default

I figured it out! I need to use an UPDATE instead of INSERT. I should have known. Thanks for your help though!
__________________
Brenda

If it weren't for you guys, where would I be?
Reply With Quote
  #8  
Old 10-07-2004, 01:19 PM
brendalisalowe brendalisalowe is offline
Centurion
 
Join Date: Aug 2004
Posts: 159
Default

One more question. Is there a way to view the file in a PictureBox before I upload it?
__________________
Brenda

If it weren't for you guys, where would I be?
Reply With Quote
  #9  
Old 10-07-2004, 01:28 PM
GMan_NC's Avatar
GMan_NCstoring an image GMan_NC is offline
Proud Daddy

Forum Leader
* Expert *
 
Join Date: Sep 2002
Location: North Carolina, USA
Posts: 1,962
Default

Yes there is.

Code:
'assuming pb1 is your picturebox pb1.Image = Image.FromFile(txtImagePath.Text) 'stretch/shrink the image to fit pb1.SizeMode = PictureBoxSizeMode.StretchImage
__________________
A morning without coffee is like something without something else.
Reply With Quote
  #10  
Old 10-07-2004, 01:34 PM
brendalisalowe brendalisalowe is offline
Centurion
 
Join Date: Aug 2004
Posts: 159
Default

So something like this:
Code:
Dim OpenFileDialog As New OpenFileDialog() With OpenFileDialog .InitialDirectory = "C:\\Server_pc\C\Brenda\" .Filter = "PDF (*.pdf)|*.pdf|All files (*.*)|*.*" End With OpenFileDialog.ShowDialog() txtImagePath.Text = OpenFileDialog.FileName pbImage.Visible = True pbImage.Image = Image.FromFile(txtImagePath.Text) pbImage.SizeMode = PictureBoxSizeMode.StretchImage
But it doens't like that. Any ideas? It says out of memory. It is only a 15KB file.
__________________
Brenda

If it weren't for you guys, where would I be?
Reply With Quote
  #11  
Old 10-07-2004, 01:40 PM
GMan_NC's Avatar
GMan_NCstoring an image GMan_NC is offline
Proud Daddy

Forum Leader
* Expert *
 
Join Date: Sep 2002
Location: North Carolina, USA
Posts: 1,962
Default

hmmm, looks just similar to my code. This works great for me.
Code:
With fd1 'openfiledialog .InitialDirectory = "C:\Documents and Settings\Administrator\My Documents\My Pictures\" .Filter = "All Files|*.*|Bitmaps|*.bmp|GIFs|*.gif|JPEGs|*.jpg" .FilterIndex = 4 End With If fd1.ShowDialog() = DialogResult.OK Then With pb1 .Image = Image.FromFile(fd1.FileName) .SizeMode = PictureBoxSizeMode.StretchImage .BorderStyle = BorderStyle.Fixed3D End With txtFileName.Text = fd1.FileName End If
__________________
A morning without coffee is like something without something else.
Reply With Quote
  #12  
Old 10-07-2004, 01:45 PM
brendalisalowe brendalisalowe is offline
Centurion
 
Join Date: Aug 2004
Posts: 159
Default

On this:

.Image = Image.FromFile(OpenFileDialog.FileName)

It says "Out of Memory." It is a .PDF file though.

Why?
__________________
Brenda

If it weren't for you guys, where would I be?
Reply With Quote
  #13  
Old 10-07-2004, 01:49 PM
GMan_NC's Avatar
GMan_NCstoring an image GMan_NC is offline
Proud Daddy

Forum Leader
* Expert *
 
Join Date: Sep 2002
Location: North Carolina, USA
Posts: 1,962
Default

Well because a .PDF file is not an Image file.
__________________
A morning without coffee is like something without something else.
Reply With Quote
  #14  
Old 10-07-2004, 01:51 PM
brendalisalowe brendalisalowe is offline
Centurion
 
Join Date: Aug 2004
Posts: 159
Default

What is it then? When I inserted into SQL Server, the column should be Image right?
__________________
Brenda

If it weren't for you guys, where would I be?
Reply With Quote
  #15  
Old 10-07-2004, 01:57 PM
GMan_NC's Avatar
GMan_NCstoring an image GMan_NC is offline
Proud Daddy

Forum Leader
* Expert *
 
Join Date: Sep 2002
Location: North Carolina, USA
Posts: 1,962
Default

Just because you save a Document file to an Image datatype in the table, does not convert it to an Image. You CAN save the .PDF to the database in a binary field and view it using some kind of .PDF file viewer.
__________________
A morning without coffee is like something without something else.
Reply With Quote
  #16  
Old 10-07-2004, 02:05 PM
brendalisalowe brendalisalowe is offline
Centurion
 
Join Date: Aug 2004
Posts: 159
Default

OK, That makes sense. Question though. On my download button, I have this so far:
Code:
Dim SqlConn As New SqlConnection(strConnectionString) SqlConn.Open() Dim SqlCommand As New SqlCommand("SELECT Contract FROM tblCapRec " & _ "WHERE CaseNumber = '" & g_CaseNum & "' AND PartNumber = " & g_PartNum & "") SqlCommand.CommandType = CommandType.Text SqlCommand.Connection = SqlConn Dim da As New SqlDataAdapter(SqlCommand) Dim ds As New DataSet() da.Fill(ds) Dim bits As Byte() = CType(ds.Tables(0).Rows(0).Item(0), Byte()) Dim memorybits As New MemoryStream(bits)
Where do I go from there to save it in a file somewhere, in a place I choose? I love that you know so much!
__________________
Brenda

If it weren't for you guys, where would I be?
Reply With Quote
  #17  
Old 10-07-2004, 02:22 PM
GMan_NC's Avatar
GMan_NCstoring an image GMan_NC is offline
Proud Daddy

Forum Leader
* Expert *
 
Join Date: Sep 2002
Location: North Carolina, USA
Posts: 1,962
Default

You could try something like this
Code:
Dim bits As Byte() = CType(ds.Tables(0).Rows(0).Item(0), Byte()) Dim fs As New FileStream("c:\YourPDF.PDF", FileMode.CreateNew) Dim bw As New BinaryWriter(fs) bw.Write(bits) fs.Close() bw.close


*I'm getting offline now, I'll be back tomorrow*
__________________
A morning without coffee is like something without something else.
Reply With Quote
  #18  
Old 10-07-2004, 02:38 PM
brendalisalowe brendalisalowe is offline
Centurion
 
Join Date: Aug 2004
Posts: 159
Default

WOW, you are good. One more question though. How do I make it so I can pick the place where I save it and what I want to name it? SaveDialogFile?

This is what I have now:
Code:
Dim SqlConn As New SqlConnection(strConnectionString) Dim strSQL As String = ("SELECT Contract FROM tblCapRec " & _ "WHERE CaseNumber = '" & g_CaseNum & "' AND PartNumber = " & g_PartNum & "") Dim SqlCommand As New SqlCommand(strSQL, SqlConn) Dim da As New SqlDataAdapter(SqlCommand) Dim ds As New DataSet() da.Fill(ds) Dim bits As Byte() = CType(ds.Tables(0).Rows(0).Item(0), Byte()) Dim fs As New FileStream("c:\YourPDF.PDF", FileMode.CreateNew) Dim bw As New BinaryWriter(fs) bw.Write(bits) fs.Close() bw.Close() Me.Close()
__________________
Brenda

If it weren't for you guys, where would I be?
Reply With Quote
  #19  
Old 10-08-2004, 06:31 AM
GMan_NC's Avatar
GMan_NCstoring an image GMan_NC is offline
Proud Daddy

Forum Leader
* Expert *
 
Join Date: Sep 2002
Location: North Carolina, USA
Posts: 1,962
Default

Yes you can use the SaveFileDialog.

Code:
If sfd.ShowDialog() <> DialogResult.Cancel Then Dim strFileName as String = sfd.FileName End If

Then use strFileName in place of "c:\YourPDF.PDF"
__________________
A morning without coffee is like something without something else.
Reply With Quote
  #20  
Old 10-08-2004, 09:00 AM
brendalisalowe brendalisalowe is offline
Centurion
 
Join Date: Aug 2004
Posts: 159
Default

Can anyone figure out why my Form closes after I do this:
Code:
Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click Dim ofd As New OpenFileDialog() With ofd .InitialDirectory = "C:\Scans\" .Filter = "PDF (*.pdf)|*.pdf|All files (*.*)|*.*" .FilterIndex = 4 End With If ofd.ShowDialog() = DialogResult.OK Then txtImagePath.Text = ofd.FileName End If End Sub

I can't see why.
__________________
Brenda

If it weren't for you guys, where would I be?
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
storing an image
storing an image
storing an image storing an image
storing an image
storing an image
storing an image storing an image storing an image storing an image storing an image storing an image storing an image
storing an image
storing an image
 
storing an image
storing an image
 
-->