Hey Cor,
OK, I've copied the entire CM_Pic_Edit Sub to the bottom of this post. when
a user right-clicks on a picturebox (that has an image), the context menu
has a choice for editing the pic. I don't decide which program to use for
editing. when my app loads, I get all of the information regarding the
current users preferences from the registry.
So, this is the flow:
User right-clicks on picturebox and selects Edit
Create an SQL Statement related to whichever picturebox was the source
Open a connection to the DB
Use the SQLDataReader and a filestream to write the pic to disk
shell to whatever the users preferred editor for pics is
write the edited pic back to the DB
Delete the disk file
Using the OLEDBDataReader with a JetDB throws an exception if the pic isn't
actually in the DB (only in the picbox because the user didn't save/update
the DB yet)
Using the SQLDataReader with MSDE2K, in the same situation does not throw an
exception. It just writes an empty file to disk.
As I said, I'd like to understand why no exception is thrown with the SQL /
MSDE version, but is with the OLE / JetDB version. Once I understand why, it
should be relatively easy to fix. I already know I can check the size of the
disk file and if it's 0, I can throw my own exception. However, my
preference would be to handle this before I get to file creation.
Again, my guess is that it has something to do with the different ways the
JetDB and the SQL DB represent an empty field. DBNull is what causes the
exception to be thrown in the OLE / JetDB case. But I don't know what the
equivalent to a DBNull is in an SQL DB.
regarding the memorystream, I'm not sure a memorystream would work for
sending an image from the DB to an editor, having it edited, then getting it
back to the DB.
Here's the code:
Private Sub CM_Pic_Edit_Cli ck(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles CM_Pic_Edit.Cli ck
Dim pb, pm, pbfile, sql As String
sql = ""
pm = ""
pbfile = ""
If CM_Pics.SourceC ontrol Is BTS_Pic1 Then
pm = "Picture 1"
pb = "Pic1"
sql = "SELECT BTSDB." + pb + " FROM BTSDB Where
(BTSDB.BTSIndex ='" +
CStr(BP_BTS_Pic k.Splits(0).Dis playColumns("BT SIndex").DataCo lumn.Value) +
"')"
pbfile =
Environment.Get FolderPath(Envi ronment.Special Folder.Personal ) + "\" + pb +
".jpg"
ElseIf CM_Pics.SourceC ontrol Is BTS_Pic2 Then
pm = "Picture 2"
pb = "Pic2"
sql = "SELECT BTSDB." + pb + " FROM BTSDB Where
(BTSDB.BTSIndex ='" +
CStr(BP_BTS_Pic k.Splits(0).Dis playColumns("BT SIndex").DataCo lumn.Value) +
"')"
pbfile =
Environment.Get FolderPath(Envi ronment.Special Folder.Personal ) + "\" + pb +
".jpg"
ElseIf CM_Pics.SourceC ontrol Is BTS_Pic3 Then
pm = "Picture 3"
pb = "Pic3"
sql = "SELECT BTSDB." + pb + " FROM BTSDB Where
(BTSDB.BTSIndex ='" +
CStr(BP_BTS_Pic k.Splits(0).Dis playColumns("BT SIndex").DataCo lumn.Value) +
"')"
pbfile =
Environment.Get FolderPath(Envi ronment.Special Folder.Personal ) + "\" + pb +
".jpg"
End If
If CM_Pics.SourceC ontrol Is Sec_Pic1 Then
pm = "Picture 1"
pb = "Pic1"
sql = "SELECT SectorDB." + pb + " FROM SectorDB Where
(SectorDB.SECTO RIndex='" +
CStr(SI_Sector_ Pick.Splits(0). DisplayColumns( "SECTORIndex"). DataColumn.Valu e)
+ "')"
pbfile =
Environment.Get FolderPath(Envi ronment.Special Folder.Personal ) + "\" + pb +
".jpg"
ElseIf CM_Pics.SourceC ontrol Is Sec_Pic2 Then
pm = "Picture 2"
pb = "Pic2"
sql = "SELECT SectorDB." + pb + " FROM SectorDB Where
(SectorDB.SECTO RIndex='" +
CStr(SI_Sector_ Pick.Splits(0). DisplayColumns( "SECTORIndex"). DataColumn.Valu e)
+ "')"
pbfile =
Environment.Get FolderPath(Envi ronment.Special Folder.Personal ) + "\" + pb +
".jpg"
ElseIf CM_Pics.SourceC ontrol Is Sec_Pic3 Then
pm = "Picture 3"
pb = "Pic3"
sql = "SELECT SectorDB." + pb + " FROM SectorDB Where
(SectorDB.SECTO RIndex='" +
CStr(SI_Sector_ Pick.Splits(0). DisplayColumns( "SECTORIndex"). DataColumn.Valu e)
+ "')"
pbfile =
Environment.Get FolderPath(Envi ronment.Special Folder.Personal ) + "\" + pb +
".jpg"
End If
Dim conn As SqlClient.SqlCo nnection = New SqlClient.SqlCo nnection
conn.Connection String = XConn.Connectio nString
If conn.State <> ConnectionState .Open Then
conn.Open()
End If
Dim cmd As SqlClient.SqlCo mmand = New SqlClient.SqlCo mmand(sql,
conn)
Dim fs As FileStream
Dim bw As BinaryWriter
Dim bufferSize As Integer = 300000
Dim outbyte(300000 - 1) As Byte
Dim retval As Long
Dim startIndex As Long = 0
Try
Dim reader As SqlClient.SqlDa taReader =
cmd.ExecuteRead er(CommandBehav ior.SequentialA ccess)
reader.Read()
fs = New FileStream(pbfi le, FileMode.OpenOr Create,
FileAccess.Writ e)
bw = New BinaryWriter(fs )
startIndex = 0
retval = reader.GetBytes (0, 0, outbyte, 0, bufferSize)
bw.Write(outbyt e)
bw.Flush()
bw.Close()
fs.Close()
reader.Close()
If conn.State = ConnectionState .Open Then
conn.Close()
conn.Dispose()
End If
Dim go_edit As String
go_edit = CM_Pic_EditC(1) + " " + """" + pbfile + """"
Shell(go_edit, AppWinStyle.Nor malFocus, True)
fs = New FileStream(pbfi le, FileMode.OpenOr Create,
FileAccess.Read )
Dim rawData() As Byte = New Byte(CInt(fs.Le ngth)) {}
fs.Read(rawData , 0, System.Convert. ToInt32(fs.Leng th))
fs.Close()
If CM_Pics.SourceC ontrol Is BTS_Pic1 Then
_XBTS.DataTable .Rows.Find(BP_B TS_Pick.Splits( 0).DisplayColum ns("BTSIndex"). DataColumn.Valu e).Item("Pic1")
= rawData
ElseIf CM_Pics.SourceC ontrol Is BTS_Pic2 Then
_XBTS.DataTable .Rows.Find(BP_B TS_Pick.Splits( 0).DisplayColum ns("BTSIndex"). DataColumn.Valu e).Item("Pic2")
= rawData
ElseIf CM_Pics.SourceC ontrol Is BTS_Pic3 Then
_XBTS.DataTable .Rows.Find(BP_B TS_Pick.Splits( 0).DisplayColum ns("BTSIndex"). DataColumn.Valu e).Item("Pic3")
= rawData
End If
If CM_Pics.SourceC ontrol Is Sec_Pic1 Then
_XSector.DataTa ble.Rows.Find(S I_Sector_Pick.S plits(0).Displa yColumns("Secto rIndex").DataCo lumn.Value).Ite m("Pic1")
= rawData
ElseIf CM_Pics.SourceC ontrol Is Sec_Pic2 Then
_XSector.DataTa ble.Rows.Find(S I_Sector_Pick.S plits(0).Displa yColumns("Secto Index").DataCol umn.Value).Item ("Pic2")
= rawData
ElseIf CM_Pics.SourceC ontrol Is Sec_Pic3 Then
_XSector.DataTa ble.Rows.Find(S I_Sector_Pick.S plits(0).Displa yColumns("Secto rIndex").DataCo lumn.Value).Ite m("Pic3")
= rawData
End If
File.Delete(pbf ile)
Catch ex As Exception
If SD_Tabs.Current = 4 Then
MessageBox.Show (BP_BTS_Pick.Sp lits(0).Display Columns("BTSID" ).DataColumn.Va lue.ToString
+ " - " + pm + " has not been saved to the Database. Click Update/Save, then
try Opening the picture again.", "Picture Not in DB", MessageBoxButto ns.OK,
MessageBoxIcon. Information)
Else
MessageBox.Show (SI_Sector_Pick .Splits(0).Disp layColumns("Sec torID").DataCol umn.Value.ToStr ing
+ " - " + pm + " has not been saved to the Database. Click Update/Save, then
try Opening the picture again.", "Picture Not in DB", MessageBoxButto ns.OK,
MessageBoxIcon. Information)
End If
End Try
End Sub
"Cor Ligthert" <no************ @planet.nl> wrote in message
news:%2******** **********@TK2M SFTNGP14.phx.gb l...
Lee,
Your code snippet is to small to give all that information to use you are
telling now.
However what tackled me direct. Why are you writing something to a
temporaly disk file?
Are you sure you would not have a look at my sample where the Memory
Stream is used?
Cor