通用处理程序不返回值

时间:2015-05-21 09:31:21

标签: c# asp.net generic-handler

处理程序未返回图像。如果我删除条件语句,处理程序返回图像。这是我的代码

protected void Button_Hented_Click(object sender, EventArgs e)
     {
         string Hejsa;

         SqlConnection conn = new SqlConnection();
         conn.ConnectionString = ConfigurationManager.ConnectionStrings["DatabaseConnectionString1"].ToString();

         SqlCommand cmd = new SqlCommand();
         cmd.Connection = conn;
         cmd.CommandText = "SELECT * FROM Transactioner";



         conn.Open();
         SqlDataReader reader = cmd.ExecuteReader();

         if (reader.Read())
         {

             Hejsa = reader["Id"].ToString();



             foreach (DataGridItem item in GridView1.Items)
             {
                 CheckBox Cb = item.Cells[0].Controls[1] as CheckBox;


                 if (Cb.Checked)
                 {

                     SqlConnection conn2 = new SqlConnection();
                     conn2.ConnectionString =
                         ConfigurationManager.ConnectionStrings["DatabaseConnectionString1"].ToString();
                     SqlCommand cmd2 = new SqlCommand();
                     cmd2.Connection = conn2;

                     cmd2.CommandText = "UPDATE Transactioner"
                         + " SET Afhented = @Afhented"
                         + " where Id = @Id";


                     cmd2.Parameters.Add("@Afhented", SqlDbType.NVarChar).Value = "Ja";
                     cmd2.Parameters.Add("@Id", SqlDbType.Int).Value = Hejsa;


                     conn2.Open();
                     cmd2.ExecuteNonQuery();
                     conn2.Close();

                 }

             }
             Response.Redirect(Request.RawUrl);

         }
         conn.Close();

        }

我不知道我哪里出错了?任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:1)

首先,我建议以不同的方式进行投射,因为您的代码可能会导致无效的强制转换异常:

Byte[] br = dr[0] as Byte[];

然后检查null

if (br != null && br.Length > 1)

然后写下文件:

context.Response.ContentType = "image/jpeg"; // or whatever type you're using
context.Response.BinaryWrite(br);

并替换

context.Response.End();

HttpContext.Current.ApplicationInstance.CompleteRequest();

因为我注意到某些浏览器不喜欢Response.End()

希望这是有用的。祝你好运!

答案 1 :(得分:0)

我可以注意到,在你的代码br.Length总是少于1

试试此代码段link

{
SqlConnection con = new SqlConnection("Server=Darkover;uid=<username>;pwd=<strong password>;database=northwind");
SqlDataAdapter da = new SqlDataAdapter("Select * From MyImages", con);
SqlCommandBuilder MyCB = new SqlCommandBuilder(da);
DataSet ds = new DataSet("MyImages");

byte[] MyData= new byte[0];

da.Fill(ds, "MyImages");
DataRow myRow;
myRow=ds.Tables["MyImages"].Rows[0];

MyData =  (byte[])myRow["imgField"];
int ArraySize = new int();
ArraySize = MyData.GetUpperBound(0); 

FileStream fs = new FileStream(@"C:\winnt\Gone Fishing2.BMP", FileMode.OpenOrCreate, FileAccess.Write);
fs.Write(MyData, 0,ArraySize);
fs.Close();
}

答案 2 :(得分:0)

试试这个

Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        Dim _ConnectionString As String = ConfigurationManager.AppSettings("ConnectionString")
        Dim UserId As String = context.Request.QueryString("Id")
        Dim con As New SqlConnection(_ConnectionString)
        Try
            con.Open()
            Dim cmd As New SqlCommand(Convert.ToString("select UserPhoto from tblEmployee where EmpId=") & UserId, con)
            Dim dr As SqlDataReader = cmd.ExecuteReader()
            dr.Read()
            If Not dr.IsDBNull(0) Then
                context.Response.BinaryWrite(DirectCast(dr(0), Byte()))
            Else
                Dim imgpath As String = context.Server.MapPath("~/images/images.jpg")
                Dim byteArray As Byte() = File.ReadAllBytes(imgpath)
                context.Response.BinaryWrite(byteArray)
            End If

        Catch ex As Exception
            Throw ex
        End Try
    End Sub