1

Currently I am struggling to create a dataset in C# using Visual Studio, this will eventually be used to create a chart that show the retention time of users to groups (Like a clock in and out system)

Unfortunately whilst debugging I am brought to the attention of an error

Index was outside the bounds of the array

Below is my code creating the data set, I was wondering if anyone could spot any small errors with this code that I cannot.

Thank you.

  {
        string connectionString = null;

        SqlConnection connection ;
        SqlCommand command ;

        SqlDataAdapter adapter = new SqlDataAdapter();
        DataSet RetDs = new DataSet();

        int Counter = 0;

        string Chrt_NamesSql = null;
        string Chrt_RetenSql = null;

        connectionString = @"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename = G:\NEA\GardenRegister\GardenRegister\bin\Debug\GardenRegister.mdf; Integrated Security = True";

        Chrt_NamesSql = "SELECT Name FROM Member";
        Chrt_RetenSql = "SELECT RetentionTime FROM Attendance";

        connection = new SqlConnection(connectionString);

        try
        {
            connection.Open();

            command = new SqlCommand(Chrt_NamesSql, connection);
            adapter.SelectCommand = command;
            adapter.Fill(RetDs, "Member");

            adapter.SelectCommand.CommandText = Chrt_RetenSql;
            adapter.Fill(RetDs, "Attendance");

            adapter.Dispose();
            command.Dispose();
            connection.Close();

            // retrieve first table data 
            for (Counter = 0; Counter <= RetDs.Tables[0].Rows.Count - 1; Counter++)
            {
                MessageBox.Show(RetDs.Tables[0].Rows[Counter].ItemArray[0] + " -- " + RetDs.Tables[0].Rows[Counter].ItemArray[1]);
            }

            // retrieve second table data 
            for (Counter = 0; Counter <= RetDs.Tables[1].Rows.Count - 1; Counter++)
            {
                MessageBox.Show(RetDs.Tables[1].Rows[Counter].ItemArray[0] + " -- " + RetDs.Tables[1].Rows[Counter].ItemArray[1]);
            }
        }
1
  • 1
    When asking a question about an exception, you might want to highlight the line at which the exception is thrown. This makes it easier for others to diagnose the problem. Commented Feb 18, 2018 at 12:27

1 Answer 1

4

You only select one column from each table, then you try to access ItemArray[1]. You'll have to change your queries to include the other column you are trying to retrieve.

For example, if you wanted to include the "Id" column of the "Member" table, use:

SELECT Name, Id FROM Member

And the same for the "Attendance" table, of course.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, that quick fix solved it. I have used a message box to display what errors are shown and now all the data is sent through the try catch nicely with no errors, now I can progress to adding this to the chart. Thank you again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.