By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,192 Members | 1,818 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,192 IT Pros & Developers. It's quick & easy.

Error creating connection class

P: n/a
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\clConn ect.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,

--
Peter Afonin
Nov 15 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Peter Afonin wrote:
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)
Nov 15 '05 #2

P: n/a
Thank you, Frank.

"Frank Oquendo" <fr*******@acadx.com> wrote in message
news:eM*************@tk2msftngp13.phx.gbl...
Peter Afonin wrote:
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)

Nov 15 '05 #3

P: n/a
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.

"Peter Afonin" <pa**@specialtypulltabs.com> a écrit dans le message de
news:eZ**************@TK2MSFTNGP11.phx.gbl...
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\clConn ect.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,

--
Peter Afonin

Nov 15 '05 #4

P: n/a
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/de...us/csspec/html
/CSharpSpecStart.asp

Section 8.13

Glenn

"Peter Afonin" <pa**@specialtypulltabs.com> wrote in message
news:eZ**************@TK2MSFTNGP11.phx.gbl...
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\clConn ect.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,

--
Peter Afonin

Nov 15 '05 #5

P: n/a
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" <un********@yahoo.co.uk> wrote in message
news:#G*************@TK2MSFTNGP11.phx.gbl...
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/de...us/csspec/html /CSharpSpecStart.asp

Section 8.13

Glenn

"Peter Afonin" <pa**@specialtypulltabs.com> wrote in message
news:eZ**************@TK2MSFTNGP11.phx.gbl...
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\clConn ect.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,

--
Peter Afonin


Nov 15 '05 #6

P: n/a
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" <pa**@specialtypulltabs.com> a écrit dans le message de
news:uT**************@TK2MSFTNGP11.phx.gbl...
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" <un********@yahoo.co.uk> wrote in message
news:#G*************@TK2MSFTNGP11.phx.gbl...
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/de...us/csspec/html
/CSharpSpecStart.asp

Section 8.13

Glenn

"Peter Afonin" <pa**@specialtypulltabs.com> wrote in message
news:eZ**************@TK2MSFTNGP11.phx.gbl...
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\clConn ect.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,

--
Peter Afonin



Nov 15 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.