?
Solved

Uploaded image leaves blank container when database entry empty

Posted on 2009-05-03
25
Medium Priority
?
524 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 14
  • 10
25 Comments
 
LVL 6

Expert Comment

by:DanielWillmott
ID: 24291156
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
ID: 24291165
Or the 'no image' could just be a blank GIF...etc.
0
 

Author Comment

by:designaire
ID: 24291191
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:DanielWillmott
ID: 24291198
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
ID: 24291274
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
ID: 24291314
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
ID: 24291331
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
ID: 24291543
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
ID: 24311121
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
ID: 24313010
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
ID: 24320607
Parameter @img_id has no default value.
0
 
LVL 6

Expert Comment

by:DanielWillmott
ID: 24320799
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
 

Author Comment

by:designaire
ID: 24321131
Value cannot be null.
Parameter name: String
0
 
LVL 6

Expert Comment

by:DanielWillmott
ID: 24321307
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
ID: 24321620
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
ID: 24328471
This image is being written to a database. Maybe I didn't mention that.
0
 

Author Comment

by:designaire
ID: 24331732
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 1600 total points
ID: 24331814
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
ID: 24332044
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
ID: 24341108
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
ID: 24341116
The images keep disappearing also. I'm not sure why that is...
0
 

Author Comment

by:designaire
ID: 24341337

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
ID: 24341347
I wanted to increase the points
0
 

Author Closing Comment

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

THANKS FOR YOUR HELP!!!
0
 
LVL 6

Expert Comment

by:DanielWillmott
ID: 24341354
ah of course... 'is'
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

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