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
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;
}