Solved

Uploaded image leaves blank container when database entry empty

Posted on 2009-05-03
25
512 Views
Last Modified: 2012-05-06
I have this program for a client that enables the client to enter events and upload an image on their events page. I'm getting everything working except I just realized that if they decide not to upload an image and the image database is blank. When the asp page is viewed the image that is empty leaves a container holder with an x. How do I prevent the blank image from viewing if there is nothing in the database.

Below is the code I'm using for the asp page except I have an event table for each month. this is just for January...

The database fields that are empty are..
img_type and img_stream
stream is a OLE Object

One thing I could so is have it upload a small white image if an image isn't uploaded...I don't know how to do that either.....below is code I'm using for both...

Aspx file

 

Sub Page_Load()

        Dim sql

Dim StringConnection = "Provider=Microsoft.Jet.OLEDB.4.0; data source=" & Server.MapPath("~\App_Data\db1.mdb")

        sql = "SELECT description, dateorder, MONTH(dateorder) AS [dateordermonth] , title, times, img_id, img_stream FROM tblImg"

 

        Dim ds As New DataSet()

        Dim objectconnection As New OleDbConnection(StringConnection)

        Dim command As New OleDbCommand(sql, objectconnection)

        Dim objAdapter As New OleDbDataAdapter(sql, objectconnection)

        objAdapter.SelectCommand = command

        objAdapter.Fill(ds, "Events")

        Dim objDataView As New DataView(ds.Tables("Events"))

        objAdapter.Dispose()

        command.Dispose()

        Dim dv1 As DataView

  dv1 = New DataView(ds.Tables(0), "dateordermonth = 1",            "dateordermonth Desc", DataViewRowState.CurrentRows)

        jan.DataSource = dv1

        jan.DataBind()

 

<asp:Repeater ID="jan" runat="server" OnPreRender="repReview_PreRender"> 

 

<HeaderTemplate>

<table border="1" width="100%">

<tr><td colspan="7"><div align="center" class="header_events">January</div></td></tr>

</HeaderTemplate>

<ItemTemplate>

<tr>

<td width="100" align="left" valign="top" bgcolor="#cccccc" class="SubHeadsmEventsDates"><%#Container.DataItem("times")%></class></td>

<td width="490" align="left" valign="top" bgcolor="#c7e0e6" class="SubHeadsmEvents"><%#Container.DataItem("title")%><br />

<%#Container.DataItem("description")%></td>

<td> <asp:Image ID="imgSaved" runat="server" ImageUrl='<%# imageURL(DataBinder.Eval(Container.DataItem, "img_id")) %>'

                                AlternateText='<%#DataBinder.Eval(Container.DataItem,"img_id") %>' /></td>

</tr>

</ItemTemplate>

<FooterTemplate>

</table>

</FooterTemplate>

</asp:Repeater>
 
 
 
 
 
 
 

Upload image
 

Partial Class access_db_Default

    Inherits System.Web.UI.Page

    ' Access Database oledb connection string

    ' Using Provider Microsoft.Jet.OLEDB.4.0

    Dim connStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("App_Data/db1.mdb")
 

    ' Object created for Oledb Connection

    Dim myAccessConnection As New OleDbConnection(connStr)
 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then

            'displayImages()

        End If

    End Sub
 

    Public Sub openAccessConnection()

        ' If condition that can be used to check the access database connection

        ' whether it is already open or not.

        If myAccessConnection.State = ConnectionState.Closed Then

            myAccessConnection.Open()

        End If

    End Sub
 

    Public Sub closeAccessConnection()

        ' If condition to check the access database connection state

        ' If it is open then close it.

        If myAccessConnection.State = ConnectionState.Open Then

            myAccessConnection.Close()

        End If

    End Sub
 

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
 

        openAccessConnection()
 

        Dim imageSize As Int64

        Dim imageType As String

        Dim imageStream As Stream

        Dim imageStream1 As Stream
 

        ' Gets the Size of the Image

        imageSize = fileImgUpload.PostedFile.ContentLength

        '

        'Gets the Image Type

        imageType = fileImgUpload.PostedFile.ContentType
 

        'Reads the Image stream

        imageStream = fileImgUpload.PostedFile.InputStream

        imageStream1 = fileImgUpload.PostedFile.InputStream
 

        Dim imageContent(imageSize) As Byte

        Dim intStatus As Integer

        intStatus = imageStream.Read(imageContent, 0, imageSize)
 

            Dim cmd As New OleDbCommand("insert into tblImg(dateorder,title,times,description,img_type,img_stream) values(@dateorder,@title,@times,@description,@img_type,@img_stream)", myAccessConnection)

            cmd.CommandType = CommandType.Text

            cmd.Parameters.Add("@dateorder", OleDbType.VarChar).Value = txtDateOrder.Text

            cmd.Parameters.Add("@title", OleDbType.VarChar).Value = txtDescription.Text

            cmd.Parameters.Add("@times", OleDbType.VarChar).Value = txtTimes.Text

            cmd.Parameters.Add("@description", OleDbType.VarChar).Value = txtDescription.Text

            cmd.Parameters.Add("@img_type", OleDbType.VarChar).Value = imageType

            cmd.Parameters.Add("img_stream", OleDbType.Binary).Value = imageContent

            cmd.ExecuteNonQuery()
 

            closeAccessConnection()

            Response.Redirect("default.aspx")
 
 
 

    End Sub

Open in new window

0
Comment
Question by:designaire
  • 14
  • 10
25 Comments
 
LVL 6

Expert Comment

by:DanielWillmott
Comment Utility
Assuming imageURL() is a sub you have in the code behind, you could have that sub detect whether the img_id actually exists. If not, set the URL to a static GIF that says something like 'No Image Available' .
0
 
LVL 6

Expert Comment

by:DanielWillmott
Comment Utility
Or the 'no image' could just be a blank GIF...etc.
0
 

Author Comment

by:designaire
Comment Utility
The img_id has other fields that have copy in the them. It's just the image that they might not have.
How would you add a blank gif. Would you add it to the server? ( I got the image upload code online)
Is this where I could put an blank gif...InputStream?

If fileImgUpload.PostedFile.InputStream.Length equals 0 then
imageStream = fileImgUpload.PostedFile.InputStream ????
End If
0
 
LVL 6

Expert Comment

by:DanielWillmott
Comment Utility
What does the imageURL function in the ASPX codebehind look like? You could modify that so it just points to a url on your server when there is no image... (i.e. /images/noimage.gif)
0
 

Author Comment

by:designaire
Comment Utility
I think the imput stream is here...

imageStream = fileImgUpload.PostedFile.InputStream
Imports System.Data

Imports System.Data.OleDb

Imports System.IO

Partial Class access_db_Default

    Inherits System.Web.UI.Page

    ' Access Database oledb connection string

    ' Using Provider Microsoft.Jet.OLEDB.4.0

    Dim connStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("App_Data/db1.mdb")
 

    ' Object created for Oledb Connection

    Dim myAccessConnection As New OleDbConnection(connStr)
 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then

            'displayImages()

        End If

    End Sub
 

    Public Sub openAccessConnection()

        ' If condition that can be used to check the access database connection

        ' whether it is already open or not.

        If myAccessConnection.State = ConnectionState.Closed Then

            myAccessConnection.Open()

        End If

    End Sub
 

    Public Sub closeAccessConnection()

        ' If condition to check the access database connection state

        ' If it is open then close it.

        If myAccessConnection.State = ConnectionState.Open Then

            myAccessConnection.Close()

        End If

    End Sub
 

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        'Try

        openAccessConnection()
 

        Dim imageSize As Int64

        Dim imageType As String

        Dim imageStream As Stream

        Dim imageStream1 As Stream
 

        ' Gets the Size of the Image

        imageSize = fileImgUpload.PostedFile.ContentLength

        '

        'Gets the Image Type

        imageType = fileImgUpload.PostedFile.ContentType
 

        'Reads the Image stream

        imageStream = fileImgUpload.PostedFile.InputStream
 

        Dim imageContent(imageSize) As Byte

        Dim intStatus As Integer

        intStatus = imageStream.Read(imageContent, 0, imageSize)
 

        If txtDateOrder.Text <> "" And txtTitle.Text <> "" And txtTimes.Text <> "" And txtDescription.Text <> "" And txtDescription.Text <> "" Then
 
 

            Dim cmd As New OleDbCommand("insert into tblImg(dateorder,title,times,description,img_type,img_stream) values(@dateorder,@title,@times,@description,@img_type,@img_stream)", myAccessConnection)

            cmd.CommandType = CommandType.Text

            cmd.Parameters.Add("@dateorder", OleDbType.VarChar).Value = txtDateOrder.Text

            cmd.Parameters.Add("@title", OleDbType.VarChar).Value = txtDescription.Text

            cmd.Parameters.Add("@times", OleDbType.VarChar).Value = txtTimes.Text

            cmd.Parameters.Add("@description", OleDbType.VarChar).Value = txtDescription.Text

            cmd.Parameters.Add("@img_type", OleDbType.VarChar).Value = imageType

            cmd.Parameters.Add("img_stream", OleDbType.Binary).Value = imageContent

            cmd.ExecuteNonQuery()
 

            closeAccessConnection()

            Response.Redirect("default.aspx")
 

        Else

            lblErrorAdd.Text = "You must fill out all fields"

        End If

        ' Catch ex As Exception

        'Response.Write(ex.Message)

        'closeAccessConnection()

        ' End Try
 
 

        'Response.Redirect("default.aspx")

    End Sub
 

    'Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    '   If Not Page.IsPostBack Then

    '      If Session("Enter") Is Nothing Then

    '         Response.Redirect("loginpage.aspx")
 

    '     End If

    ' End If

    'End Sub
 

    Protected Sub EventsGrid_RowUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdatedEventArgs) Handles GridView1.RowUpdated

        If e.Exception IsNot Nothing Then

            lblError.Text = "An exception ocurred." & e.Exception.Message

            e.ExceptionHandled = True

            e.KeepInEditMode = True

        ElseIf e.AffectedRows = 0 Then

            lblError.Text = "No rows Updated"

        End If

    End Sub
 
 

    Protected Sub EventsGrid_RowDeleted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeletedEventArgs) Handles GridView1.RowDeleted

        If e.Exception IsNot Nothing Then

            lblError.Text = "An Deleted exception ocurred." & e.Exception.Message

            e.ExceptionHandled = True

        ElseIf e.AffectedRows = 0 Then

            lblError.Text = "No rows Updated"

        End If
 

    End Sub
 

    'Public Sub displayImages()

    ' Try
 

    '     myAccessConnection.Open()

    ' Dim cmd As New OleDbCommand("select * from tblImg", myAccessConnection)
 

    '' Mark the Command as a Text

    '     cmd.CommandType = CommandType.Text

    '

    ' Dim myAdapter As New OleDbDataAdapter(cmd)

    ' Dim myDataSet As New DataSet

    '  myAdapter.Fill(myDataSet)
 

    '   GridView1.DataSource = myDataSet

    '    GridView1.DataBind()
 

    '   myAccessConnection.Close()

    ' Catch ex As Exception

    '  End Try

    ' End Sub
 

    Public Function imageURL(ByVal img_id) As String

        Return ("retrieveImages.aspx?id=" & img_id)

    End Function
 

    Protected Sub clnDate_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles clnDate.SelectionChanged

        txtDateOrder.Text = CStr(clnDate.SelectedDate.Date)

        clnDate.Visible = False

        btnDate.Visible = True

    End Sub
 

    Protected Sub btnDate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDate.Click

        clnDate.Visible = True

    End Sub

End Class
 
 

Retrieve Images page

mports System.Data

Imports System.Data.OleDb

Imports System.IO

Partial Class access_db_retrieveImages

    Inherits System.Web.UI.Page
 

    ' Access Database oledb connection string

    ' Using Provider Microsoft.Jet.OLEDB.4.0

    Dim connStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("App_Data/db1.mdb")
 

    ' Object created for Oledb Connection

    Dim myAccessConnection As New OleDbConnection(connStr)
 

    Public Sub openAccessConnection()

        ' If condition that can be used to check the access database connection

        ' whether it is already open or not.

        If myAccessConnection.State = ConnectionState.Closed Then

            myAccessConnection.Open()

        End If

    End Sub
 

    Public Sub closeAccessConnection()

        ' If condition to check the access database connection state

        ' If it is open then close it.

        If myAccessConnection.State = ConnectionState.Open Then

            myAccessConnection.Close()

        End If

    End Sub
 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Try

            Dim ID As String = Request.QueryString("id")
 

            myAccessConnection.Open()

            Dim cmd As New OleDbCommand("select * from tblImg where img_id=@img_id", myAccessConnection)
 

            ' Mark the Command as a Text

            cmd.CommandType = CommandType.Text
 

            ' Add Parameters to Command

            Dim img_id As New OleDbParameter("@img_id", SqlDbType.Int)

            img_id.Value = ID

            cmd.Parameters.Add(img_id)
 

            Dim myAdapter As New OleDbDataAdapter(cmd)

            Dim myDataSet As New DataSet

            myAdapter.Fill(myDataSet)
 

            For Each dRow As DataRow In myDataSet.Tables(0).Rows

                Response.ContentType = dRow("img_type")

                Response.BinaryWrite(dRow("img_stream"))

            Next
 

            myAccessConnection.Close()

        Catch exc As Exception

        End Try

    End Sub

End Class

Open in new window

0
 
LVL 6

Expert Comment

by:DanielWillmott
Comment Utility
You could change the OnLoad sub for the retrieve images page to push the 'noimage.gif' file when needed:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Try

            Dim ID As String = Request.QueryString("id")

 

            myAccessConnection.Open()

            Dim cmd As New OleDbCommand("select * from tblImg where img_id=@img_id", myAccessConnection)

 

            ' Mark the Command as a Text

            cmd.CommandType = CommandType.Text

 

            ' Add Parameters to Command

            Dim img_id As New OleDbParameter("@img_id", SqlDbType.Int)

            img_id.Value = ID

            cmd.Parameters.Add(img_id)

 

            Dim myAdapter As New OleDbDataAdapter(cmd)

            Dim myDataSet As New DataSet

            myAdapter.Fill(myDataSet)

            

            If myDataSet.Tables(0).Rows.Count=0 then

            	Response.Clear();

        		Response.ContentType = "image/GIF";      

		Response.WriteFile("~/images/NoImage.gif");

            Else

	            For Each dRow As DataRow In myDataSet.Tables(0).Rows

	                Response.ContentType = dRow("img_type")

	                Response.BinaryWrite(dRow("img_stream"))

	            Next

            End If

 

            myAccessConnection.Close()

        Catch exc As Exception

        End Try

    End Sub

Open in new window

0
 
LVL 6

Expert Comment

by:DanielWillmott
Comment Utility
Sorry....this one should work:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Try

            Dim ID As String = Request.QueryString("id")

 

            myAccessConnection.Open()

            Dim cmd As New OleDbCommand("select * from tblImg where img_id=@img_id", myAccessConnection)

 

            ' Mark the Command as a Text

            cmd.CommandType = CommandType.Text

 

            ' Add Parameters to Command

            Dim img_id As New OleDbParameter("@img_id", SqlDbType.Int)

            img_id.Value = ID

            cmd.Parameters.Add(img_id)

 

            Dim myAdapter As New OleDbDataAdapter(cmd)

            Dim myDataSet As New DataSet

            myAdapter.Fill(myDataSet)

            

            If myDataSet.Tables(0).Rows.Count=0 then

            	Response.Clear()

        		Response.ContentType = "image/GIF"      

		Response.WriteFile("~/images/NoImage.gif")

            Else

	            For Each dRow As DataRow In myDataSet.Tables(0).Rows

	                Response.ContentType = dRow("img_type")

	                Response.BinaryWrite(dRow("img_stream"))

	            Next

            End If

 

            myAccessConnection.Close()

        Catch exc As Exception

        End Try

    End Sub

Open in new window

0
 

Author Comment

by:designaire
Comment Utility
Actually, when I transferred it to the server it wasn't working right even without the code you added. It seems like something is wrong with the postback. It uploads the image but when I add the date or add another image, one of the images are missing. Then when you add another image another image is missing and another one comes back.
0
 

Author Comment

by:designaire
Comment Utility
I got the other problem with the postback working.

I tried your code to upload a blank image, but it didn't work yet. It still gave me a blank image with an x. I also tried using the entire url instead of ~/images/NoImage.gif. Any other suggestions?
0
 
LVL 6

Expert Comment

by:DanielWillmott
Comment Utility
Put a throw in the exception area and see if there is an error that is happening in there.. Also, did you step through the above code with the debugger?
 
 

Catch exc As Exception

     Throw exc

End Try

Open in new window

0
 

Author Comment

by:designaire
Comment Utility
Parameter @img_id has no default value.
0
 
LVL 6

Expert Comment

by:DanielWillmott
Comment Utility
How about...

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Try

            Dim ID As String = Request.QueryString("id")

 

            myAccessConnection.Open()

            Dim cmd As New OleDbCommand("select * from tblImg where img_id=@img_id", myAccessConnection)

 

            ' Mark the Command as a Text

            cmd.CommandType = CommandType.Text

 

            ' Add Parameters to Command

            Dim img_id As New OleDbParameter("@img_id", OleDbType.Integer)

            img_id.Value = Integer.Parse(ID)

            cmd.Parameters.Add(img_id)

 

            Dim myAdapter As New OleDbDataAdapter(cmd)

            Dim myDataSet As New DataSet

            myAdapter.Fill(myDataSet)

            

            If myDataSet.Tables(0).Rows.Count=0 then

            	Response.Clear()

        		Response.ContentType = "image/GIF"      

		Response.WriteFile("~/images/NoImage.gif")

            Else

	            For Each dRow As DataRow In myDataSet.Tables(0).Rows

	                Response.ContentType = dRow("img_type")

	                Response.BinaryWrite(dRow("img_stream"))

	            Next

            End If

 

            myAccessConnection.Close()

        Catch exc As Exception

        	Throw exc

        End Try

    End Sub

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:designaire
Comment Utility
Value cannot be null.
Parameter name: String
0
 
LVL 6

Expert Comment

by:DanielWillmott
Comment Utility
This should do it... :)
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Try         

            If Request.QueryString("id")="" then

            	Response.Clear()

        		Response.ContentType = "image/GIF"      

		Response.WriteFile("~/images/NoImage.gif")            

            Else

            	Dim ID As String = Request.QueryString("id")

            	

	            myAccessConnection.Open()

	            Dim cmd As New OleDbCommand("select * from tblImg where img_id=@img_id", myAccessConnection)

	 

	            ' Mark the Command as a Text

	            cmd.CommandType = CommandType.Text

	 

	            ' Add Parameters to Command

	            Dim img_id As New OleDbParameter("@img_id", OleDbType.Integer)

	            img_id.Value = Integer.Parse(ID)

	            cmd.Parameters.Add(img_id)

	 

	            Dim myAdapter As New OleDbDataAdapter(cmd)

	            Dim myDataSet As New DataSet

	            myAdapter.Fill(myDataSet)

	            

	            If myDataSet.Tables(0).Rows.Count=0 then

	            	Response.Clear()

	        		Response.ContentType = "image/GIF"      

			Response.WriteFile("~/images/NoImage.gif")

	            Else

		            For Each dRow As DataRow In myDataSet.Tables(0).Rows

		                Response.ContentType = dRow("img_type")

		                Response.BinaryWrite(dRow("img_stream"))

		            Next

	            End If

	 

	            myAccessConnection.Close()

            end if

        Catch exc As Exception

        	Throw exc

        End Try

    End Sub

Open in new window

0
 

Author Comment

by:designaire
Comment Utility
None of the images showed. It looks like the small image was the correct size of the spacer image. (I used a spacer instead of a noimage.gif and  changed it in the code) The image didn't show and neither did any of the others. Sorry I can't offer any help.
0
 

Author Comment

by:designaire
Comment Utility
This image is being written to a database. Maybe I didn't mention that.
0
 

Author Comment

by:designaire
Comment Utility
I did get the other images to show but it still didn't work when I don't upload an image. When I looked at the database the image type was written as application/octet-stream. When I upload an images it's image/gif. So it's not writing to the correct format. I think if I can get it to upload to the correct format it will work.
0
 
LVL 6

Accepted Solution

by:
DanielWillmott earned 400 total points
Comment Utility
OK then. I see where this problem is. Change the Button1_Click sub and the Page_Load event handler:
'Button1_Click

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

 

		openAccessConnection()

	  

	    Dim cmd As New OleDbCommand("insert into tblImg(dateorder,title,times,description,img_type,img_stream) values(@dateorder,@title,@times,@description,@img_type,@img_stream)", myAccessConnection)

	    cmd.CommandType = CommandType.Text

	    cmd.Parameters.Add("@dateorder", OleDbType.VarChar).Value = txtDateOrder.Text

	    cmd.Parameters.Add("@title", OleDbType.VarChar).Value = txtDescription.Text

	    cmd.Parameters.Add("@times", OleDbType.VarChar).Value = txtTimes.Text

	    cmd.Parameters.Add("@description", OleDbType.VarChar).Value = txtDescription.Text

	            

		if fileImgUpload.HasFile=true then 

		        Dim imageSize As Int64

		        Dim imageType As String

		        Dim imageStream As Stream

		        Dim imageStream1 As Stream

		        

		      

		        ' Gets the Size of the Image

		        imageSize = fileImgUpload.PostedFile.ContentLength

		        '

		        'Gets the Image Type

		        imageType = fileImgUpload.PostedFile.ContentType

		 

		        'Reads the Image stream

		        imageStream = fileImgUpload.PostedFile.InputStream

		        imageStream1 = fileImgUpload.PostedFile.InputStream

		 

		        Dim imageContent(imageSize) As Byte

		        Dim intStatus As Integer

		        intStatus = imageStream.Read(imageContent, 0, imageSize)

		                          

		        cmd.Parameters.Add("@img_type", OleDbType.VarChar).Value = imageType

		        cmd.Parameters.Add("@img_stream", OleDbType.Binary).Value = imageContent

		Else

		        cmd.Parameters.Add("@img_type", OleDbType.VarChar).Value = DBNull.Value

		        cmd.Parameters.Add("@img_stream", OleDbType.Binary).Value = DBNull.Value

		End If         

	        

		cmd.ExecuteNonQuery()

		

		closeAccessConnection()

		Response.Redirect("default.aspx")
 

    End Sub
 
 
 

'Page_Load

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Try         

            If Request.QueryString("id")="" then

            	Response.Clear()

        		Response.ContentType = "image/GIF"      

				Response.WriteFile("~/images/NoImage.gif")            

            Else

            	Dim ID As String = Request.QueryString("id")

            	

	            myAccessConnection.Open()

	            Dim cmd As New OleDbCommand("select * from tblImg where img_id=@img_id", myAccessConnection)

	 

	            ' Mark the Command as a Text

	            cmd.CommandType = CommandType.Text

	 

	            ' Add Parameters to Command

	            Dim img_id As New OleDbParameter("@img_id", OleDbType.Integer)

	            img_id.Value = Integer.Parse(ID)

	            cmd.Parameters.Add(img_id)

	 

	            Dim myAdapter As New OleDbDataAdapter(cmd)

	            Dim myDataSet As New DataSet

	            myAdapter.Fill(myDataSet)

	            

	            If myDataSet.Tables(0).Rows.Count=0 then

	            	Response.Clear()

	        		Response.ContentType = "image/GIF"      

					Response.WriteFile("~/images/NoImage.gif")

	            Else

		            If myDataSet.Tables(0).Rows.Count=1 then

		            	If myDataSet.Tables(0).Rows(0)("img_type")=DBNull.Value then

		            		Response.Clear()

	        				Response.ContentType = "image/GIF"      

							Response.WriteFile("~/images/NoImage.gif")

		            	Else

		                	Response.ContentType = myDataSet.Tables(0).Rows(0)("img_type")

		                	Response.BinaryWrite(myDataSet.Tables(0).Rows(0)("img_stream"))

		                End If

		            End If

	            End If

	 

	            myAccessConnection.Close()

            end if

        Catch exc As Exception

        	Throw exc

        End Try

    End Sub

Open in new window

0
 
LVL 4

Expert Comment

by:ruffone
Comment Utility
A check I would make before adding the file would be the one below. No point adding a record if there is no file. You might also want to check the content type against an expected list so that you know what type of files are being added

If imageSize > 0 Then 

    cmd.ExecuteNonQuery() 

End If 
 

Dim isSupportedImage As Boolean = False

        Select Case imageType.ToLower

            Case Is = "image/gif"

                isSupportedImage = True

            Case Is = "image/jpeg"

                isSupportedImage = True

            Case Else

                isSupportedImage = False

        End Select

        If isSupportedImage = True Then

            'Continue processing/

        End If

Open in new window

0
 

Author Comment

by:designaire
Comment Utility
It doesn't like this line...

If myDataSet.Tables(0).Rows(0)("img_type")=DBNull.Value then
operator '=' is not defined for types Object and System.DBNull
0
 

Author Comment

by:designaire
Comment Utility
The images keep disappearing also. I'm not sure why that is...
0
 

Author Comment

by:designaire
Comment Utility

If myDataSet.Tables(0).Rows(0)("img_type")=DBNull.Value then

I used is instead of =

myDataSet.Tables(0).Rows(0)("img_type")is DBNull.Value then

AND IT WORKS!!!  THANKS FOR YOUR HELP! COULD HAVE NEVER DONE THIS MYSELF!!
0
 

Author Comment

by:designaire
Comment Utility
I wanted to increase the points
0
 

Author Closing Comment

by:designaire
Comment Utility
I tried to increase the points..not sure if it worked or not.

THANKS FOR YOUR HELP!!!
0
 
LVL 6

Expert Comment

by:DanielWillmott
Comment Utility
ah of course... 'is'
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now