WebService“并非所有代码路径都返回值”。错误 [英] WebService "not all code paths return a value" error
问题描述
你好。
我正在建立一个网络服务,我收到了这个错误。
NEWS.News.CoverNews(string)'':并非所有代码路径都返回值
这是WebMethod
[WebMethod]
public SqlDataReader CoverNews(字符串体育)
{
SqlCommand myCommand = new SqlCommand(" SELECT TOP 1 News.idNews FROM
News",myConnection);
myConnection.Open();
SqlDataReader dr = myCommand.ExecuteReader();
if(dr.Read())
{
返回博士;
}
myConnection.Close();
}
我不知道如何解决它...
提前感谢
***已发送通过开发人员指南 http://www.developersdex.com ***
不要只是参加USENET ......获得奖励!
Hello.
I''m building a web service and I get this error.
NEWS.News.CoverNews(string)'': not all code paths return a value
This is the WebMethod
[WebMethod]
public SqlDataReader CoverNews(string Sport)
{
SqlCommand myCommand = new SqlCommand ("SELECT TOP 1 News.idNews FROM
News", myConnection);
myConnection.Open();
SqlDataReader dr = myCommand.ExecuteReader();
if(dr.Read())
{
return dr;
}
myConnection.Close();
}
I don''t know how to solve it...
thanks in advance
*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!
推荐答案
我们ll,这是真的!!并非所有代码路径都返回值!!
[WebMethod]
public SqlDataReader CoverNews(string Sport)
{
SqlCommand myCommand = new SqlCommand(" SELECT TOP 1 News.idNews FROM
News",myConnection);
myConnection.Open();
SqlDataReader dr = myCommand.ExecuteReader();
if(dr.Read())
{
/ /如果有一行,这将返回博士
返回博士;
}
myConnection.Close();
//新:如果没有行,这将返回
返回null;
}
Jose Fernandez < pp@inder.co.cu>在消息中写道
news:uM ************** @ tk2msftngp13.phx.gbl ...
Well, it''s true!! Not all code paths return a value!!
[WebMethod]
public SqlDataReader CoverNews(string Sport)
{
SqlCommand myCommand = new SqlCommand ("SELECT TOP 1 News.idNews FROM
News", myConnection);
myConnection.Open();
SqlDataReader dr = myCommand.ExecuteReader();
if(dr.Read())
{
// This will return dr if there IS a row
return dr;
}
myConnection.Close();
// NEW: This will return if there isn''t a row
return null;
}
"Jose Fernandez" <pp@inder.co.cu> wrote in message
news:uM**************@tk2msftngp13.phx.gbl...
你好。
我正在构建一个Web服务,我收到了这个错误。
NEWS.News.CoverNews(string)'':并非所有代码路径都返回一个值
这个是WebMethod
[WebMethod]
公共SqlDataReader CoverNews(字符串运动)
{sqlCommand myCommand = new SqlCommand(" SELECT TOP 1 News.idNews FROM
News",myConnection);
myConnection.Open();
SqlDataReader dr = myCommand.ExecuteReader();
if(dr.Read())
{
return dr;
}
myConnection.Close();
}
我不知道如何解决它...
提前致谢
***通过开发人员指南 http:// www。 developersdex.com ***
不要只是参加USENET ......获得奖励!
Hello.
I''m building a web service and I get this error.
NEWS.News.CoverNews(string)'': not all code paths return a value
This is the WebMethod
[WebMethod]
public SqlDataReader CoverNews(string Sport)
{
SqlCommand myCommand = new SqlCommand ("SELECT TOP 1 News.idNews FROM
News", myConnection);
myConnection.Open();
SqlDataReader dr = myCommand.ExecuteReader();
if(dr.Read())
{
return dr;
}
myConnection.Close();
}
I don''t know how to solve it...
thanks in advance
*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!
It编译O. K
但是当我运行它的时候是
,我收到这个错误...
System.NullReferenceException:对象引用未设置为实例
对象。
我无法弄清楚它触发此错误的位置
谢谢
***通过Developersdex发送 http://www.developersdex.com ***
不要只是参加USENET ......获得奖励!
It compiled OK
BUT
when I run it, I get this error...
System.NullReferenceException: Object reference not set to an instance
of an object.
I can''t figure out where it trigger this error
Thanks
*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!
Jose Fernandez< pp@inder.co.cu>写道:
Jose Fernandez <pp@inder.co.cu> wrote:
它编译好了
但是当我运行它时,我收到了这个错误...
System.NullReferenceException:对象引用未设置为对象的实例。
我无法弄清楚它触发此错误的位置
It compiled OK
BUT
when I run it, I get this error...
System.NullReferenceException: Object reference not set to an instance
of an object.
I can''t figure out where it trigger this error
>
打印出堆栈跟踪,你应该找出导致它的原因。
考虑表中没有任何行的可能性 - 所以
修改后的代码返回null。
请注意,您应该使用try / finally语句关闭命令和阅读器
或者(最好是IMO)使用
语句。还要考虑如果两个请求同时进入
会发生什么 - 因为myConnection和变量是一个实例
变量,两个请求都可以尝试使用相同的连接。你需要创建,打开和关闭连接,所有这些都在同一个
方法中。
-
Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet
如果回复小组,请不要给我发邮件
Print out the stack trace, and you should find out what''s causing it.
Consider the possibility that there aren''t any rows in the table - so
the modified code is returning null.
Note that you should be closing the command and the reader, either
using try/finally statements or (preferrably IMO) using a using
statement. Also consider what would happen if two requests came in
simultaneously - because the "myConnection" variable is an instance
variable, both requests could try to use the same connection. You
should create, open and close the connection, all within the same
method.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
这篇关于WebService“并非所有代码路径都返回值”。错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!