我正在使用ImageHandler的.ashx文件。
这是我收到的错误消息System.Data.dll中发生类型'System.InvalidOperationException'的异常,但未在用户代码中处理
附加信息:没有数据时读取的尝试无效。
This part showing the error
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
context.Response.ContentType = dr["Image_Type"].ToString();
context.Response.BinaryWrite((byte[])dr["Profile_Picture"]);
dr.Close();
这是我的ImageHandler.ashx:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.IO;
public class ImageHandler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["Register"].ConnectionString);
myConnection.Open();
string sql = "Select Profile_Picture, Image_Type from Member where Login_Id=@Name";
SqlCommand cmd = new SqlCommand(sql, myConnection);
cmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value = context.Request.QueryString["Login_Id"];
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
context.Response.ContentType = dr["Image_Type"].ToString();
context.Response.BinaryWrite((byte[])dr["Profile_Picture"]);
dr.Close();
myConnection.Close();
}
public bool IsReusable {
get {
return false;
}
}
}
这是我的UpdateProfile.aspx.cs:
protected void btnSave_Click(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["Register"].ConnectionString;
string filename = Path.GetFileName(fuProfilePicture.PostedFile.FileName);
string contentType = fuProfilePicture.PostedFile.ContentType;
using (Stream fs = fuProfilePicture.PostedFile.InputStream)
{
using (BinaryReader br = new BinaryReader(fs))
{
byte[] bytes = br.ReadBytes((Int32)fs.Length);
using (SqlConnection con1 = new SqlConnection(constr))
{
string query1 = "Update Member Set Name = @name,
Email = @email, Phone_Number = @phonenumber,
Gender = gender, Date_Of_Birth = @dob,
Password = @password, Login_Id = @loginid,
Student_ID = @studentid,
Profile_Picture = @profilepicture
WHERE Login_Id = '" + Request.QueryString["Login_Id"] + "'";
using (SqlCommand cmd1 = new SqlCommand(query1))
{
cmd1.Connection = con1;
cmd1.Parameters.AddWithValue("@imagename", filename);
cmd1.Parameters.AddWithValue("@imagetype", contentType);
cmd1.Parameters.AddWithValue("@profilepicture", bytes);
cmd1.Parameters.AddWithValue("@name", txtName.Text);
cmd1.Parameters.AddWithValue("@email", txtEmail.Text);
cmd1.Parameters.AddWithValue("@phonenumber", txtContactNumber.Text);
cmd1.Parameters.AddWithValue("@gender", ddlGender.SelectedItem.ToString());
cmd1.Parameters.AddWithValue("@dob", ddlDay.SelectedItem.ToString() + "/" + ddlMonth.SelectedItem.ToString() + "/" + txtYear.Text);
cmd1.Parameters.AddWithValue("@password", txtPassword.Text);
cmd1.Parameters.AddWithValue("@loginid", txtUsername.Text);
cmd1.Parameters.AddWithValue("@studentid", txtStudentID.Text);
con1.Open();
cmd1.ExecuteNonQuery();
con1.Close();
}
}
}
}
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<script type = 'text/javascript'>");
sb.Append("window.onload=function(){");
sb.Append("alert('");
sb.Append("Update Successfuly!");
sb.Append("')};");
sb.Append("</script>");
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());
}
}
答案 0 :(得分:0)
也许是因为你忘记了性别上的@符号:
Phone_Number = @phonenumber, Gender = gender,