Need some Advice on ArrayList or alternative method for database

waheedrafiq

Member
Joined
Jan 4, 2015
Messages
11
Programming Experience
1-3
Hi all

I have done 5 hours of research today and have found this link on cSharpforums.net http://www.csharpforums.net/windows-forms/1145-fill-listview-arraylist-text-file.html

a bit confused if I should be using Arraylist or not.

I need some advice on my logical thinking here.

I have a dataGrid that reads from sql database , I want user to select a item that has a price and I just want to add that price to subtotal until the user decided to stop shopping.

here is my code example it works but only once , I need something that would loop until user doesn't select anything
        private void txtAddToInvoice_Click(object sender, EventArgs e)
        {
            // Select the value from table that user has selected product 
            DataGridViewRow dr = tbl_ProductDetailsDataGridView.SelectedRows[0];
            
            string UnitPriceS; // String value 
            int Quantity;
            decimal total;
            decimal subTotal;
            ArrayList SubtotalList = new ArrayList();
            
            btnGenerateInvoice.Enabled = true;

            // storing the value that user has click  to UnitPrices
            UnitPriceS = dr.Cells[3].Value.ToString();

           decimal UnitPrice = Math.Round(Convert.ToDecimal(UnitPriceS), 2);

           Quantity = Convert.ToInt32(txtQuantity.Text);

           total = UnitPrice * Quantity;

           SubtotalList.Add(total);

            // count array
           int count = SubtotalList.Count;           
            
                listBox1.Items.Add(count);

            MessageBox.Show(count.ToString());
        }


I hope the above makes sense

I need some serious advice any support would be appreciated
 
Last edited by a moderator:

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,273
Location
Sydney, Australia
Programming Experience
10+
Firstly, no, you should not be using an ArrayList. Secondly, you have a DataGridView, not a DataGrid. They are not the same thing, hence the different names. Finally, you say that you "just want to add that price to subtotal" but the code suggests that you want much more than that. Maybe you should start by providing a FULL and CLEAR description of what you want to happen and what the problem is. It's hard to write code when you don't know what it's actually supposed to do.
 

waheedrafiq

Member
Joined
Jan 4, 2015
Messages
11
Programming Experience
1-3
Firstly, no, you should not be using an ArrayList. Secondly, you have a DataGridView, not a DataGrid. They are not the same thing, hence the different names. Finally, you say that you "just want to add that price to subtotal" but the code suggests that you want much more than that. Maybe you should start by providing a FULL and CLEAR description of what you want to happen and what the problem is. It's hard to write code when you don't know what it's actually supposed to do.


Hi Jmcilhinney

Thanks for the reply back , I am Dyslexic and writing is something i need to get a grip on , Attach is a logic diagram that I just done , and a screen dump ,
which I hope will provide you with extra information. please ignore the above code. I just write this new test example and will be able to fix my issue from this example as its the same thing that I want. I have a Shopping Cart App it connects to the shop.mdf database and tbl_shopping_list when user clicks on the row ie select the product this gets to be display in listbox. by clicking on the add to list order button. this is where I am now stuck can't find my logics , when user clicks on Generate Invoice I need to some how copy / read the productPrice to a array, hence arraylist as it was obvious one in intellisense. But I understand why you state we shouldn't use this method. adding the subtotal and what product selected to a text file or pdf as a invoice.


I hope the above was enough to get a justice of what i am trying to achieved.
 

Attachments

  • shoppinglist.PNG
    shoppinglist.PNG
    30.1 KB · Views: 44
  • logic_shoppingcart.PNG
    logic_shoppingcart.PNG
    19.1 KB · Views: 41

waheedrafiq

Member
Joined
Jan 4, 2015
Messages
11
Programming Experience
1-3
Hi again I have created a button call test code to see if I can work out my problem here is that code and added a textbox call txtQuantity

I have it working about 85% just need to figure out few things

as user select a item , that item has a price and is now store in a variable called ArrayTotal[] the for next statement sort out each time user click yes button from the message box it increment the variable count. the problem is it keeps going into a loop and even the break command does't work any ideas would be much appericated

I have just spent 3 hours learning the basic of Array from this site Visual C# .NET - Set the size of an array at runtime
        private void btnTestCode_Click(object sender, EventArgs e)
        {
            // store values from DataGridViewRow to variables
            string UnitPriceS; // String value 
            int Quantity;
           
            decimal total;
            decimal[] arrayTotal;

            // Select the value from table that user has selected product 
            DataGridViewRow dr = tbl_ProductDetailsDataGridView.SelectedRows[0];

            // storing the value that user has click  to UnitPrices
            UnitPriceS = dr.Cells[3].Value.ToString();

            // Convert hte UnitPriceS string value to decimal variable UnitPrice
            decimal UnitPrice = Math.Round(Convert.ToDecimal(UnitPriceS), 2);

            // Convert text box Quantity to interger variable Quantity whole number.
            Quantity = Convert.ToInt32(txtQuantity.Text);

            // arrary Initialization Zero 
            arrayTotal = new decimal[12];

            for (int count = 0; count != (arrayTotal.Length); count++)
            {
                DialogResult m = MessageBox.Show("Add another Order?", "Add", MessageBoxButtons.YesNo);
                // Sum  UnitPrice Multiply by Quantity
                total = UnitPrice * Quantity;
                if (m == DialogResult.Yes)
                {
                    arrayTotal[count] = total;

                    MessageBox.Show("Yes button press array value  :  " + arrayTotal[count].ToString() + "your array count index Value is : " + count.ToString());
                }
                else if (m == DialogResult.No)
                {
                    arrayTotal[count] = total;
                    MessageBox.Show("total bill:" + total.ToString() + " ArraryCount Value:" + arrayTotal[count].ToString() + "Your Count index value:" + count.ToString());
                    break; // break out of loop 
                }
            } // end of for statement
        }
 
Last edited by a moderator:
Top Bottom