-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

問題は、フォームの ascx ページにタイプミスがあったことです。つまり、本質的に、存在しないボックスを宣言していました!! ほとんどのプログラミングの問題と同様に、悪魔は細部に宿る!! ご協力ありがとうございました。結局のところ、私はタイプする方法を学ぶ必要があるだけです!!

于 2012-07-30T21:30:13.960 に答える
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 に答える