J
Jack MacRank
Hello,
I'm coding a webform application in C# (ASP.NET 1.1 SP1 with VS.NET 2003 Pro
on WinXP SP2 using IIS 5.1).
I created a seperate "data" class to house all the MySQL connection and sql
command methods. This is exactly what the Microsoft Data Access Application
Block assembly does but I coded my own simple, custom class.
I have a method named "ExecuteAggregate" that takes in a sql string like
"SELECT COUNT(*) FROM users", executes it like ExecuteScalar() does and
returns an object typed value. Here is the code below:
***CODE BLOCK***
public static object ExecuteAggregate(string SQL)
{
MySqlCommand myCmd = new MySqlCommand(SQL, myConn);
object retval = myCmd.ExecuteScalar();
return retval;
}
***CODE BLOCK***
I use the above method in the page_load event as shown below. The project
builds without error or warning but the page errors out when it executes.
***CODE BLOCK***
private void Page_Load(object sender, System.EventArgs e)
{
string userCount;
Data.Connect();
//>>>>>"Specified cast is not valid." exception occurs on next line!
userCount = (string)Data.ExecuteAggregate("SELECT COUNT(*) FROM users");
lblBalance.Text = userCount;
Data.Disconnect();
}
***CODE BLOCK***
I have no idea why this is erroring out when executing and not during
building. Convert.ToInt32() fixes this but I would like to know why a C#
cast will not.
Thanks for your help in advance!
I'm coding a webform application in C# (ASP.NET 1.1 SP1 with VS.NET 2003 Pro
on WinXP SP2 using IIS 5.1).
I created a seperate "data" class to house all the MySQL connection and sql
command methods. This is exactly what the Microsoft Data Access Application
Block assembly does but I coded my own simple, custom class.
I have a method named "ExecuteAggregate" that takes in a sql string like
"SELECT COUNT(*) FROM users", executes it like ExecuteScalar() does and
returns an object typed value. Here is the code below:
***CODE BLOCK***
public static object ExecuteAggregate(string SQL)
{
MySqlCommand myCmd = new MySqlCommand(SQL, myConn);
object retval = myCmd.ExecuteScalar();
return retval;
}
***CODE BLOCK***
I use the above method in the page_load event as shown below. The project
builds without error or warning but the page errors out when it executes.
***CODE BLOCK***
private void Page_Load(object sender, System.EventArgs e)
{
string userCount;
Data.Connect();
//>>>>>"Specified cast is not valid." exception occurs on next line!
userCount = (string)Data.ExecuteAggregate("SELECT COUNT(*) FROM users");
lblBalance.Text = userCount;
Data.Disconnect();
}
***CODE BLOCK***
I have no idea why this is erroring out when executing and not during
building. Convert.ToInt32() fixes this but I would like to know why a C#
cast will not.
Thanks for your help in advance!