c#Get Set返回null [英] c# Get Set return null

查看:1025
本文介绍了c#Get Set返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您尝试将数据传递到数据层从另一个方法中取回数据。但失败了。我在另一个c#类中使用get set方法来获取值,但它返回null



Hi try to pass the data to a data layer take the data back from another methods. But fails. I use get set method in another c# class to get the value but it return null

  public partial class _Default : Page
    {
        private string strFile = HttpContext.Current.Server.MapPath("~/Output/");
        private string strUpload = HttpContext.Current.Server.MapPath("~/Data/");
        private string strOutput = "" + DateTime.Now.ToString("ddMMyyy_hhmmss") + ".xls";
        private ReadExcel readExcel = new ReadExcel();
        private ExcelData excelData;
        private string sFileName;

        protected void Page_Load(object sender, EventArgs e)
        {
            excelData = new ExcelData();
            sFileName = string.Empty;
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if ((fileUploadDialog.PostedFile != null) && (fileUploadDialog.PostedFile.ContentLength > 0))
            {
                string fn = Path.GetFileName(fileUploadDialog.PostedFile.FileName);
                excelData.fileName = fn;
                sFileName = fn;
                string saveLocation = strUpload + "\\" + fn;
                try
                {
                    fileUploadDialog.PostedFile.SaveAs(saveLocation);
                   // Response.Write("");
                    lblStatus.Text = "Upload Status: The file has been uploaded. " + excelData.fileName ;

                }
                catch (Exception ex)
                {
                    Response.Write(ex);
                }
            }
            else
            {
                lblStatus.Text = "Please select a file to upload";
            }
        }

        protected void btnDisplay_Click(object sender, EventArgs e)
        {

            lblStatus.Text = "test" + sFileName  ;
//bothe sFileName and excelData.fileName will return null Why?
            //DataTable dt = readExcel.readCutomManualID(excelData.fileName);
        }
    }
}







namespace TGSTax.Models
{
    public class ExcelData
    {
       // public string fileName { get; set; }
        string _fileName;
        public string fileName
        {
            get
            {
                return this._fileName;
            }
            set
            {
                this._fileName = value;
            }
        }

        //public ExcelData()
        //{
        // public string fileName { get; set; }
        //}
    }
}







有没有人有任何想法?




Do anyone have any idea?

推荐答案

此逻辑仅适用于Windows而不是Web,因为Web已断开连接,因此在处理请求后您的对象将丢失。解决方案是存储对象然后重用。您可以根据需要考虑会话或缓存对象。
This logic works only in windows and not web because web is disconnected so your object are lost once the request is processed. The solution is to store the object and then reuse. You can think of session or cache object depending upon your requirement.


这篇关于c#Get Set返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆