我想在我的系统上传PDF文件和图片文件,我有2个FileUpload控件,fileUpload1上传pdf文件,fileUpload2上传图片文件,但是当我尝试保存在数据库中时收到以下错误,我不知道问题出在哪里:
这是我的表结构:
这是我的客户代码:
Imports System
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Imports System.Threading
Imports System.Globalization
Partial Class addevent
Inherits System.Web.UI.Page
Protected Sub ButtonAddEvent_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonAddEvent.Click
Dim fileUpload1 As FileUpload = CType(Me.FindControl("fileUpload1"), FileUpload)
Dim fileUpload2 As FileUpload = CType(Me.FindControl("fileUpload2"), FileUpload)
'Make sure a file has been successfully uploaded
If fileUpload1.PostedFile Is Nothing OrElse String.IsNullOrEmpty(fileUpload1.PostedFile.FileName) OrElse fileUpload1.PostedFile.InputStream Is Nothing Then
Label1.Text = "Please Upload Valid PDF file"
Exit Sub
End If
If fileUpload2.PostedFile Is Nothing OrElse String.IsNullOrEmpty(fileUpload2.PostedFile.FileName) OrElse fileUpload2.PostedFile.InputStream Is Nothing Then
Label1.Text = "Please Upload Valid picture file"
Exit Sub
End If
'Make sure we are dealing with a JPG or GIF file
Dim extension As String = System.IO.Path.GetExtension(fileUpload1.PostedFile.FileName).ToLower()
Dim MIMEType As String = Nothing
Select Case extension
Case ".gif"
MIMEType = "image/gif"
Case ".jpg", ".jpeg", ".jpe"
MIMEType = "image/jpeg"
Case ".png"
MIMEType = "image/png"
Case Else
'Invalid file type uploaded
Label1.Text = "Not a Valid file format"
Exit Sub
End Select
Dim fileextension As String = System.IO.Path.GetExtension(fileUpload2.PostedFile.FileName).ToLower()
Dim fileMIMEType As String = Nothing
Select Case extension
Case ".pdf"
MIMEType = "file/pdf"
Case Else
'Invalid file type uploaded
Label2.Text = "Not a Valid file format"
Exit Sub
End Select
'Connect to the database and insert a new record into upload_image
Dim connString As String = ConfigurationManager.ConnectionStrings("ConnectionString1").ConnectionString
'"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\eventdb.mdf;Integrated Security=True;User Instance=True"
Dim con As SqlConnection = New SqlConnection(connString)
Dim cmdQuery As String = "INSERT INTO eventinfo(venue,eventTitle,startDate,endDate,description,layout,eventPDF,layoutData) VALUES (@venue,@eventTitle,@startDate,@endDate,@description,@layout,@eventPDF.@layoutData)"
Dim cmd = New SqlCommand(cmdQuery)
cmd.Connection = con
cmd.CommandType = CommandType.Text
con.Open()
cmd.Parameters.AddWithValue("@eventTitle", txtEventTitle.Text)
cmd.Parameters.AddWithValue("@venue", txtEventLocation.Text)
cmd.Parameters.AddWithValue("@description", txtEventDescription.Text)
Dim fmt As String = "dd/MM/yyyy"
Dim dtStart As DateTime = DateTime.ParseExact(txtStartDate.Text, fmt, Nothing)
Dim dtEnd As DateTime = DateTime.ParseExact(txtEndDate.Text, fmt, Nothing)
cmd.Parameters.AddWithValue("@startDate", dtStart)
cmd.Parameters.AddWithValue("@endDate", dtEnd)
Dim totalDay As Integer = DateDiff(DateInterval.Day, dtStart, dtEnd)
cmd.Parameters.AddWithValue("@totalDay", totalDay)
cmd.Parameters.AddWithValue("@layout", MIMEType)
cmd.Parameters.AddWithValue("@eventPDF", fileMIMEType)
'Load FileUpload's InputStream into Byte array
Dim imageBytes(fileUpload1.PostedFile.InputStream.Length) As Byte
fileUpload1.PostedFile.InputStream.Read(imageBytes, 0, imageBytes.Length)
cmd.Parameters.AddWithValue("@layoutData", imageBytes)
MsgBox("The picture is successful uploaded", 0, "Saved")
cmd.ExecuteNonQuery()
cmd.Parameters.Clear()
con.Close()
Response.Redirect("~/addbooth.aspx")
End Sub
End Class
我的客户代码:
<%@ Page Language="VB" MasterPageFile="~/MasterPageAdmin.master" AutoEventWireup="false" CodeFile="addevent.aspx.vb" Inherits="addevent" title="Untitled Page" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="ContentPlaceHolder2" runat="server"
contentplaceholderid="ContentPlaceHolder2">
<table class="style8">
<tr>
<td> Add New Event</td>
<td> Event Title:</td>
<td><asp:TextBox ID="txtEventTitle" runat="server"></asp:TextBox></td></tr>
<tr>
<td>Event Location:</td>
<td><asp:TextBox ID="txtEventLocation" runat="server"></asp:TextBox></td>
<tr><td >Start Date:</td>
<td><asp:TextBox ID="txtStartDate" runat="server" MaxLength="10" ></asp:TextBox>
<asp:TextBoxWatermarkExtender ID="txtStartDate_TextBoxWatermarkExtender"
runat="server" Enabled="True" TargetControlID="txtStartDate"
WatermarkCssClass="WaterMark" WatermarkText="DD/MM/YYYY">
</asp:TextBoxWatermarkExtender>
<asp:CalendarExtender ID="txtStartDate_CalendarExtender" runat="server"
Enabled="True" Format="dd/MM/yyyy" TargetControlID="txtStartDate"
TodaysDateFormat="dd/MM/yyyy">
</asp:CalendarExtender>
</td>
</tr>
<tr><td>End Date:</td>
<td><asp:TextBox ID="txtEndDate" runat="server" MaxLength="10"></asp:TextBox>
<asp:TextBoxWatermarkExtender ID="txtEndDate_TextBoxWatermarkExtender"
runat="server" Enabled="True" TargetControlID="txtEndDate"
WatermarkCssClass="WaterMark" WatermarkText="DD/MM/YYYY">
</asp:TextBoxWatermarkExtender>
<asp:CalendarExtender ID="txtEndDate_CalendarExtender" runat="server"
Enabled="True" Format="dd/MM/yyyy" TargetControlID="txtEndDate"
TodaysDateFormat="dd/MM/yyyy">
</asp:CalendarExtender>
</td></tr>
<tr><td >Event Description:</td>
<td><asp:TextBox ID="txtEventDescription" runat="server" TextMode="multiline" Wrap="true" Height="100px" Width="215px" ></asp:TextBox></td>
</tr>
<tr><td >Event PDF:</td>
<td ><asp:FileUpload ID="fileUpload1" runat="server" /></td>
<td >asp:Label ID="Label1" runat="server"></asp:Label></td>
</tr>
<tr>
<td>Event Layout:</td>
<td><asp:FileUpload ID="fileUpload2" runat="server" /></td>
<td><asp:Label ID="Label2" runat="server"></asp:Label></td>
</tr>
<tr>
<td><asp:Button ID="ButtonAddEvent" runat="server" Text="Add New Event"/></td>
</tr>
</table>
</asp:Content>