473,387 Members | 3,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

not all code paths return a value

I have a Winform news kiosk app which connects to a SQL database. When the
main form is running, it reaches a threshold time or number of sequential
operations and then loads another instance with a different dataset. The
datasets are based on local settings which I'm determining from IP space (all
machines running in that space will show the same news). The "special" set is
constructed at runtime so all important messages play in their entirty.

I keep getting the subject error on build. All functions work on their own,
but not as part of the form class. I don't see what I've done that will mess
it up. Here's the function to get the critical data. BTW, the ShortIPName
returns the first 3 segments of a machine's IP address.
string GetCommandText(string s)
{
try
{
string myIP = ShortIPName();
SqlConnection conn = new SqlConnection("Data Source=testSQL; Integrated
Security=SSPI;Initial Catalog=InfoScreens");
string scalarquery = "SELECT CmdCritical FROM Prefs WHERE IP = '" + myIP +
"'";
SqlCommand myCommand = new SqlCommand(scalarquery,conn);
myCommand.Connection.Open();
string rCommand = (string) myCommand.ExecuteScalar();
myCommand.Connection.Close();
return rCommand;
}

Thanks in advance,

E.
Nov 16 '05 #1
16 7923
"Esteban404" <Es********@discussions.microsoft.com> wrote in message
news:58**********************************@microsof t.com...
string GetCommandText(string s)
{
try
{
string myIP = ShortIPName();
SqlConnection conn = new SqlConnection("Data Source=testSQL; Integrated
Security=SSPI;Initial Catalog=InfoScreens");
string scalarquery = "SELECT CmdCritical FROM Prefs WHERE IP = '" + myIP + "'";
SqlCommand myCommand = new SqlCommand(scalarquery,conn);
myCommand.Connection.Open();
string rCommand = (string) myCommand.ExecuteScalar();
myCommand.Connection.Close();
return rCommand;
}


That's only half the function. At the very least we need a catch or
a finally and a closing brace to even get that to compile. What you've
shown as is fine. The problem is in the part you left our......

--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com


Nov 16 '05 #2
Hey,

The error you are getting is not because of the code you posted. The error
is that you have an if statment somewhere and you are using a "return"
keyword in it.
"Esteban404" <Es********@discussions.microsoft.com> wrote in message
news:58**********************************@microsof t.com...
I have a Winform news kiosk app which connects to a SQL database. When the
main form is running, it reaches a threshold time or number of sequential
operations and then loads another instance with a different dataset. The
datasets are based on local settings which I'm determining from IP space
(all
machines running in that space will show the same news). The "special" set
is
constructed at runtime so all important messages play in their entirty.

I keep getting the subject error on build. All functions work on their
own,
but not as part of the form class. I don't see what I've done that will
mess
it up. Here's the function to get the critical data. BTW, the ShortIPName
returns the first 3 segments of a machine's IP address.
string GetCommandText(string s)
{
try
{
string myIP = ShortIPName();
SqlConnection conn = new SqlConnection("Data Source=testSQL; Integrated
Security=SSPI;Initial Catalog=InfoScreens");
string scalarquery = "SELECT CmdCritical FROM Prefs WHERE IP = '" + myIP
+
"'";
SqlCommand myCommand = new SqlCommand(scalarquery,conn);
myCommand.Connection.Open();
string rCommand = (string) myCommand.ExecuteScalar();
myCommand.Connection.Close();
return rCommand;
}

Thanks in advance,

E.

Nov 16 '05 #3
Tom Jones <cs*************@hotmail.com> wrote:
The error you are getting is not because of the code you posted. The error
is that you have an if statment somewhere and you are using a "return"
keyword in it.


Actually, it could well be because of the code he's posted. The return
statement is in a try block. Presumably there's also a catch block, and
that doesn't have a return statement. There's no need for an "if"
statement to get that error message.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Yea I saw that after I replied...

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Tom Jones <cs*************@hotmail.com> wrote:
The error you are getting is not because of the code you posted. The
error
is that you have an if statment somewhere and you are using a "return"
keyword in it.


Actually, it could well be because of the code he's posted. The return
statement is in a try block. Presumably there's also a catch block, and
that doesn't have a return statement. There's no need for an "if"
statement to get that error message.

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

Nov 16 '05 #5
Thanks, James. Here's the catch block, but it's not doing anything more
specific or catching a sql-specific error.

The catch is nothing:
catch (Exception e)
{
MessageBox.Show(e.ToString());
}

The function ends after this block.

I've tried placing the return statement at the end of the try block and in a
finally block. I have also tried implicit initialization of the variables
locally and globally. I wonder if there is a connection pooling problem. The
form has an sqlDataAdapter that was created by the designer. I needed to
bypass it for this piece of code so a topical select statement can be
constructed (rCommand).

I can sledgehammer it to work, but that doesn't solve the problem.

E.

"James Curran" wrote:
That's only half the function. At the very least we need a catch or
a finally and a closing brace to even get that to compile. What you've
shown as is fine. The problem is in the part you left our......


Nov 16 '05 #6
Esteban404 <Es********@discussions.microsoft.com> wrote:
Thanks, James. Here's the catch block, but it's not doing anything more
specific or catching a sql-specific error.


Exactly. In other words, if an exception is thrown, nothing is being
returned. What do you expect to happen at that point? What do you want
it to return?

The problem has nothing to do with connection pooling - it has to do
with logical flow through your code.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7
Thanks for the reply, Jon.

This works just fine on it own, but it *never* fires in context. That's why
I thought some other problem is happening. I can throw specific errors, but
they none are more informative and I don't know what's happening to fix it
when it occurs. I'm still prototyping the application for best solution.

The function is just supposed to return the string retrieved from the scalar
query and plug it into the form's sqlCommand to build a dataset.

I made a copy of the solution so I can remove the designer code and put in
runtime initialized data to see if that works.

"Jon Skeet [C# MVP]" wrote:
Esteban404 <Es********@discussions.microsoft.com> wrote:
Thanks, James. Here's the catch block, but it's not doing anything more
specific or catching a sql-specific error.


Exactly. In other words, if an exception is thrown, nothing is being
returned. What do you expect to happen at that point? What do you want
it to return?

The problem has nothing to do with connection pooling - it has to do
with logical flow through your code.

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

Nov 16 '05 #8
Esteban404 <Es********@discussions.microsoft.com> wrote:
Thanks for the reply, Jon.

This works just fine on it own, but it *never* fires in context. That's why
I thought some other problem is happening. I can throw specific errors, but
they none are more informative and I don't know what's happening to fix it
when it occurs. I'm still prototyping the application for best solution.
In that case I'd just let the exception bubble up rather than catch it.
The function is just supposed to return the string retrieved from the scalar
query and plug it into the form's sqlCommand to build a dataset.
And what is it supposed to return if an exception occurs?
I made a copy of the solution so I can remove the designer code and put in
runtime initialized data to see if that works.


It won't if you've still got the try/catch in place.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9
I moved the variable init to the top of the function before the try block and
added a finally {} to finish up. Before it gets to the function, I test to
make sure it will fire or return "" string and test for that at the form
init(). Seems to work now. Testing time.

Thanks for the discussion.

E.

"Tom Jones" wrote:
Yea I saw that after I replied...

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Tom Jones <cs*************@hotmail.com> wrote:
The error you are getting is not because of the code you posted. The
error
is that you have an if statment somewhere and you are using a "return"
keyword in it.


Actually, it could well be because of the code he's posted. The return
statement is in a try block. Presumably there's also a catch block, and
that doesn't have a return statement. There's no need for an "if"
statement to get that error message.

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


Nov 16 '05 #10
You cannot create a method that returns a string only if the try is
successful because the compiler expects the return statement for this
function in the catch or the finally block too.

with regards,
J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #11
That's very interesting. I'll look into that documentation. I do not see that
before. I have it working now, but I will try to mark up the previous code
and see if it fires with returns in the other blocks.

Thanks again, J.V.!

"Ravichandran J.V." wrote:
You cannot create a method that returns a string only if the try is
successful because the compiler expects the return statement for this
function in the catch or the finally block too.

with regards,
J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #12
Ravichandran J.V. wrote:
You cannot create a method that returns a string only if the try is
successful because the compiler expects the return statement for this
function in the catch or the finally block too.


this compiles fine:
public string Bla()
{
string toReturn = "Foo";
try
{
Console.WriteLine("Some output");
return toReturn;
}
catch(Exception ex)
{
throw ex;
}
finally
{
}
}

Frans.

--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Nov 16 '05 #13
That's because the throw is a return statement also.

--
John Wood
Blog: http://spaces.msn.com/members/johnwood
"Frans Bouma [C# MVP]" <pe******************@xs4all.nl> wrote in message
news:e1**************@TK2MSFTNGP15.phx.gbl...
Ravichandran J.V. wrote:
You cannot create a method that returns a string only if the try is
successful because the compiler expects the return statement for this
function in the catch or the finally block too.


this compiles fine:
public string Bla()
{
string toReturn = "Foo";
try
{
Console.WriteLine("Some output");
return toReturn;
}
catch(Exception ex)
{
throw ex;
}
finally
{
}
}

Frans.

--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------

Nov 16 '05 #14
Without looking at anything else just the

not all code paths return a value

that the OP stated is suffiecient because this compiler message appears
not just in .Net but in any languages.

with regards,
J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #15
John Wood <my-name@priorganize-dot-com> wrote:
That's because the throw is a return statement also.


It's not - certainly not by the language specification definition.

(For one thing, it doesn't even guarantee that control leaves the
method.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #16
This error happens when write the return command inside a loop where the
compiler suspects the code will not be evaluated all the time.

example:

private string GetToken(string TokenString, int TokenPos)
{
int x, i, j;
x = 1; i = 0; j = 0;
while ( i < TokenPos )
{
j = x;
while (( x <= TokenString.Length ) && (
TokenString.Substring(x,1).Trim() != ";" ))
{
x++;
}
i++;
x++;
//// try put return here
}
return TokenString.Substring(j, x - j - 1).Trim();
}
The return command inside the loop cause the error.
Reginaldo

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for...
6
by: Bruce W.1 | last post by:
The intent of my web service is an RSS feed from a blog. Originally I used a StringBuilder to make the XML and returned a string from the webmethod. But this doesn't display properly in IE. So...
5
by: n_o_s_p_a__m | last post by:
Can't compile. Does this mean that all functions that throw exceptions must be of return type void? examples: // won't compile: "not all code paths return a value" public override int Run() {...
12
by: Jose Fernandez | last post by:
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 public SqlDataReader CoverNews(string Sport)...
4
by: OutdoorGuy | last post by:
Greetings, I am attempting to compile the code below, but I am receiving an error message when I do so. The error message is: "CSO161: 'Forloop.CalcAvg(int)': Not all code paths return a...
3
by: Oberon | last post by:
How do I deal with this? I am getting an error for each get in the Game class (see code below). In the simplified example below I have reduced this to just 3 fields, one which can be NULL. I...
7
by: Robert | last post by:
I have the function below. it returns a "simpleresult" which I've also included the definition of below. In VS2005 (after upgrading the project), I get a warning indicating that Function...
1
by: fretIT | last post by:
Hello, when I write web method using C# in Visual basic 2005, I can't return the string value to the client request. I got such kind of error Not all code paths return a value. Don't know how...
9
by: reachmsn | last post by:
Hi, At the url http://www.python.org/doc/essays/graphs.html there is some code by Guido Van Rossum for computing paths through a graph - I have pasted it below for reference - Let's write a...
4
Markus
by: Markus | last post by:
I have a method that checks whether the passed argument is present in an array. The method needs to return a bool value. Take a look at said method: private bool IPExist(string IP) { ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.