为什么“指定演员表无效”错误? [英] Why "Specified cast is not valid" error?

查看:80
本文介绍了为什么“指定演员表无效”错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我正在用C#编写一个webform应用程序(带有VS.NET 2003 Pro的ASP.NET 1.1 SP1
WinXP SP2上的
)使用IIS 5.1)。


我创建了一个单独的数据。类来容纳所有MySQL连接和sql

命令方法。这正是Microsoft数据访问应用程序

块程序集所做的,但我编写了自己的简单自定义类。


我有一个名为ExecuteAggregate的方法;它接受一个sql字符串,如

" SELECT COUNT(*)FROM users",像ExecuteScalar()那样执行它,

返回一个对象类型的值。下面的代码如下:


***代码块***

公共静态对象ExecuteAggregate(string SQL)

{


MySqlCommand myCmd = new MySqlCommand(SQL,myConn);


object retval = myCmd.ExecuteScalar();


返回retval;


}


***代码块***

我在page_load事件中使用上面的方法,如下所示。该项目

构建时没有错误或警告,但页面在执行时出错。


***代码块***


private void Page_Load(object sender,System.EventArgs e)

{


string userCount;


Data.Connect();


//>>>>>"指定演员表无效。下一行发生异常!


userCount =(string)Data.ExecuteAggregate(" SELECT COUNT(*)FROM users");


lblBalance.Text = userCount;


Data.Disconnect();


}


***代码块***


我不知道为什么在执行时出错,而不是在

建筑期间出错。 Convert.ToInt32()解决了这个问题,但我想知道为什么C#

演员不会。


感谢您提前的帮助!

解决方案

尝试

convert.toString(Data.ExecuteAggregate(" SELECT COUNT(*)FROM users"));


" Jack MacRank"写道:

你好,

我正在用C#编写一个webform应用程序(带有VS.NET 2003 Pro的ASP.NET 1.1 SP1)
在使用IIS 5.1的WinXP SP2上。

我创建了一个单独的数据。类来容纳所有MySQL连接和sql
命令方法。这正是Microsoft数据访问应用程序块组件所做的,但我编写了自己的简单自定义类。

我有一个名为ExecuteAggregate的方法。它接受像
SELECT COUNT(*)FROM users这样的sql字符串,像ExecuteScalar()那样执行它,并且
返回一个对象类型的值。以下是代码:

***代码块***
公共静态对象ExecuteAggregate(字符串SQL)


> MySqlCommand myCmd = new MySqlCommand(SQL,myConn);

对象retval = myCmd.ExecuteScalar();

返回retval;

}

***代码块***

我在page_load事件中使用上面的方法,如下所示。该项目构建时没有错误或警告,但页面在执行时出错。

***代码块***

private void Page_Load(对象发送者) ,System.EventArgs e)
{string userCount;

Data.Connect();

//>> >>>"指定的广告投放无效。下一行发生异常!

userCount =(string)Data.ExecuteAggregate(" SELECT COUNT(*)FROM users");

lblBalance.Text = userCount;

Data.Disconnect();

}

***代码块***

我不知道为什么这在执行时出错,而不是在
构建期间出错。 Convert.ToInt32()解决了这个问题,但我想知道为什么C#
演员不会。

提前感谢您的帮助!


您好,


您是否尝试过:


userCount = Data.ExecuteAggregate(" SELECT COUNT(*)FROM users")。ToString();


而不是使用强制转换?

如果仍然出错,请将返回值赋值给一个对象实例并在下一行放置

a断点,看看你得到的回应。


干杯,


-

Ignacio Machin,
ignacio.machin AT dot.state.fl.us

佛罗里达州交通局


" Jack MacRank" < JA ** @ macrank.com>在消息中写道

新闻:aP ***************** @ fe1.columbus.rr.com ...

你好,

我正在用C#编写一个webform应用程序(使用IIS 5.1在WinXP SP2上使用VS.NET 2003
Pro进行编写)。
Application Block程序集所做的,但我编写了我自己的简单自定义类。

我有一个名为ExecuteAggregate的方法。它接受像
SELECT COUNT(*)FROM users这样的sql字符串,像ExecuteScalar()那样执行它,并且
返回一个对象类型的值。以下是代码:

***代码块***
公共静态对象ExecuteAggregate(字符串SQL)


> MySqlCommand myCmd = new MySqlCommand(SQL,myConn);

对象retval = myCmd.ExecuteScalar();

返回retval;

}

***代码块***

我在page_load事件中使用上面的方法,如下所示。该项目构建时没有错误或警告,但页面在执行时出错。

***代码块***

private void Page_Load(对象发送者) ,System.EventArgs e)
{string userCount;

Data.Connect();

//>> >>>"指定的广告投放无效。下一行发生异常!

userCount =(string)Data.ExecuteAggregate(" SELECT COUNT(*)FROM users");

lblBalance.Text = userCount;

Data.Disconnect();

}

***代码块***

我不知道为什么这在执行时出错,而不是在
构建期间出错。 Convert.ToInt32()修复此问题,但我想知道为什么C#
演员不会。

感谢你的帮助!



Jack MacRank< ja ** @ macrank.com>写道:


< snip>

***代码块***

private void Page_Load(对象发送者,System.EventArgs e)
{string / userCount;

Data.Connect();

//> >>>>"指定的广告投放无效。下一行发生异常!

userCount =(string)Data.ExecuteAggregate(" SELECT COUNT(*)FROM users");

lblBalance.Text = userCount;

Data.Disconnect();

}

***代码块***

我不知道为什么这在执行时出错,而不是在
构建期间出错。 Convert.ToInt32()解决了这个问题,但我想知道为什么C#
强制转换不会。




你的C#期待Data返回的引用。 ExecuteAggregate to

* be * a string。不是。尝试转换为int - 或者打印出来

Data.ExecuteAggregate(...)。GetType()找出实际上是什么类型

返回。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复该群组,请不要给我发邮件太


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!

解决方案

Try
convert.toString(Data.ExecuteAggregate("SELECT COUNT(*) FROM users"));

"Jack MacRank" wrote:

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!



Hi,

Have you tried:

userCount = Data.ExecuteAggregate("SELECT COUNT(*) FROM users").ToString();

instead of using a casting ?
if still getting error, assig the return value to an object instance and put
a break point in the next line to see what you are getting as response.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Jack MacRank" <ja**@macrank.com> wrote in message
news:aP*****************@fe1.columbus.rr.com...

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!



Jack MacRank <ja**@macrank.com> wrote:

<snip>

***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.



Your C# is expecting the reference returned by Data.ExecuteAggregate to
*be* a string. It''s not. Try casting to int instead - or print out
Data.ExecuteAggregate(...).GetType() to find out what type is actually
returned.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


这篇关于为什么“指定演员表无效”错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆