سؤال

I wrote a function to update a datagrid view but it is being replaced instead of appended.

I previously declared the following;

List<LineItem> Lines = new List<LineItem>(); // Creates the list of LINES
 public class LineItem{

       private string _itemNumber;
       private string _description;
       private string _unitPrice;
       private string _quantity;
       private string _netWeight;
       private string _netPrice;



       public string itemNumber
        {
            get
            {
                return _itemNumber;
            }
            set
            {
                _itemNumber = value;
            }
        }
       public string description
        {
            get
            {
                return _description;
            }
            set
            {
                _description = value;
            }
        }
       public string unitPrice
        {
            get
            {
                return _unitPrice;
            }
            set
            {
                _unitPrice = value;
            }
        }
       public string quantity
        {
            get
            {
                return _quantity;
            }
            set
            {
                _quantity = value;
            }
        }
       public string netWeight
        {
            get
            {
                return _netWeight;
            }
            set
            {
                _netWeight = value;
            }
        }
       public string netPrice
        {
            get
            {
                return _netPrice;
            }
            set
            {
                _netPrice = value;
            }
        }

The function not working is below;

protected void Button10_Click(object sender, EventArgs e)
    {
        if (TextBox16.Text == "")
        {
            Label20.Visible = true;
            Label20.Text = "Must enter a quantity!";
        }
        else
        {
            if (TextBox15.Text == "")
            {

            }
            else
            {
                int tempInt01 = Int32.Parse(TextBox16.Text);
                GlobalVariables.qlQuantity = tempInt01;
                GlobalVariables.qlItemNumber = TextBox15.Text;
                Label20.Visible = false;
                TextBox15.Text = "";
                TextBox16.Text = "";

                try
                {
                    string connectionString = "Data Source=DBSERVER01;Initial Catalog=TCM95;Integrated Security=False;User ID=sa;Password=foobar";
                    using (SqlConnection connection = new SqlConnection(connectionString))
                    {
                        connection.Open();
                        SqlCommand command = new SqlCommand("SELECT omi.ITMMAS_BASE.ID_ITEM, omi.ITMMAS_BASE.DESCR_1, omi.ITMMAS_LOC.PRICE_SELL_1, omi.ITMMAS_BASE.WGT_ITEM FROM omi.ITMMAS_BASE INNER JOIN omi.ITMMAS_COST ON omi.ITMMAS_BASE.ID_ITEM = omi.ITMMAS_COST.ID_ITEM INNER JOIN omi.ITMMAS_LOC ON omi.ITMMAS_BASE.ID_ITEM = omi.ITMMAS_LOC.ID_ITEM WHERE (omi.ITMMAS_BASE.ID_ITEM = '" + GlobalVariables.qlItemNumber + "') AND (omi.ITMMAS_LOC.ID_LOC = '11')", connection);
                        SqlDataReader myDataReader = command.ExecuteReader();
                        while (myDataReader.Read())
                        {
                            LineItem a1 = new LineItem();  // CREATES NEW Line Item
                            int tempInt0 = myDataReader.GetOrdinal("ID_ITEM");
                            GlobalVariables.qlItemNumber = myDataReader.GetString(tempInt0);
                            a1.itemNumber = GlobalVariables.qlItemNumber;
                            int tempInt1 = myDataReader.GetOrdinal("DESCR_1");
                            GlobalVariables.qlDescription = myDataReader.GetString(tempInt1);
                            a1.description = GlobalVariables.qlDescription;
                            int tempInt2 = myDataReader.GetOrdinal("PRICE_SELL_1");
                            GlobalVariables.qlPrice = myDataReader.GetDecimal(tempInt2);
                            a1.unitPrice = GlobalVariables.qlPrice.ToString();
                            int tempInt3 = myDataReader.GetOrdinal("WGT_ITEM");
                            GlobalVariables.qlWeight = myDataReader.GetDecimal(tempInt3);
                            a1.netWeight = GlobalVariables.qlWeight.ToString();
                            GlobalVariables.netWeight = GlobalVariables.netWeight + (GlobalVariables.qlWeight * GlobalVariables.qlQuantity);
                            a1.quantity = GlobalVariables.qlQuantity.ToString();
                            GlobalVariables.totalQuantity = GlobalVariables.totalQuantity + GlobalVariables.qlQuantity;
                            a1.netPrice = a1.unitPrice;
                            Lines.Add(a1);


                        }
                        connection.Close();
                    }

                }
                catch (SqlException ex)
                {
                    Label20.Visible = true;
                    Label20.Text = ex.ToString();


                }
                TextBox6.Text = GlobalVariables.netWeight.ToString();
                TextBox14.Text = GlobalVariables.totalQuantity.ToString();
                GridView1.Visible = true;// Sets visibility on Datalist1.
                GridView1.DataSource = Lines;
                GridView1.DataBind();
            }
        } // end of else for IF TEXTBOX16..
    }

Basically, I just want the Button10 function to add a LineItem to the List and Append to the DataGridView1, not overwrite it completly (as its doing now!).

هل كانت مفيدة؟

المحلول

To understand your problem, please read-up on .Net Page Lifecycle ... it will teach you what happens to the code on each page load, postback, etc...

The place where you have declared the list, will basically be wiped clean every time the page loads (when you click the button on the browser for example, it initiates a postback, and your entire class is "re-created" from scratch). There is no real "Page Specific Global Variables" in ASP.Net.

So for you, you will want to look at using the Session object, or ViewState object to store your list, for that user, for that page, in a global way.

Hope this helps.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top