Dimitri
Dimitri

Reputation: 1966

System index out range exception C#

I have the following code:

using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TimeClock
{
    class Company
    {
        DataTable rows = new DataTable();

        public Company()
        {
            MySqlConnection connection = null;
            try
            {

                string connectionString = TimeClock.Properties.Settings.Default.timeclockConnectionString;
                connection = new MySqlConnection(connectionString);
                connection.Open();
                MySqlCommand command = new MySqlCommand("SELECT * FROM companies WHERE ID = @ID  LIMIT 1", connection);
                command.Parameters.AddWithValue("@ID", TimeClock.Properties.Settings.Default.CompanyID);
                MySqlDataAdapter da = new MySqlDataAdapter(command);
                da.Fill(rows);

            }
            catch (MySql.Data.MySqlClient.MySqlException ex)
            {
                Console.WriteLine(ex);
            }
            finally
            {
                if (connection != null)
                {
                    connection.Close();
                }
            }
        }

        public String getName()
        {
            DataRow row = rows.Rows[0];

            return row["company_name"].ToString();
        }
    }
}

I know why I'm getting this error: say that no records were found in the database, the of course row[0] will no exist, hence the exception. But how do I deal with when there are no records to store in the Datatable? By the way, I'm quite new to C# and any input would be great; feel free to criticize.

Upvotes: 0

Views: 241

Answers (3)

You must change getName function.

 public String getName()
 {
     if (rows.Rows != null && rows.Rows.Count > 0)
        {
            DataRow row = rows.Rows[0];
            return row["company_name"].ToString();
        }
        return string.Empty;
 }

Upvotes: 1

DrKoch
DrKoch

Reputation: 9782

Before you access a collection

DataRow row = rows.Rows[0];

you'll have to make sure that the item exists:

if(rows.Count > 0) 
   DataRow row = rows.Rows[0];

always

Upvotes: 1

Xanatos
Xanatos

Reputation: 445

To read data from SQL I'm doing it like so:

        using (SqlCommand SelectCommand = new SqlCommand(strbSelect.ToString()))
        {
                SelectCommand.Parameters.AddWithValue("Asset", AssetNumber);
                SelectCommand.Parameters.AddWithValue("Subnumber", Subnumber);

                SelectCommand.Connection = new SqlConnection(GetConnectionString());
                SelectCommand.Connection.Open();
                using (SqlDataReader Reader = SelectCommand.ExecuteReader())
                {
                    if (Reader.HasRows)
                    {
                        while (Reader.Read())
                        {
                            if (Reader[0] != DBNull.Value)
                            {
                                ReturnValue = Reader.GetBoolean(0);
                            }
                        }
                    }
                    else
                        return false;
                }
                SelectCommand.Connection.Close();
        }

StrbSelect is a StringBuilder.

Upvotes: 0

Related Questions