WebService“并非所有代码路径都返回值”。错误

编程入门 行业动态 更新时间:2024-10-26 14:34:44
本文介绍了WebService“并非所有代码路径都返回值”。错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

你好。 我正在建立一个网络服务,我收到了这个错误。 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(); } 我不知道如何解决它... 提前感谢 ***已发送通过开发人员指南 www.developersdex *** 不要只是参加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 www.developersdex *** 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 *** 不要只是参加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 www.developersdex *** Don''t just participate in USENET...get rewarded for it!

It编译O. K 但是当我运行它的时候是,我收到这个错误... System.NullReferenceException:对象引用未设置为实例 对象。 我无法弄清楚它触发此错误的位置 谢谢 ***通过Developersdex发送 www.developersdex *** 不要只是参加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 www.developersdex *** 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> www.pobox/~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> www.pobox/~skeet If replying to the group, please do not mail me too

更多推荐

WebService“并非所有代码路径都返回值”。错误

本文发布于:2023-11-10 01:17:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1573962.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路径   返回值   错误   代码   WebService

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!