跆拳道? [英] wtf?

查看:86
本文介绍了跆拳道?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。任何人都可以告诉我为什么这一大块代码:


if(Convert.IsDBNull(oCommand.Parameters [" @ userid"]。Value。)

{

p_nErrorCode = 10;

p_sErrorMessage ="登录失败,找不到匹配的凭证" ;;

goto失败;

}

sReturn = Convert.ToString(oCommand.Parameters [" @ userid"]。Value);


生成以下运行时错误:


System.InvalidCastException:对象无法从DBNull转换为其他

类型


这似乎是不可能的。没有记录ErrorCode或ErrorMessage,

表示if条件为false,且值不是
DBNull。进一步的证据是,如果我注释掉最后一行

(sReturn = Convert ...)那么代码不会产生运行时异常。

进一步表明if条件是false,值不是
DBNull。那么最后一行怎么能产生DBNull演员

异常呢?或者它不可能不是DBNull然后突然DBNull。


显然我错过了什么。你们看到了吗?你需要看看

更多代码吗?


感谢您的帮助,


jason


(如果有人想知道,这都是失败了)


失败:

if(oCommand!= null )

oCommand.Dispose();

if(oConn!= null)

{

oConn.Close ();

oConn.Dispose();

}


(这就是函数的结尾。不再篡改

ErrorCode或ErrorMessage,或任何会产生DBNull

投射异常的东西)

解决方案

jason< ia **** @ yahoo.com>写道:

大家好。任何人都可以告诉我为什么这一大块代码:

if(Convert.IsDBNull(oCommand.Parameters [" @ userid"]。Value。)
{
p_nErrorCode = 10 ;
p_sErrorMessage ="登录失败,未找到匹配的凭据" ;;
goto失败;
}
sReturn = Convert.ToString(oCommand.Parameters [" @ userid"] .Value);

生成以下运行时错误:

System.InvalidCastException:无法将对象从DBNull强制转换为其他类型
这似乎不可能。没有记录ErrorCode或ErrorMessage,这表明if条件为false,且值不是DBNull。进一步的证据是,如果我注释掉最后一行
(sReturn = Convert ...)那么代码不会产生运行时异常。
进一步表明if条件为false,且值不是< DBNull。那么最后一行怎么能产生DBNull cast
异常呢?或者它不可能不是DBNull然后突然DBNull。

显然我错过了一些东西。你们看到了吗?你需要看更多的代码吗?




嗯,一个简短但完整的例子会有所帮助 - 请参阅
http://www.pobox.com/~skeet/csharp/complete.html 有关详细信息

我的意思是什么。


请注意,使用goto在C#中很少是一个好主意 - 同样

错误代码和手动处理。例外通常会处理前两个(至少在错误的情况下)和使用的情况。声明使

后者更容易正确编写。


-

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

如果回复小组,请不要给我发邮件


好吧,一个完整的工作示例会相当庞大,不得不

包含整个函数,它调用许多其他函数。我会

修改了这个例子以达到完整性,因为我已经在我的

许多其他帖子中完成了,除了这个问题是特定于此代码的

上下文。我正在十几个其他地方做同样的测试,并且

它运作得很好。实际上 - 当从经典ASP调用

时,这段代码工作得很好。从ASP.NET调用它似乎只是窒息,

同样令人费解。所以简而言之,修改它的完整性

可能会删除有关上下文的任何特性导致错误的b / b
。我的希望是有人会认出

代码中的错误,不变的形状,通常是可能的。


在该笔记上,另一个同义词形状代码可以采取仍然

产生错误:


if(!Convert.IsDBNull(oCommand.Parameters [" @ userid"]。 ))

sReturn = Convert.ToString(oCommand.Parameters [" @ userid"]。Value);


这会报告DBNull强制转换错误sReturn = ...行。怎么用

地球这可能让我感到困惑,只需看看这两行

吧!


关于你的其他注意事项:据我所知,.NET异常不能由ASP经典代码优雅地处理。如果可以的话,请让我知道

在哪里可以了解更多信息。否则,我们必须手动编码错误,以便

使用此类库的ASP经典应用程序可以正确处理

错误。我将看一下使用声明,看看我是否可以改善我的处理机制。


感谢您的评论,


jason


hello everyone. can anyone tell me why this chunk of code:

if (Convert.IsDBNull(oCommand.Parameters["@userid"].Value))
{
p_nErrorCode = 10;
p_sErrorMessage = "Login failed, no matching credentials found";
goto Failed;
}
sReturn = Convert.ToString(oCommand.Parameters["@userid"].Value);

generates the following runtime error:

System.InvalidCastException: Object cannot be cast from DBNull to other
types

this seems impossible. no ErrorCode or ErrorMessage is recorded, which
would suggest that the if condition is false, and the value is not
DBNull. further evidence being that if i comment out the last line
(sReturn = Convert...) then the code generates no runtime exception.
that further suggests the if condition is false, and the value is not
DBNull. so how on earth can the last line generate the DBNull cast
exception? either it can''t be NOT DBNull and then suddenly DBNull.

clearly i''m missing something. do you guys see it? do you need to see
more code?

thanks for any help,

jason

(in case anyone is wondering, this is all Failed does:)

Failed:
if (oCommand != null)
oCommand.Dispose();
if (oConn != null)
{
oConn.Close();
oConn.Dispose();
}

(and that''s the end of the function. no more tampering with the
ErrorCode or ErrorMessage, or anything that would generate a DBNull
casting exception)

解决方案

jason <ia****@yahoo.com> wrote:

hello everyone. can anyone tell me why this chunk of code:

if (Convert.IsDBNull(oCommand.Parameters["@userid"].Value))
{
p_nErrorCode = 10;
p_sErrorMessage = "Login failed, no matching credentials found";
goto Failed;
}
sReturn = Convert.ToString(oCommand.Parameters["@userid"].Value);

generates the following runtime error:

System.InvalidCastException: Object cannot be cast from DBNull to other
types

this seems impossible. no ErrorCode or ErrorMessage is recorded, which
would suggest that the if condition is false, and the value is not
DBNull. further evidence being that if i comment out the last line
(sReturn = Convert...) then the code generates no runtime exception.
that further suggests the if condition is false, and the value is not
DBNull. so how on earth can the last line generate the DBNull cast
exception? either it can''t be NOT DBNull and then suddenly DBNull.

clearly i''m missing something. do you guys see it? do you need to see
more code?



Well, a short but complete example would help - see
http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Note that using "goto" is very rarely a good idea in C# - likewise
error codes and manual disposal. Exceptions usually take care of the
first two (in error cases, at least) and the "using" statement makes
the latter easier to write correctly.

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


well, a complete working example would be rather huge, having to
include the whole function, which calls many other functions. i would
have modified the example to attain completeness, as i''ve done in my
many other postings, except that this problem is specific to this code
context. i''m doing the same kind of test in a dozen other places, and
it works just fine. in fact -- THIS code works just fine when called
from classic ASP. it seems to only choking when called from ASP.NET,
which is equally baffling. so in short, modifying it for completeness
would likely remove whatever peculiarity about the context is causing
the error. my hope is that someone would recognize the error in the
codes present, unaltered shape, as is quite often the possible.

on that note, another synonymous shape the code can take and still
produce the error:

if (!Convert.IsDBNull(oCommand.Parameters["@userid"].Value))
sReturn = Convert.ToString(oCommand.Parameters["@userid"].Value);

this reports the DBNull cast error on the sReturn = ... line. how on
earth this is possible baffles me, simply looking at these two lines
alone!

regarding your other notes: so far as i know, .NET exceptions can''t be
gracefully handled by ASP classic code. if they can, do let me know
where to learn more. otherwise, we have to manually code errors so that
ASP classic applications which consume this class library can handle
errors correctly. i''ll take a look at the using statement to see if i
grock how that might improve my disposal mechanisms.

thanks for the comment,

jason


这篇关于跆拳道?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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