I am using .ashx file for ImageHandler.
The is the error message that I received An exception of type 'System.InvalidOperationException' occurred in System.Data.dll but was not handled in user code
Additional information: Invalid attempt to read when no data is present.
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();
This is my 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;
}
}
}
This is my 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());
}
}
也许是因为您忘记了性别上的@符号:
Phone_Number = @phonenumber, Gender = gender,
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.