ASP.NET image upload help
65 pts.
0
Q:
ASP.NET image upload help
hello i need some help trouble shooting an image upload problem. it is from a tutorial from here:
http://www.beansoftware.com/ASP.NET-Tutorials/Images-Database.aspx

i altered slightly: to this--
default.aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 260px;
}
.style2
{
width: 260px;
height: 29px;
}
.style3
{
height: 29px;
}
.style4
{
width: 260px;
height: 21px;
}
.style5
{
height: 21px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>

<table style="width:100%;">
<tr>
<td class="style2">
<asp:Label ID="Label4" runat="server" Text="images"></asp:Label>
</td>
<td class="style3">
<asp:FileUpload ID="fileUpload" runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="No Image Selected" ControlToValidate="fileUpload"></asp:RequiredFieldValidator>
</td>

</tr>
<tr>
<td class="style1">
<asp:Label ID="Label3" runat="server" Text="title"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtTitle" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ErrorMessage="Title Is Required" ControlToValidate="txtTitle"></asp:RequiredFieldValidator>
</td>

</tr>
<tr>
<td class="style4">
</td>
<td class="style5">
<asp:Label ID="lblStatus" runat="server" Text="lblStatus"></asp:Label>
</td>

</tr>
<tr>
<td class="style1">
 </td>
<td>

<asp:Button ID="btnUpload" runat="server" Text="Upload" />

</td>

</tr>
<tr>
<td class="style1">
 </td>
<td>

<asp:HyperLink ID="lnkView" runat="server">ViewImages</asp:HyperLink>

</td>

</tr>
</table>

</div>
</form>
</body>
</html>






default.sapx.vb


Partial Class _Default
Inherits System.Web.UI.Page

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

End Sub

Protected Sub btnUpload_Click() Handles btnUpload.Click

Dim intLength As Integer

Dim arrContent As Byte()



If fileUpload.PostedFile Is Nothing Then

lblStatus.Text = "No file specified."

Exit Sub

Else

Dim fileName As String = fileUpload.PostedFile.FileName

Dim ext As String = fileName.Substring(fileName.LastIndexOf("."))

ext = ext.ToLower



Dim imgType = fileUpload.PostedFile.ContentType

If ext = ".jpg" Then

ElseIf ext = ".bmp" Then

ElseIf ext = ".gif" Then

ElseIf ext = "jpg" Then

ElseIf ext = "bmp" Then

ElseIf ext = "gif" Then

Else

lblStatus.Text = "Only gif, bmp, or jpg format files supported."

Exit Sub

End If



intLength = Convert.ToInt32(fileUpload.PostedFile.InputStream.Length)

ReDim arrContent(intLength)



fileUpload.PostedFile.InputStream.Read(arrContent, 0, intLength)



If Doc2SQLServer(txtTitle.Text.Trim, arrContent, intLength, imgType) = True Then

lblStatus.Text = "Image uploaded successfully."

Else

lblStatus.Text = "An error occured while uploading Image... Please try again."

End If

End If

End Sub

Protected Function Doc2SQLServer(ByVal title As String, ByVal Content As Byte(), ByVal Length As Integer, ByVal strType As String) As Boolean

Try

Dim cnn As Data.SqlClient.SqlConnection

Dim cmd As Data.SqlClient.SqlCommand

Dim param As Data.SqlClient.SqlParameter

Dim strSQL As String



strSQL = "Insert Into tblImage(imgData,imgTitle,imgType,imgLength) Values(@content,@title,@type,@length)"



Dim connString As String = "Data Source=ZONE777\SQLS;Initial Catalog=imgDB;Integrated Security=True"

cnn = New Data.SqlClient.SqlConnection(connString)



cmd = New Data.SqlClient.SqlCommand(strSQL, cnn)



param = New Data.SqlClient.SqlParameter("@content", Data.SqlDbType.Image)



param.Value = Content

cmd.Parameters.Add(param)



param = New Data.SqlClient.SqlParameter("@title", Data.SqlDbType.VarChar)

param.Value = title

cmd.Parameters.Add(param)



param = New Data.SqlClient.SqlParameter("@type", Data.SqlDbType.VarChar)

param.Value = strType

cmd.Parameters.Add(param)



param = New Data.SqlClient.SqlParameter("@length", Data.SqlDbType.BigInt)

param.Value = Length

cmd.Parameters.Add(param)



cnn.Open()

cmd.ExecuteNonQuery()

cnn.Close()

Return True

Catch ex As Exception

Return False

End Try

End Function


End Class





lastly web.config



<connectionStrings>
<add name="connString" connectionString="Data Source=ZONE777\SQLS;Initial Catalog=imgDB;Integrated Security=True;"/>
</connectionStrings>


the page is displaying but i am getting-----An error occured while uploading Image... Please try again.





thanks craig
ASKED: Aug 5 2009  0:47 AM GMT
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
0
29830 pts.
0
A:
 RATE THIS ANSWER
0
Click to Vote:
  •   0
  •  0
  • AddThis Social Bookmark Button
Hi Craig.

You really need to try to understand your code.

This: "An error occured while uploading Image..." is a message from your application.

It is being displayed because the Doc2SQLServer function returned false, and it is returning false because some error occurred and was caught by the exception handler (in a very bad way, because it does nothing with the error but return false, denying the posibility to see what the error was).

Chances are that you are not successfully connecting to the database, but you will never know until you run your application in debug mode, or put some debug messages in it.

I would also temporarily remove the exception handling code, to be able to see the error messages generated when running the form.

One last thing: May I ask why you were doing something similar a few days ago using C#, and now you are doing almost the same thing but using VB ? (just curious).

Best regards,
Last Answered: Aug 5 2009  1:32 AM GMT by Carlosdl   29830 pts.
0
0
Discuss This Answer:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _



_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

Zpupster   65 pts.  |   Aug 5 2009  10:35AM GMT

i thank you for your response. the reason i am trying to do this in vb rather than C# is becasue
i am trying to get tutorials so i can accomplish the task of storing images into a database and them displaying them out in aspx page.

the last tutorial in c#– i could not get it to display in a page, so i was frustrated and found this tutorial
in vb. this tutorial lets me display the results using gridview so that would be a big help to me.

but first i have to get it to store the images, i understood what you posted. i wil try to get the error messages to generate by removing the exception handling code.

as for it not connecting to database. i believe it is, in vs2008 i added a gridview to this page,
configured the datasource, tested the connection and that was ok’d. then i went back and copied
the connection string, put that in my code and the web.config file. then went back and deleted the gridview and datasource because gridview is not needed anymore.

thanks,
craig

 
0