---
title: "System.IndexOutOfRangeException"
type: "forum-thread"
url: "https://www.webforum.nu/amne/dotnet/162637-system-indexoutofrangeexception"
topic: ".NET"
topic_url: "https://www.webforum.nu/amne/dotnet"
author: "flaxx"
published: "2007-07-17T12:51:48.000Z"
updated: "2007-07-19T12:33:37.000Z"
replies: 2
views: 550
page: 1
pages: 1
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/dotnet/162637-system-indexoutofrangeexception"
---

# System.IndexOutOfRangeException

## #1 — flaxx, 2007-07-17T12:51Z

Hi,

I have the following code: 

 
public static DepartmentDetails GetDetails(string Culture, string DepartmentID)
{
        // create the connection
        MySqlConnection myConnection = new MySqlConnection(ConnectionString);

        string CommandText = "SELECT Name FROM department INNER JOIN cultures ON department.CultureID = cultures.CultureID WHERE UICulture = ?Culture AND DepartmentID = ?DepartmentID";

        // create the command
        MySqlCommand command = new MySqlCommand(CommandText, myConnection);

        // add the parameters
        MySqlParameter Culture = new MySqlParameter("?Culture", MySqlDbType.String);
        Culture.Value = Culture;
        command.Parameters.Add(Culture );
        MySqlParameter ID = new MySqlParameter("?DepartmentID", MySqlDbType.Int32);
        ID.Value = DepartmentID;
        command.Parameters.Add(ID );

        // The DataTable to be returned
        DataTable table;

        // Execute the command making sure the connection gets closed in the end
        try
        {
            // Open the data connection
            command.Connection.Open();
            // Execute the command and save the results in the DataTable
            MySqlDataReader reader = command.ExecuteReader();
            table = new DataTable();
            table.Load(reader);
            // Close the reader
            reader.Close();
        }
        finally
        {
            // Close the connection
            command.Connection.Close();
        }
        // wrap retrieved data into a DepartmentDetails object
        DepartmentDetails details = new DepartmentDetails();
        if (table.Rows.Count \> 0)
        {
            details.Name= table.Rows\[0\]\["Name"\].ToString();
        }
        return details;
    }

 Everything works in IE but when I test it in Firefox, then I get the following code and the query string seems not returning any value:

table.Rows\[0\]    'table.Rows\[0\]' threw an exception of type 'System.IndexOutOfRangeException'    System.Data.DataRow {System.IndexOutOfRangeException}

table.Rows\[0\]\["Name"\]    'table.Rows\[0\]' threw an exception of type 'System.IndexOutOfRangeException'    object {System.IndexOutOfRangeException}

base    {"There is no row at position 0."}    System.SystemException {System.IndexOutOfRangeException}

 

Any help on why I get this problem with firefox? Thanks

Permalänk: https://www.webforum.nu/p/162637

## #2 — emission, 2007-07-17T20:34Z

There's really no reason why the browser would have any influence over this, and you do seem to have a proper dimension check. Nonetheless, I rewrote the method to use a "using"-clause (automatically closes and disposes the connection) and to retrieve the data directly from the datareader, without an intermediate datatable. The DepartmentID argument is type cast to an int, since it seems that is what the query expects.

```
public static DepartmentDetails GetDetails(string Culture, string DepartmentID)
{
	string CommandText = "SELECT Name FROM department INNER JOIN cultures ON department.CultureID = cultures.CultureID WHERE UICulture = ?Culture AND DepartmentID = ?DepartmentID";

	// create the command
	using (MySqlConnection myConnection = new MySqlConnection(ConnectionString))
	{
		MySqlCommand command = new MySqlCommand(CommandText, myConnection);

		// add the parameters
		MySqlParameter Culture = new MySqlParameter("?Culture", MySqlDbType.String);
		Culture.Value = Culture;
		command.Parameters.Add(Culture );
		MySqlParameter ID = new MySqlParameter("?DepartmentID", MySqlDbType.Int32);
		ID.Value = int.Parse(DepartmentID);
		command.Parameters.Add(ID );

		// Execute the command making sure the connection gets closed in the end
		myConnection.Open();
		// Execute the command and save the results in the DataTable
		MySqlDataReader reader = command.ExecuteReader();
		if (reader.Read())
		{
			DepartmentDetails details = new DepartmentDetails();
			details.Name=reader["Name"].ToString();
			return details;
		}
                reader.Close();
	}
	return null;
}
```

Permalänk: https://www.webforum.nu/p/2004422

## #3 — flaxx, 2007-07-19T12:33Z

The code seems to work now, maybe a problem with the database connection?? Anyway thanks for the new code you provide.

Permalänk: https://www.webforum.nu/p/2004815

---

Tråden på webben: https://www.webforum.nu/amne/dotnet/162637-system-indexoutofrangeexception
