我尝试使用 ajaxfileupload 将图像(使用二进制)上传到我的数据库(sql server 2008)。我不确定我是否正确执行了后面的代码。当我尝试在不上传或不上传任何图片的情况下发送报告时,会弹出一些错误。我在下面发布了错误。请帮我看看出了什么问题。谢谢。
AJAX文件上传
<asp:AjaxFileUpload ID="AjaxFileUpload1" runat="server"
ThrobberID="myThrobber"
ContextKeys="fred"
AllowedFileTypes="jpg"
MaximumNumberOfFiles=1
UploadedComplete="AjaxFileUpload1_UploadedComplete"
/>
代码背后
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MP
{
public partial class Report : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
String fullname = (String)Session["fullname"];
String contact = (String)Session["contact"];
lblFullName.Text = fullname;
lblContact.Text = contact;
lblDateTime.Text = DateTime.Now.ToString();
Session["datetime"] = lblDateTime.Text;
}
protected void btnReport_Click(object sender, EventArgs e)
{
String fullname = (String)Session["fullname"];
String contact = (String)Session["contact"];
String datetime = (String)Session["datetime"];
String typeofcrime = ddlTOC.SelectedItem.Text;
String location = txtLocation.Text;
String detail = txtDetail.Text;
String picture = (String)Session["picture"];
if (picture.Equals(""))
{
SqlConnection conn = new SqlConnection("Data Source=localhost; Initial Catalog=project; Integrated Security=True");
conn.Open();
SqlCommand cmd = new SqlCommand("Insert into Report (fullname, contact, typeofcrime, location, CRdatetime, citizenreport) values ('" + fullname + "','" + contact + "','" + typeofcrime + "','" + location.Trim() + "','" + datetime + "','" + detail.Trim() + "')", conn);
cmd.ExecuteNonQuery();
lblMessage.Text = "Report Submitted.";
conn.Close();
txtDetail.Text = "";
txtLocation.Text = "";
}
else if (!picture.Equals(""))
{
SqlConnection conn = new SqlConnection("Data Source=localhost; Initial Catalog=project; Integrated Security=True");
conn.Open();
SqlCommand cmd = new SqlCommand("Insert into Report (fullname, contact, typeofcrime, location, CRdatetime, citizenreport, picture) values ('" + fullname + "','" + contact + "','" + typeofcrime + "','" + location.Trim() + "','" + datetime + "','" + detail.Trim() + "','" + picture + "')", conn);
cmd.ExecuteNonQuery();
lblMessage.Text = "Report Submitted.";
conn.Close();
txtDetail.Text = "";
txtLocation.Text = "";
}
}
protected void btnCancel_Click(object sender, EventArgs e)
{
txtDetail.Text = "";
txtLocation.Text = "";
}
protected void AjaxFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
byte[] image = e.GetContents();
Session["picture"] = image;
}
}
}
错误(不上传图片时报告)
错误(当我按上传时)