Newbie
Newbie

Reputation: 231

Array was outside the bounds of the array error

Hi guys this codes gives me Array was outside the bounds of the array error and points to here " if (!objReader.IsDBNull(i))". This script is meant to print out in a txt format. any help will be appreciated :)

namespace test
    {
        public partial class frmSales : Form
        {
            public frmSales()
            {
                InitializeComponent();
            }


            private void dtpFrom_ValueChanged(object sender, EventArgs e)
            {

            }

            private void btnExtract_Click(object sender, EventArgs e)
            {

                SqlConnection objConn = new SqlConnection("Data Source=asdasd;Initial Catalog=Medprac;Persist Security Info=True;User ID=sa;Password=");

                    objConn.Open();

                    SqlCommand objCmd = new SqlCommand("SELECT CONVERT(char(80), inv.[InvDate],3) AS InvDate,inv.[InvoiceNo],inv.[TaxAmount] + inv.[SubTotal] AS Amount, '' AS Payment FROM [Invoice] inv LEFT JOIN [PatientDetails] tab ON inv.[MedicalRecordID] = tab.[MedicalRecordID] WHERE (inv.[InvDate] >= CONVERT(datetime, '" + dtpFrom.Text + "', 105 )) AND (inv.[InvDate] <= CONVERT(datetime, '" + dtpTo.Text + "', 105))", objConn);

                SqlDataReader objReader;
                objReader = objCmd.ExecuteReader();

                System.IO.FileStream fs = new System.IO.FileStream("C:\\CMSExportedData\\Sales-" + DateTime.Now.ToString("dd-MM-yyyy") + ".txt", System.IO.FileMode.Create);
                System.IO.StreamWriter sw = new System.IO.StreamWriter(fs, System.Text.Encoding.Default);

                int count = 0;
                while (objReader.Read())
                {

                    for (int i = 0; i < 5; i++)
                    {
                        if (!objReader.IsDBNull(i))
                        {
                            string s;
                            s = objReader.GetDataTypeName(i);
                            //MessageBox.Show(s);
                            if (objReader.GetDataTypeName(i) == "char")
                            {
                                sw.Write(objReader.GetString(i));
                            }
                            else if (objReader.GetDataTypeName(i) == "money")

                            {
                                sw.Write(objReader.GetSqlMoney(i).ToString());
                            }
                            else if (objReader.GetDataTypeName(i) == "nvarchar")
                            {
                                sw.Write(objReader.GetString(i));
                            }
                        }
                        if (i < 4)
                        {
                            sw.Write("\t");
                        }

                    }
                    count = count + 1;
                    sw.WriteLine();

                }
                sw.Flush();
                fs.Close();
                objReader.Close();
                objConn.Close();
                MessageBox.Show(count + " records exported successfully.");
                this.Close();
            }

            private void groupBox1_Enter(object sender, EventArgs e)
            {

            }

            private void dtpTo_ValueChanged(object sender, EventArgs e)
            {

            }

            private void frmSales_Load(object sender, EventArgs e)
            {

            }
        }
    }

Upvotes: 1

Views: 275

Answers (2)

Stephan Bauer
Stephan Bauer

Reputation: 9249

Your SqlCommand has the following SQL statement

SELECT 
    CONVERT(char(80), inv.[InvDate],3) AS InvDate,
    inv.[InvoiceNo],
    inv.[TaxAmount] + inv.[SubTotal] AS Amount, 
    '' AS Payment
FROM 
    ...

which returns 4 values (index 0 to 3) but your for-loop goes from index 0 to 4

Upvotes: 1

Hinek
Hinek

Reputation: 9729

Your select statement queries 4 columns, but your for loop covers the columns 0,1,2,3,4: that's five columns, so i = 4 is out of bounds.

Try for (int i = 0; i < 4; i++)

Upvotes: 1

Related Questions