Wayne Barron
asked on
VB Upload Script, giving errors.
Hello All;
I download the code from here
http://www.codeproject.com/KB/aspnet/fileupload.aspx
Anc converted to VB from here
http://www.developerfusion.com/tools/convert/csharp-to-vb/
The code is not compiling, no matter what I try.
I am a ASP Classic guy, but work with .net when the need is there.
The need is here for our new site to have a solid upload script
To puts the data in the database as well.
And this scripts works nicely, though I will be moving it over to a SQL Server and away from the access database.
Thanks
Carrzkiss
default.aspx
default.aspx.vb
I download the code from here
http://www.codeproject.com/KB/aspnet/fileupload.aspx
Anc converted to VB from here
http://www.developerfusion.com/tools/convert/csharp-to-vb/
The code is not compiling, no matter what I try.
I am a ASP Classic guy, but work with .net when the need is there.
The need is here for our new site to have a solid upload script
To puts the data in the database as well.
And this scripts works nicely, though I will be moving it over to a SQL Server and away from the access database.
Thanks
Carrzkiss
default.aspx
<%@ page Language="VB" CodeFile="default.aspx.vb" Inherits="default" %>
<HTML>
<HEAD>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server" enctype="multipart/form-data">
<asp:label id="lblFile" runat="server" Font-Bold="True">Picture:</asp:label>
<input id="filMyFile" type="file" runat="server">
<p></p>
<asp:button id="cmdSend" runat="server" Text="Send"/>
<p></p>
<asp:Label id="lblInfo" runat="server" Font-Bold="True" Visible="false"></asp:Label>
<p></p>
<table>
<tr>
<td>
<asp:Label id="lblText1" runat="server" Font-Bold="True" Visible="false">This was stored as file</asp:Label>
</td>
<td>
<asp:Label id="lblText2" runat="server" Font-Bold="True" Visible="false">This was stored in database</asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Image id="imgFile" runat="server" Visible="False"></asp:Image>
</TD>
<td>
<asp:Image id="imgDB" runat="server" Visible="False"></asp:Image>
</td>
</TR>
</TABLE>
</FORM>
</body>
</HTML>
default.aspx.vb
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.IO.Path
Imports System.IO
Namespace FileUpload
''' <summary>
''' Summary description for WebForm1.
''' </summary>
Public Class WebForm1
Inherits System.Web.UI.Page
Private Const MDBFILE As String = "FileUpload.mdb"
Protected lblFile As Label
Protected filMyFile As HtmlInputFile
Protected lblInfo As System.Web.UI.WebControls.Label
Protected cmdSend As System.Web.UI.WebControls.Button
Protected imgFile As System.Web.UI.WebControls.Image
Protected imgDB As System.Web.UI.WebControls.Image
Protected lblText1 As System.Web.UI.WebControls.Label
Protected lblText2 As System.Web.UI.WebControls.Label
Protected Overrides Sub OnInit(e As EventArgs)
InitializeComponent()
MyBase.OnInit(e)
End Sub
Private Sub InitializeComponent()
AddHandler Me.Load, New System.EventHandler(AddressOf Me.Page_Load)
AddHandler Me.cmdSend.Click, New System.EventHandler(AddressOf Me.cmdSend_Click)
End Sub
Private Sub Page_Load(sender As Object, e As System.EventArgs)
' Check if FileID was passed to this page as a parameter
If Request.QueryString("FileID") IsNot Nothing Then
' Get the file out of database and return it to requesting client
ShowTheFile(Convert.ToInt32(Request.QueryString("FileID")))
End If
End Sub
' Processes click on our cmdSend button
Private Sub cmdSend_Click(sender As Object, e As System.EventArgs)
' Check to see if file was uploaded
If filMyFile.PostedFile IsNot Nothing Then
' Get a reference to PostedFile object
Dim myFile As HttpPostedFile = filMyFile.PostedFile
' Get size of uploaded file
Dim nFileLen As Integer = myFile.ContentLength
' make sure the size of the file is > 0
If nFileLen > 0 Then
' Allocate a buffer for reading of the file
Dim myData As Byte() = New Byte(nFileLen - 1) {}
' Read uploaded file from the Stream
myFile.InputStream.Read(myData, 0, nFileLen)
' Create a name for the file to store
Dim strFilename As String = Path.GetFileName(myFile.FileName)
' Write data into a file
WriteToFile(Server.MapPath(strFilename), myData)
' Store it in database
Dim nFileID As Integer = WriteToDB(strFilename, myFile.ContentType, myData)
' Set label's text
lblInfo.Text = "Filename: " & strFilename & "<br>" & "Size: " & nFileLen.ToString() & "<p>"
' Set URL of the the object to point to the file we've just saved
imgFile.ImageUrl = strFilename
imgFile.ToolTip = "This file was stored to as file."
lblText1.Text = imgFile.ImageUrl
' Set URL of the the object to point to the this script with ID of the file
' that will retreive file out the database
imgDB.ImageUrl = GetMyName() & "?FileID=" & nFileID.ToString()
imgDB.ToolTip = "This file was stored in database."
lblText2.Text = imgDB.ImageUrl
' show the images and text
imgFile.Visible = True
imgDB.Visible = True
lblText1.Visible = True
lblText2.Visible = True
End If
End If
End Sub
' Writes file to current folder
Private Sub WriteToFile(strPath As String, ByRef Buffer As Byte())
' Create a file
Dim newFile As New FileStream(strPath, FileMode.Create)
' Write data to the file
newFile.Write(Buffer, 0, Buffer.Length)
' Close file
newFile.Close()
End Sub
' Generates database connection string
Private Function GetConnectionString() As String
Return "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath(MDBFILE) & ";"
End Function
' Writes file to the database
Private Function WriteToDB(strName As String, strType As String, ByRef Buffer As Byte()) As Integer
Dim nFileID As Integer = 0
' Create connection
Dim dbConn As New OleDbConnection(GetConnectionString())
' Create Adapter
Dim dbAdapt As New OleDbDataAdapter("SELECT * FROM tblFile", dbConn)
' We need this to get an ID back from the database
dbAdapt.MissingSchemaAction = MissingSchemaAction.AddWithKey
' Create and initialize CommandBuilder
Dim dbCB As New OleDbCommandBuilder(dbAdapt)
' Open Connection
dbConn.Open()
' New DataSet
Dim dbSet As New DataSet()
' Populate DataSet with data
dbAdapt.Fill(dbSet, "tblFile")
' Get reference to our table
Dim dbTable As DataTable = dbSet.Tables("tblFile")
' Create new row
Dim dbRow As DataRow = dbTable.NewRow()
' Store data in the row
dbRow("FileName") = strName
dbRow("FileSize") = Buffer.Length
dbRow("ContentType") = strType
dbRow("FileData") = Buffer
' Add row back to table
dbTable.Rows.Add(dbRow)
' Update data source
dbAdapt.Update(dbSet, "tblFile")
' Get newFileID
If Not dbRow.IsNull("FileID") Then
nFileID = CInt(dbRow("FileID"))
End If
' Close connection
dbConn.Close()
' Return FileID
Return nFileID
End Function
' Read file out of the database and returns it to client
Private Sub ShowTheFile(FileID As Integer)
' Define SQL select statement
Dim SQL As String = "SELECT FileSize, FileData, ContentType FROM tblFile WHERE FileID = " & FileID.ToString()
' Create Connection object
Dim dbConn As New OleDbConnection(GetConnectionString())
' Create Command Object
Dim dbComm As New OleDbCommand(SQL, dbConn)
' Open Connection
dbConn.Open()
' Execute command and receive DataReader
Dim dbRead As OleDbDataReader = dbComm.ExecuteReader()
' Read row
dbRead.Read()
' Clear Response buffer
Response.Clear()
' Set ContentType to the ContentType of our file
Response.ContentType = DirectCast(dbRead("ContentType"), String)
' Write data out of database into Output Stream
Response.OutputStream.Write(DirectCast(dbRead("FileData"), Byte()), 0, CInt(dbRead("FileSize")))
' Close database connection
dbConn.Close()
' End the page
Response.[End]()
End Sub
' Reads the name of current web page
Private Function GetMyName() As String
' Get the script name
Dim strScript As String = Request.ServerVariables("SCRIPT_NAME")
' Get position of last slash
Dim nPos As Integer = strScript.LastIndexOf("/")
' Get everything after slash
If nPos > -1 Then
strScript = strScript.Substring(nPos + 1)
End If
Return strScript
End Function
End Class
End Namespace
Is there a reason why you can't use the FileUpload control?
1. tell us in which line u get the error and whats the error?
2. also, which version of .Net u use?
3. remove the following lines and check
Protected lblFile As Label
Protected filMyFile As HtmlInputFile
Protected lblInfo As System.Web.UI.WebControls. Label
Protected cmdSend As System.Web.UI.WebControls. Button
Protected imgFile As System.Web.UI.WebControls. Image
Protected imgDB As System.Web.UI.WebControls. Image
Protected lblText1 As System.Web.UI.WebControls. Label
Protected lblText2 As System.Web.UI.WebControls. Label
Protected Overrides Sub OnInit(e As EventArgs)
InitializeComponent()
MyBase.OnInit(e)
End Sub
Private Sub InitializeComponent()
AddHandler Me.Load, New System.EventHandler(Addres sOf Me.Page_Load)
AddHandler Me.cmdSend.Click, New System.EventHandler(Addres sOf Me.cmdSend_Click)
End Sub
Also
make this line as
Private Sub cmdSend_Click(sender As Object, e As System.EventArgs) Handles cmdSend.click
and
make this line as
Private Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
2. also, which version of .Net u use?
3. remove the following lines and check
Protected lblFile As Label
Protected filMyFile As HtmlInputFile
Protected lblInfo As System.Web.UI.WebControls.
Protected cmdSend As System.Web.UI.WebControls.
Protected imgFile As System.Web.UI.WebControls.
Protected imgDB As System.Web.UI.WebControls.
Protected lblText1 As System.Web.UI.WebControls.
Protected lblText2 As System.Web.UI.WebControls.
Protected Overrides Sub OnInit(e As EventArgs)
InitializeComponent()
MyBase.OnInit(e)
End Sub
Private Sub InitializeComponent()
AddHandler Me.Load, New System.EventHandler(Addres
AddHandler Me.cmdSend.Click, New System.EventHandler(Addres
End Sub
Also
make this line as
Private Sub cmdSend_Click(sender As Object, e As System.EventArgs) Handles cmdSend.click
and
make this line as
Private Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
ASKER
.net 3.5
The example that I downloaded for C# works on my system.
But the Converted one does not work.
The error hits the top line
Inports System
Error:
Compiler Error Message: ASPNET: Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends the correct base class (e.g. Page or UserControl).
I made the modifications, but it is stopping on the first line, and not going any further.
I changed my default.aspx top line
<%@ page Language="VB" CodeFile="default.aspx.vb" Inherits="default" %>
to this
<%@ Page Language="VB" CodeFile="default.aspx.vb" Inherits="default" %>
With a Capital P for Page
And still the same error.
@kaufmed
Provide an example of the fileupload control that uploads to a specify location
And inserts the filename into the database, and I will be more than happy to use it, but so for, I have had no luck in that area in finding an example.
The example that I downloaded for C# works on my system.
But the Converted one does not work.
The error hits the top line
Inports System
Error:
Compiler Error Message: ASPNET: Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends the correct base class (e.g. Page or UserControl).
I made the modifications, but it is stopping on the first line, and not going any further.
I changed my default.aspx top line
<%@ page Language="VB" CodeFile="default.aspx.vb"
to this
<%@ Page Language="VB" CodeFile="default.aspx.vb"
With a Capital P for Page
And still the same error.
@kaufmed
Provide an example of the fileupload control that uploads to a specify location
And inserts the filename into the database, and I will be more than happy to use it, but so for, I have had no luck in that area in finding an example.
ASKER
In IIS
ASP.net 2.0
ASP.net 2.0
><%@ Page Language="VB" CodeFile="default.aspx.vb" Inherits="default" %>
Is the name of class in default.aspx.vb "default"?
>Inports System
Should be
Imports System
Is the name of class in default.aspx.vb "default"?
>Inports System
Should be
Imports System
ASKER
It is, I misspelled it wrong when I types it in here.
The dafault.
Please look at the code that I provided above, I am not really sure about the inherit.
I know in my other code, it inherits the [Public Class ]
But when I tried to add this one which is: WebForm1
It gave me an error.
Please look at my code that I supplied at the top of the thread.
Thank You
Carrzkiss
The dafault.
Please look at the code that I provided above, I am not really sure about the inherit.
I know in my other code, it inherits the [Public Class ]
But when I tried to add this one which is: WebForm1
It gave me an error.
Please look at my code that I supplied at the top of the thread.
Thank You
Carrzkiss
I think you need to change
<%@ page Language="VB" CodeFile="default.aspx.vb" Inherits="default" %>
to
<%@ page Language="VB" CodeFile="default.aspx.vb" Inherits="Webform1" %>
<%@ page Language="VB" CodeFile="default.aspx.vb"
to
<%@ page Language="VB" CodeFile="default.aspx.vb"
ASKER
Still the same error.
I need this to be VB, as that is what I am use to using.
I DO NOT want to get envolved with C++ coding and get myself screwed up.
I need this to be VB, as that is what I am use to using.
I DO NOT want to get envolved with C++ coding and get myself screwed up.
Where did the C++ come from?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
What I would expect would be along these lines (code cropped for clarity):
Markup
Code-behind
Markup
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="cmdSend" runat="server" Text="Button" />
</div>
</form>
</body>
Code-behind
Protected Sub cmdSend_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdSend.Click
If Me.FileUpload1.HasFile Then
Dim nFileLen As Integer = Me.FileUpload1.FileBytes.Length
If nFileLen > 0 Then
' Create a name for the file to store
Dim strFilename As String = Path.GetFileName(Me.FileUpload1.FileName)
WriteToFile(Server.MapPath(strFilename), Me.FileUpload1.FileBytes)
' Store it in database
Dim nFileID As Integer = WriteToDB(strFilename, myFile.ContentType, myData)
...
End If
End If
End Sub
ASKER
THANK YOU!!!
Works now.
That was all that was needed.
As well as removing the Protected
Elements that were already declared.
Thank you once again.
Have a great rest of the weekend.
Should not take me more than an hour to add this to my project now.
Thank you
Carrzkiss
Works now.
That was all that was needed.
As well as removing the Protected
Elements that were already declared.
Thank you once again.
Have a great rest of the weekend.
Should not take me more than an hour to add this to my project now.
Thank you
Carrzkiss
Glad to help :-)