Error creating connection class

P

Peter Afonin

Hello:

I'm trying to convert the connection class that I created in VB.Net for
ASP.Net application into C#. I'm doing something incorrectly, because I'm
getting the following error when trying to compile the project:

"D:\C#_Projects\connect_class\connect_class\clConnect.cs(18):
'connect_class.clConnect.CreateDataset(string)': not all code paths return a
value"

I would really appreciate if you could tell me what am I doing wrong. The
code is below.

using System;
using System.Data.SqlClient;
using System.Data;
namespace connect_class
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class clConnect
{
public clConnect()
{
}
// Create dataset
public DataSet CreateDataset(string sSQL)
{
string sConn="Data Source=Win2000;Initial Catalog=CustServ;User
ID=mlut2;Password=dk%$one22";
SqlConnection Con = new SqlConnection();
SqlCommand Cmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet("Report");
try
{

Cmd.CommandType=CommandType.Text;
Cmd.CommandText=sSQL;
Cmd.Connection=Con;
Cmd.CommandTimeout=480;
da.SelectCommand=Cmd;
Con.ConnectionString=sConn;
Con.Open();
da.Fill(ds);
return ds;
}
catch(System.Exception)
{
}
finally
{
if (Con !=null)
Con.Close();
if (Cmd !=null)
Cmd.Dispose();
if (da !=null)
da.Dispose();
}
}
}
}

Thank you,
 
F

Frank Oquendo

Peter said:
not all code paths return a value

The error means exactly that. The only time a DataSet gets eturned is if
everything goes off without a hitch. To fix this, move the line 'return
ds;' outside of your try/catch block. Inside your catch block, add 'ds =
null;'

That will ensure you don't return an invalid DataSet and all paths will
return a value, solving your compiler error.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
P

Peter Afonin

Thank you, Frank.

Frank Oquendo said:
The error means exactly that. The only time a DataSet gets eturned is if
everything goes off without a hitch. To fix this, move the line 'return
ds;' outside of your try/catch block. Inside your catch block, add 'ds =
null;'

That will ensure you don't return an invalid DataSet and all paths will
return a value, solving your compiler error.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
B

Bruno Jouhier [MVP]

You can also fix it by removing the catch clause.

Then, instead of returning null when something goes wrong, your method will
let the exception propagate to the caller.

Bruno.
 
G

Glenn B

In the catch block throw the exception.

Also, instead of having a finally block, why not take a look at the
IDisposable pattern. Using as 'using' block you won't have to worry about
disposing of unmanaged resources even if an exception occurs. All the data
classes that your using should implement IDisposable.

example

using ( OleDbConnection con = new OleDbConnection( connectionString ) )
{
//Do stuff with con
}

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csspec/html
/CSharpSpecStart.asp

Section 8.13

Glenn
 
P

Peter Afonin

Thank you everyone. All your suggestions worked.

Just one question: Why in VB.Net it was perfectly fine to keep "return ds"
inside the try...catch block, but in C# this doesn't work. What is the
difference?

Peter

Glenn B said:
In the catch block throw the exception.

Also, instead of having a finally block, why not take a look at the
IDisposable pattern. Using as 'using' block you won't have to worry about
disposing of unmanaged resources even if an exception occurs. All the data
classes that your using should implement IDisposable.

example

using ( OleDbConnection con = new OleDbConnection( connectionString ) )
{
//Do stuff with con
}

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csspec/html
/CSharpSpecStart.asp

Section 8.13

Glenn

Peter Afonin said:
Hello:

I'm trying to convert the connection class that I created in VB.Net for
ASP.Net application into C#. I'm doing something incorrectly, because I'm
getting the following error when trying to compile the project:

"D:\C#_Projects\connect_class\connect_class\clConnect.cs(18):
'connect_class.clConnect.CreateDataset(string)': not all code paths
return
 
B

Bruno Jouhier [MVP]

You can keep the "return ds" inside the try block if you get rid of the
empty catch.

The problem is that your empty catch clause does not end with a return or a
throw. So, the empty catch clause is the code path that makes the compiler
unhappy.

Bruno

Peter Afonin said:
Thank you everyone. All your suggestions worked.

Just one question: Why in VB.Net it was perfectly fine to keep "return ds"
inside the try...catch block, but in C# this doesn't work. What is the
difference?

Peter

Glenn B said:
In the catch block throw the exception.

Also, instead of having a finally block, why not take a look at the
IDisposable pattern. Using as 'using' block you won't have to worry about
disposing of unmanaged resources even if an exception occurs. All the data
classes that your using should implement IDisposable.

example

using ( OleDbConnection con = new OleDbConnection( connectionString ) )
{
//Do stuff with con
}
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csspec/html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top