-2

I get a exception "index was out of range. Must be no-negative and less than the size of the collection. parameter name : index

    private void button3_Click(object sender, EventArgs e)
    {
        try
        {
            if (txtSubtotal.Text == "")
            {
                MessageBox.Show("The Sub Total cannot be Empty", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtSubtotal.Focus();
                return;
            }
            if (txtamountdue.Text == "")
            {
                MessageBox.Show("The Amountdue cannot be Empty", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtamountdue.Focus();
                return;
            }
            if (txtcashgiven.Text == "")
            {
                MessageBox.Show("Please enter the cash given", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtcashgiven.Focus();
                return;
            }
            if (txtchange.Text == "")
            {
                MessageBox.Show("The Cahange cannot be Empty", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtchange.Focus();
                return;
            }



            con = new SqlConnection(cs.DBConn);
            con.Open();


            string cb = "insert Into invoiceTB(InvoiceNo,Date,BillTo,CustomerID,SalesBy,Warranty,ShipTo,SubTotal,AmountDue,CashGiven,Change) VALUES (@d1,@d2,@d3,@d4,@d5,@d6,@d7,@d8,@d9,@d10,@d11)";
            cmd = new SqlCommand(cb);
            cmd.Connection = con;

            cmd.Parameters.AddWithValue("@d1",txtInvoice.Text);
            cmd.Parameters.AddWithValue("@d2",dateTimePicker1.Text);
            cmd.Parameters.AddWithValue("@d3",txtBillTo.Text);
            cmd.Parameters.AddWithValue("@d4", cmbcustomerID.Text);
            cmd.Parameters.AddWithValue("@d5",txtSalesBy.Text);
            cmd.Parameters.AddWithValue("@d6",txtwarranty.Text);
            cmd.Parameters.AddWithValue("@d7",txtshipto.Text);
            cmd.Parameters.AddWithValue("@d8",txtSubtotal.Text);
            cmd.Parameters.AddWithValue("@d9",txtamountdue.Text);
            cmd.Parameters.AddWithValue("@d10",txtcashgiven.Text);
            cmd.Parameters.AddWithValue("@d11",txtchange.Text);

            cmd.ExecuteReader();
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
            con.Close();


            for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
            {
                con = new SqlConnection(cs.DBConn);

                string cd = "insert Into invoiceTB(ItemCode,Description,Quantity,PriceEach,Amount) VALUES (@d1,@d2,@d3,@d4,@d5)";
                cmd = new SqlCommand(cd);
                cmd.Connection = con;
                cmd.Parameters.AddWithValue("d1", dataGridView1.Rows[i].Cells[1].Value);
                cmd.Parameters.AddWithValue("d2", dataGridView1.Rows[i].Cells[2].Value);
                cmd.Parameters.AddWithValue("d3", dataGridView1.Rows[i].Cells[4].Value);
                cmd.Parameters.AddWithValue("d4", dataGridView1.Rows[i].Cells[3].Value);
                cmd.Parameters.AddWithValue("d5", dataGridView1.Rows[i].Cells[5].Value);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }

            btnSave.Enabled = false;
            btnprint.Enabled = true;
            MessageBox.Show("Successfully Placed", "Order", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • 1
    First of all, next time please be sensitive about formatting. Second, _doesn't work_ is not a good way to ask a question. What do you mean by? You get an exception or error message? – Soner Gönül Oct 24 '14 at 08:20
  • sorry im new here i get a exception "index was out of range. Must be no-negative and less than the size of the collection. parameter name : index – Jerome Jacobs Francis Oct 24 '14 at 08:25

1 Answers1

1

It is not clear what you asking but I see a few things;

  • You need to use ExecuteNonQuery instead of ExecuteReader in your first insert statement. That's just execute your code. ExecuteReader method returns data. Since you just inserting data, pointless to use it.

  • You need to use @ in your second insert command parameter names in your AddWithValue method.

  • Use using statement to dispose your database connections and objects.

  • DATE can be a reserved keyword future releases of SQL Server. You might need to avoid to use it.

  • As a best practice, don't use AddWithValue method. It might generates unexpected and surprising results. Use .Add() method instead. Read Can we stop using AddWithValue() already?

For your error message, read What is an "index out of range" exception, and how do I fix it?

Community
  • 1
  • 1
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • i tried everything u said but i get the same exception can u show me a example code that used to save data-grid values to database with the textbox data similar to my code – Jerome Jacobs Francis Oct 24 '14 at 08:43
  • @JeromeJacobsFrancis Did you debug your code line by line? It is clearly try to access a value that doesn't inside in your collections boundaries. Please read this question and answer carefully. http://stackoverflow.com/questions/24812679/what-is-an-index-out-of-range-exception-and-how-do-i-fix-it – Soner Gönül Oct 24 '14 at 10:07