Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8342125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:40:49+00:00 2026-06-09T05:40:49+00:00

Hi guys this codes gives me Array was outside the bounds of the array

  • 0

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)
            {

            }
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T05:40:50+00:00Added an answer on June 9, 2026 at 5:40 am

    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++)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

max($caption); gives me this: Array $caption is defined here: $i = 0; while ($i
Guys i need some help here , im getting this error here at the
Hey Guys, here is my code for this, the only help i get from
Can you guys please check this code, it is MVC3 Razor <p><strong><u>Skills:</u></strong> @foreach (var
Hey guys this is my html code: <div class=nakupy> <li class=icn_kategorie><a href=#>Nákupy</a> <div class=sub_menu>
Hi guys I was looking at this code but I'm struggling to understand what
my app crashes with this code.. it doesnt even start up.. any ideas guys
Hi guys i catch exception: java.lang.IndexOutOfBoundsException: Index: 1, Size: 0 Code look like this:
Hey guys this should be simple, I'm just not seeing it, I would like
Hey guys this is a bit of a homework puzzle I'm working on and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.