VB.NET – Single file upload

WUI View

Home | Upload File | Close Window

Enter requested information:
  • Your name
    X
  • File title:
  • File category:
  • File type:
  • Select a file to upload:
  • Additional note:


< %@ Import Namespace="System.IO" %> < %@ Import Namespace="System.Data.SqlClient" %> < %@ Page Language="VB" %> < !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> Sub SubmitButton_Click(Source As Object, e As EventArgs) 'defining controls Dim UName As String Dim FTitle As String Dim FType As String Dim FCat As String Dim uComment As String Dim uDate As DateTime 'binding controls and converting them to strings UName = UsrName.Text.ToString FTitle = FileTitle.Text.ToString FType = FileType.Text.ToString FCat = FileCat.Text.ToString uComment = txtCmt.Text.ToString uDate = DateAndTime.Now.ToString If Not (File1.PostedFile Is Nothing) Then 'create db connection string Dim SQLString As String Dim ConnString As String ConnString = "Data Source=my-srv-mssql;Initial Catalog=intranet; Persist Security Info=True;User ID=usr;Password=pwd" Try If FType = "wmv" Then File1.PostedFile.SaveAs("C:\inetpub\wwwwroot\my_website\doc_vault\" &_ FTitle + "." + FType) Span1.InnerHtml = "<span class='positive_conf'>Upload Successful - <a href='Get-File.aspx'>Upload another file!</a></span>" Else 'example of user alert just in case if decide to stick with certain type of file MsgLabel.Text = "It looks like you are trying to upload the video, but you didn't specify wmv file type!" End If 'SQL statement SQLString = "INSERT into UplTbl(wUser, fTitle, fType, fCat, fComment, uDate) &_ VALUES (@uName, @FTitle, @FType, @FCat, @uComment, @uDAte)" 'write to SQL Dim SQLConn As New SqlConnection() Dim SQLCmd As New SqlCommand() SQLConn.ConnectionString = ConnString SQLConn.Open() 'open SQL connection SQLCmd.Parameters.AddWithValue("@uName", UName) SQLCmd.Parameters.AddWithValue("@FTitle", FTitle) SQLCmd.Parameters.AddWithValue("@FType", FType) SQLCmd.Parameters.AddWithValue("@FCat", FCat) SQLCmd.Parameters.AddWithValue("@uComment", uComment) SQLCmd.Parameters.AddWithValue("@uDate", uDate) SQLCmd.Connection = SQLConn SQLCmd.CommandText = SQLString SQLCmd.ExecuteNonQuery() 'execute SQL connection SQLConn.Close() 'close SQL connection Catch ex As Exception Span1.InnerHtml = "Error saving file <b>C:\\" & _ File1.Value & "</b>
" & ex.ToString() End Try End If End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>File upload <link href="Styles/Site.css" rel="stylesheet" type="text/css" /> </head> <body> <div class="page"> <a href="Default.aspx">Home</a> | <a href="Upload.aspx">Upload File</a> | <a href="">Close Window</a> <fieldset> <legend>Enter requested information: <form id="Form2" runat="server" enctype="multipart/form-data"> <ul class="upload_ul"> <li>Your name<br /> <asp:textbox ID="UsrName" runat="server" ToolTip="Your name" class="txtbox_text"> </asp:textbox> <asp:requiredfieldvalidator id="name" runat="server" ErrorMessage="X" ControlToValidate="UsrName"> </asp:requiredfieldvalidator> </li> <li>File title:<br /> <asp:textbox ID="FileTitle" runat="server" ToolTip="File title" class="txtbox_text"></asp:textbox> <asp:requiredfieldvalidator id="title" runat="server" ErrorMessage="X" ControlToValidate="FileTitle"> </asp:requiredfieldvalidator> </li> <li>File category:<br /><asp:dropdownlist ID="FileCat" runat="server" class="txtbox_text"> <asp:listitem Value="" Text="Please select" Selected></asp:listitem> <asp:listitem Value="SF" Text="Sci-Fi"></asp:listitem> <asp:listitem Value="WW" Text="Wild West"></asp:listitem> <</asp:listitem> </asp:dropdownlist> <asp:requiredfieldvalidator id="category" runat="server" ErrorMessage="X" ControlToValidate="FileCat"> </li> <li>File type:<br /> <asp:dropdownlist ID="FileType" runat="server" class="txtbox_text"> <asp:listitem Value="" Text="Please select" Selected></asp:listitem> < </asp:listitem> < </asp:listitem> <asp:listitem Value="avi" Text="Audio Video Interleave File"></asp:listitem> </asp:dropdownlist> <asp:requiredfieldvalidator id="type" runat="server" ErrorMessage="X" ControlToValidate="FileType"> </asp:requiredfieldvalidator> </li> <li>Select a file to upload:<br /> <input type="file" id="File1" runat="Server"/> <asp:requiredfieldvalidator id="RequiredFieldValidator1" runat="server" ErrorMessage="X" ControlToValidate="File1"> </asp:requiredfieldvalidator> </li> <li>Additional note:
<asp:textbox ID="txtCmt" runat="server" ToolTip="User comment" class="txtbox_text" TextMode="MultiLine"></asp:textbox> </li> <li><asp:label ID="MsgLabel" runat="server" class="error_lbl"> <br /></li> <li> <input type="submit" id="Submit1" runat="Server" value="Upload File" OnServerClick="SubmitButton_Click"/></li> </ul> </form> </fieldset> </div> </body> </html>

Maximum request length exceeded

Having issues uploading files? Getting message like “maximum request length exceeded?” The solution to this is simple; add the following code to your configuration file:


<configuration>
    <system.web>
        <httpRuntime maxRequestLength="35000" />
    </system.web>
</configuration>

* set the maxRequestLength to whatever your need might be.

SQL Primary Key Constraint on alter table

– Login to your server MS SQL Management Studio Express

– Expand the database; expand the tables folder, right click on the table “[your table name]”, click “Open Table.” Make sure the primary key column(s) have been declared to not contain NULL values (when the table was first created). With the open table click on “Show SQL Pane” (Management Studio Express toolbar). Type the following SQL statement in the SQL pane:

SQL Server / MS Access:
 ALTER TABLE MyTable ADD PRIMARY KEY (Id) 

 

– Click on exclamation sign to execute SQL statement, or right-click in the SQL pane and select “Execute SQL.”

If you did everything correct you will get the confirmation message that SQL command executed successful.
 

Solution for defining a primary key constraint on multiple columns, use the following SQL syntax:

SQL Server / MS Access:

ALTER TABLE MyTable
ADD CONSTRAINT pk_ProductID PRIMARY KEY (Id,ItemName)

 

In case you need to drop primary key here is syntax:

SQL Server / MS Access:

ALTER TABLE MyTable
DROP CONSTRAINT id