• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • Last Modified:

Problem inserting Data to Database in ASP.Net c#

Hi,

i try to insert every item from a class into a database.
When i run the program, the Data shows up in SQL, but the application throws the following exception:

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

What can i do to fix it?
protected void btnCheckout_Click(object sender, EventArgs e)
        {
            ADDTest.Datasets.dsCribstoreTableAdapters.CheckoutTableAdapter dsCheckout = new ADDTest.Datasets.dsCribstoreTableAdapters.CheckoutTableAdapter();
            int intItems = ShoppingCart.Instance.Items.Count;
            for (int i = 0; i <= intItems; i++)
            {
                dsCheckout.Insert((string)ShoppingCart.Instance.Items[i].USer,(string)ShoppingCart.Instance.Items[i].ProductId,(int)ShoppingCart.Instance.Items[i].Quantity,(string)ShoppingCart.Instance.Items[i].ChrgDepartment,null,DateTime.Now);
            }

        }

Open in new window

0
Sebastian_OH
Asked:
Sebastian_OH
2 Solutions
 
ericathomeCommented:
Try the following:

protected void btnCheckout_Click(object sender, EventArgs e)
        {
            ADDTest.Datasets.dsCribstoreTableAdapters.CheckoutTableAdapter dsCheckout = new ADDTest.Datasets.dsCribstoreTableAdapters.CheckoutTableAdapter();
            int intItems = ShoppingCart.Instance.Items.Count;
            for (int i = 0; i < intItems; i++)
            {
                dsCheckout.Insert((string)ShoppingCart.Instance.Items[i].USer,(string)ShoppingCart.Instance.Items[i].ProductId,(int)ShoppingCart.Instance.Items[i].Quantity,(string)ShoppingCart.Instance.Items[i].ChrgDepartment,null,DateTime.Now);
            }

        }

Let me know if this resolves the problem or if the error message changes. Thanks, Eric.
0
 
AshokCommented:
protected void btnCheckout_Click(object sender, EventArgs e)
        {
            ADDTest.Datasets.dsCribstoreTableAdapters.CheckoutTableAdapter dsCheckout = new ADDTest.Datasets.dsCribstoreTableAdapters.CheckoutTableAdapter();
            int intItems = ShoppingCart.Instance.Items.Count;
            for (int i = 0; i <= intItems-1; i++)
            {
                dsCheckout.Insert((string)ShoppingCart.Instance.Items[i].USer,(string)ShoppingCart.Instance.Items[i].ProductId,(int)ShoppingCart.Instance.Items[i].Quantity,(string)ShoppingCart.Instance.Items[i].ChrgDepartment,null,DateTime.Now);
            }

        }
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now