当尝试在处理程序中调用应用程序变量时为什么会出现此错误?

问题描述 投票:0回答:1

我尝试调用应用程序变量时得到了此信息,尽管我非常确定它编写正确。问题是您无法在处理程序中调用应用程序变量,还是我尝试调用它的方式?

所以这是我的处理程序(ashx.cs)代码:

    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;
using System.Data;
using System.IO;

namespace KEY_web_v1
{
    /// <summary>
    /// Summary description for HandlerImage
    /// </summary>
    public class HandlerImage : IHttpHandler
    {
        int id_master = 19;
        int id_handler = (int)Application["id_handler"];
        public void ProcessRequest(HttpContext context)
        {
            string connectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\kfirm\Desktop\KEY_web_v1\KEY_web_v1\App_Data\Database1.mdf;Integrated Security=True";
            SqlConnection conn = new SqlConnection(connectionString);
            //SqlConnection conn = new SqlConnection("data source=.\\sqlexpress; initial catalog=SlideShow; integrated security=true");//ORIGINAL - "data source=.\\sqlexpress; initial catalog=SlideShow; integrated security=true" //I think he's just making a dataset, I can edit later if it doesnt work or add this to the global.asax
            SqlCommand comm = new SqlCommand();
            comm.Connection = conn;

            comm.CommandText = "SELECT * FROM Portfolio"; //MAKE A LOOP!!!!!!!!!!!!!!!!!!!!
            SqlDataAdapter da = new SqlDataAdapter(comm);
            DataSet dt = new DataSet();

            da.Fill(dt);

            byte[] image = (byte[])dt.Tables[0].Rows[id_master][2]; //MAKE A LOOP!!!!!!!!!!!!!!!!!!!!
            string conStr = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\kfirm\Desktop\KEY_web_v1\KEY_web_v1\App_Data\Database1.mdf;Integrated Security=True"; //where the BS begins
            SqlConnection conObj = new SqlConnection(conStr);
            string sql = string.Format("SELECT Image FROM Portfolio WHERE id=N'{0}'", id_master);
            SqlCommand command = new SqlCommand(sql, conObj);
            conObj.Open();
            SqlDataReader datareader = command.ExecuteReader();
            while (datareader.Read())
            {
                if (datareader["Image"].ToString() == "png")
                {
                    context.Response.ContentType = "image/png";
                    context.Response.BinaryWrite(image);
                    context.Response.Flush();
                    id_master++;
                }
                else
                {
                    if(datareader["Image"].ToString() == "jpg")
                    {
                        context.Response.ContentType = "image/jpg";
                        context.Response.BinaryWrite(image);
                        context.Response.Flush();
                        id_master++;
                    }
                    else
                    {
                        if(datareader["Image"].ToString() == "jpeg")
                        {
                            context.Response.ContentType = "image/jpeg";
                            context.Response.BinaryWrite(image);
                            context.Response.Flush();
                            id_master++;
                        }
                    }
                }
                conObj.Close();
                //where the BS ends
                //context.Response.ContentType = "image/jpeg";
                //context.Response.ContentType = "image/jpg";
                //context.Response.ContentType = "image/png";

                //context.Response.BinaryWrite(image);
                //context.Response.Flush();
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

这是我的Global.asax文件中的application_start代码:

protected void Application_Start(object sender, EventArgs e)
    {
        Application["admin"] = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True";
        Application["id_handler"] = 19;
    }

当我尝试在ashx.cs代码中调用变量时收到此错误:

enter image description here

为什么?

c# handler global-asax ashx
1个回答
0
投票

它是一个对象,应将其转换为字符串,然后解析试试这个

int x = int.Parse(Application["id_handler"].ToString());

或者您可以将TryParse用于空字符串或空字符串

© www.soinside.com 2019 - 2024. All rights reserved.