-1

我无法弄清楚为什么我使用这些方法中的任何一种 (load_qc_form(); enable_qc_form();disable_qc_form();) 我得到“消息:对象引用未设置为对象的实例”。信息。

调用方法如何返回null?

这是我的整个代码。我是 C# 新手,正在尝试更新现有页面。有人能指出我正确的方向吗?

我希望能够从应用程序中的任何位置调用这些方法。

    namespace test_page
{
    using System;
    //using System.Windows.Forms;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;


    /// <summary>
    ///     Summary description for ProductionQC.
    /// </summary>
    /// 

    public class hendrix : System.Web.UI.UserControl
    {
        #region class_variable_declarations
        //Test Boxes
        protected System.Web.UI.WebControls.TextBox boxOrder;
        protected System.Web.UI.WebControls.TextBox boxBox;
        protected System.Web.UI.WebControls.TextBox boxPallet;
        protected System.Web.UI.WebControls.TextBox boxPulpTemp;
        protected System.Web.UI.WebControls.TextBox boxComments;
        protected System.Web.UI.WebControls.TextBox boxMajdef1;
        protected System.Web.UI.WebControls.TextBox boxMajdef2;
        protected System.Web.UI.WebControls.TextBox boxMajdef3;
        protected System.Web.UI.WebControls.TextBox boxMindef1;
        protected System.Web.UI.WebControls.TextBox boxMindef2;
        protected System.Web.UI.WebControls.TextBox boxMindef3;
        protected System.Web.UI.WebControls.TextBox boxSamples;
        //drop down lists
        protected System.Web.UI.WebControls.DropDownList listYN;
        protected System.Web.UI.WebControls.DropDownList listMaj1;
        protected System.Web.UI.WebControls.DropDownList listMaj2;
        protected System.Web.UI.WebControls.DropDownList listMaj3;
        protected System.Web.UI.WebControls.DropDownList listMin1;
        protected System.Web.UI.WebControls.DropDownList listMin2;
        protected System.Web.UI.WebControls.DropDownList listMin3;
        protected System.Web.UI.WebControls.DropDownList listRecorder;
        //labels 
        protected System.Web.UI.WebControls.Label lblPalletTag;
        protected System.Web.UI.WebControls.Label lblmajdef1;
        protected System.Web.UI.WebControls.Label lblmajdef2;
        protected System.Web.UI.WebControls.Label lblmajdef3;
        protected System.Web.UI.WebControls.Label lblmindef1;
        protected System.Web.UI.WebControls.Label lblmindef2;
        protected System.Web.UI.WebControls.Label lblmindef3;
        protected System.Web.UI.WebControls.Label lblPulpTemp;
        protected System.Web.UI.WebControls.Label lblComments;
        protected System.Web.UI.WebControls.Label lblPackageCorrect;
        protected System.Web.UI.WebControls.Label lblTotSamples;
        protected System.Web.UI.WebControls.Label lblrecorder;
        //Buttons
        protected System.Web.UI.WebControls.Button btnGo;
        protected System.Web.UI.WebControls.Button btnGo2;
        protected System.Web.UI.WebControls.Button btnGo3;
        protected System.Web.UI.WebControls.Button btnClear;
        //Gridviews
        protected System.Web.UI.WebControls.GridView grdDisplayOrd;
        protected System.Web.UI.WebControls.GridView grdDisplayBox;
        //Data connections - adapters
        protected System.Data.SqlClient.SqlCommand cmdData;
        protected System.Data.SqlClient.SqlConnection sqlConnection1;
        protected System.Data.SqlClient.SqlDataReader dataReader;
        #endregion class_variable_declarations



        private void Page_Load(object sender, System.EventArgs e)
        {
            //initializePage();
            // Put user code to initialize the page here
            if (!this.Page.IsPostBack)
            {
                //load_qc_form();
                //disable_qc_form();

            }

        }

        //private void initializePage()
        // {
        //  throw new NotImplementedException();
        //}


        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);

        }

        /// <summary>
        ///     Required method for Designer support - do not modify
        ///     the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
            this.btnGo.Click += new System.EventHandler(this.btnGo_Click);
            this.btnGo2.Click += new System.EventHandler(this.btnGo2_Click);
           // this.btnGo3.Click += new System.EventHandler(this.btnGo3_Click);
            this.btnClear.Click += new EventHandler(this.btnClear_Click);

            // 
            // sqlConnection1
            // 
            this.sqlConnection1.ConnectionString = "workstation id=\"******";packet size=4096;user id=******;data source=\"******";persis" +
                "t security info=True;initial catalog=****;password=**********";
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

       // public virtual string DisplayMember { get; set; }
        //get and display order data
        private void btnGo_Click(object sender, System.EventArgs e)
        {


            {

                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "Select  ord.order_number as Order#,var.description as Description,grd.description as Grade, " +
                                  "size.description as Size,pkg.description as Pkg, ord.container_type as Pallet " +
                                  "FROM t_item_master itm INNER JOIN t_order_detail ord (NOLOCK) ON ord.item_number = itm.item_number " +
                                  "LEFT OUTER JOIN t_package pkg (NOLOCK) ON pkg.package_name = itm.product_package_code " +
                                  "LEFT OUTER JOIN t_grade grd (NOLOCK) ON grd.grade_id = itm.grade_id " +
                                  "LEFT OUTER JOIN t_size size (NOLOCK) ON size.size_id = itm.size_id AND size.package_id = pkg.package_id " +
                                  "LEFT OUTER JOIN t_variety var (NOLOCK) ON var.variety_id = itm.variety_id " +
                                  "WHERE order_number like @order_number";

                cmd.Parameters.Add("@order_number", SqlDbType.VarChar).Value = boxOrder.Text;

                cmd.CommandType = CommandType.Text;
                cmd.Connection = this.sqlConnection1;
                this.sqlConnection1.Open();

                this.grdDisplayOrd.Visible = true;
                SqlDataAdapter adpt = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                adpt.Fill(ds);
                grdDisplayOrd.DataSource = ds;
                grdDisplayOrd.DataBind();

                this.sqlConnection1.Close();
            }

        }
        // get and display box data
        private void btnGo2_Click(object sender, System.EventArgs e)
        {
            {

                load_qc_form();
                SqlCommand cmd = new SqlCommand();
                HyperLink test_link1 = new HyperLink();
                cmd.CommandText = "select " +
                                   "item_number as Item#, " +
                                   "description as Desctiption, " +
                                   "grower_lot as Lot#, " +
                                   "pack_date as Packed " +
                                   "from v_box_data where box_id = @box_id";

                cmd.Parameters.Add("@box_id", SqlDbType.VarChar).Value = boxBox.Text;

                cmd.CommandType = CommandType.Text;
                cmd.Connection = this.sqlConnection1;
                this.sqlConnection1.Open();

                this.grdDisplayBox.Visible = true;
                SqlDataAdapter adpt = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                adpt.Fill(ds);
                grdDisplayBox.DataSource = ds;
                grdDisplayBox.DataBind();

                this.sqlConnection1.Close();
            }
        }
        private void load_qc_form()
        {
            #region declare_variables
           // string pallet_tag;
            //string correct_package;
            //string total_samples;
            //string majdef1;
            //string majdef2;
            //string majdef3;
           // string mindef1;
           // string mindef2;
           // string mindef3;
           // string majcnt1;
           // string majcnt2;
            //string majcnt3;
           // string mincnt1;
           // string mincnt2;
           // string mincnt3;
           // string pulptemp;
           // string comments;
            string recorder;
            #endregion declare_variables

            #region fill_list_recorder
            try
            {
                listRecorder.Items.Clear();

                recorder = "select EMPID,FIRSTNAME+' '+LASTNAME from t_famous_employee " +
                            "where COSTCENTERNAME like 'Quality Control - 2011' " +
                            "and HOMECREWID LIKE 'Shipping' ";


                cmdData = new System.Data.SqlClient.SqlCommand(recorder, this.sqlConnection1);
                cmdData.Connection = this.sqlConnection1;
                this.sqlConnection1.Open();
                SqlDataAdapter da = new SqlDataAdapter(cmdData);
                DataTable dt = new DataTable();
                da.Fill(dt);
                listRecorder.DataBind();


                    listRecorder.DataSource = dt;
                    listRecorder.DataValueField = "EMPID";
                    listRecorder.DataTextField = "FIRSTNAME+' '+LASTNAME";
                    this.listRecorder.Items.Insert(0, "FIRSTNAME+' '+LASTNAME");

            }
            catch (Exception) { }
            this.dataReader.Close();

            #endregion


        }

        private void btnGo3_Click(object sender, System.EventArgs e)
        {
            {

            }

        }

        private void btnClear_Click(object sender, System.EventArgs e)
        {
            {
                this.boxOrder.Text = "";
                this.boxBox.Text = "";
                this.grdDisplayOrd.Visible = false;
                this.grdDisplayBox.Visible = false;
                //disable_qc_form();
            }
        }
        private void enable_qc_form()
        {
             this.listMaj1.Enabled = true;
            this.listMaj2.Enabled = true;
            this.listMaj3.Enabled = true;
            this.listMin1.Enabled = true;
            this.listMin2.Enabled = true;
            this.listMin3.Enabled = true;
            this.listYN.Enabled = true;
            this.listMaj1.Enabled = true;
            this.listMaj2.Enabled = true;
            this.listMaj3.Enabled = true;
            this.listMin1.Enabled = true;
            this.listMin2.Enabled = true;
            this.listMin3.Enabled = true;
            this.boxComments.Enabled = true;
            this.boxMajdef1.Enabled = true;
            this.boxMajdef2.Enabled = true;
            this.boxMajdef3.Enabled = true;
            this.boxMindef1.Enabled = true;
            this.boxMindef2.Enabled = true;
            this.boxMindef3.Enabled = true;
            this.boxPallet.Enabled = true;
            this.boxPulpTemp.Enabled = true;
            this.boxSamples.Enabled = true;
            this.lblComments.Enabled = true;
            this.lblmajdef1.Enabled = true;
            this.lblmajdef2.Enabled = true;
            this.lblmajdef3.Enabled = true;
            this.lblmindef1.Enabled = true;
            this.lblmindef2.Enabled = true;
            this.lblmindef3.Enabled = true;
            this.lblPackageCorrect.Enabled = true;
            this.lblPalletTag.Enabled = true;
            this.lblPulpTemp.Enabled = true;
            this.lblTotSamples.Enabled = true;
        }
        private void disable_qc_form()
        {
                this.listMaj1.Enabled = false;
                this.listMaj2.Enabled = false;
                this.listMaj3.Enabled = false;
                this.listMin1.Enabled = false;
                this.listMin2.Enabled = false;
                this.listMin3.Enabled = false;
                this.listYN.Enabled = false;
                this.listMaj1.Enabled = false;
                this.listMaj2.Enabled = false;
                this.listMaj3.Enabled = false;
                this.listMin1.Enabled = false;
                this.listMin2.Enabled = false;
                this.listMin3.Enabled = false;
                this.boxComments.Enabled = false;
                this.boxMajdef1.Enabled = false;
                this.boxMajdef2.Enabled = false;
                this.boxMajdef3.Enabled = false;
                this.boxMindef1.Enabled = false;
                this.boxMindef2.Enabled = false;
                this.boxMindef3.Enabled = false;
                this.boxPallet.Enabled = false;
                this.boxPulpTemp.Enabled = false;
                this.boxSamples.Enabled = false;
                this.lblComments.Enabled = false;
                this.lblmajdef1.Enabled = false;
                this.lblmajdef2.Enabled = false;
                this.lblmajdef3.Enabled = false;
                this.lblmindef1.Enabled = false;
                this.lblmindef2.Enabled = false;
                this.lblmindef3.Enabled = false;
                this.lblPackageCorrect.Enabled = false;
                this.lblPalletTag.Enabled = false;
                this.lblPulpTemp.Enabled = false;
                this.lblTotSamples.Enabled = false;
            }


        }

    }

所以我需要能够调用该方法来测试我的第一个下拉列表。

预先感谢您的帮助!!

4

4 回答 4

1
this.dataReader.Close();

dataReader 似乎没有在任何地方创建。

于 2012-07-27T23:27:02.127 回答
1

我没有看到您的控件在任何地方被初始化,它们可能具有null

    protected System.Web.UI.WebControls.TextBox boxOrder;
    protected System.Web.UI.WebControls.TextBox boxBox;
    protected System.Web.UI.WebControls.TextBox boxPallet;
    protected System.Web.UI.WebControls.TextBox boxPulpTemp;
    ...

并且您在方法中引用了它们,例如

 private void enable_qc_form()
 {
     this.listMaj1.Enabled = true; //accessing properties (e.g .Enabled) or method of an object that is not 
     this.listMaj2.Enabled = true; //initialised results in Object reference not set
     this.listMaj3.Enabled = true;
     ...
 }

InitializeComponent()没有初始化这些变量。如果您使用设计器,则在将控件添加到 UserControl 时,应该已经为您生成了所需的代码

于 2012-07-27T23:36:53.127 回答
0

您必须先创建(新)对象才能使用它们

更改您的代码

private void InitializeComponent()
{
    this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
    this.btnGo.Click += new System.EventHandler(this.btnGo_Click);
    this.btnGo2.Click += new System.EventHandler(this.btnGo2_Click);
    // this.btnGo3.Click += new System.EventHandler(this.btnGo3_Click);
    this.btnClear.Click += new EventHandler(this.btnClear_Click);
    ...

到:

private void InitializeComponent()
{
    this.btnGo = new Button();
    this.btnGo2 = new Button();
    this.btnGo3 = new Button();
    this.btnClear = new Button();
    this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
    this.btnGo.Click += new System.EventHandler(this.btnGo_Click);
    this.btnGo2.Click += new System.EventHandler(this.btnGo2_Click);
  // this.btnGo3.Click += new System.EventHandler(this.btnGo3_Click);
    this.btnClear.Click += new EventHandler(this.btnClear_Click); 
    ...

最好将控件从工具栏拖放到 aspx 文件并在 aspx.cs 文件中使用它们

于 2012-07-27T23:48:38.633 回答
0

问题是表单 ascx 页面中有错字。所以本质上我是在声明一个不存在的盒子!!与大多数编程问题一样,魔鬼在细节中!!感谢大家的帮助。事实证明,我只需要学习如何打字!

于 2012-07-30T21:30:13.960 回答