Error creating connection class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter Afonin

    Error creating connection class

    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\c lConnect.cs(18) :
    'connect_class. clConnect.Creat eDataset(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.Sql Client;
    using System.Data;
    namespace connect_class
    {
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    public class clConnect
    {
    public clConnect()
    {
    }
    // Create dataset
    public DataSet CreateDataset(s tring sSQL)
    {
    string sConn="Data Source=Win2000; Initial Catalog=CustSer v;User
    ID=mlut2;Passwo rd=dk%$one22";
    SqlConnection Con = new SqlConnection() ;
    SqlCommand Cmd = new SqlCommand();
    SqlDataAdapter da = new SqlDataAdapter( );
    DataSet ds = new DataSet("Report ");
    try
    {

    Cmd.CommandType =CommandType.Te xt;
    Cmd.CommandText =sSQL;
    Cmd.Connection= Con;
    Cmd.CommandTime out=480;
    da.SelectComman d=Cmd;
    Con.ConnectionS tring=sConn;
    Con.Open();
    da.Fill(ds);
    return ds;
    }
    catch(System.Ex ception)
    {
    }
    finally
    {
    if (Con !=null)
    Con.Close();
    if (Cmd !=null)
    Cmd.Dispose();
    if (da !=null)
    da.Dispose();
    }
    }
    }
    }

    Thank you,

    --
    Peter Afonin


  • Frank Oquendo

    #2
    Re: Error creating connection class

    Peter Afonin wrote:[color=blue]
    > not all code paths return a value[/color]

    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.


    (Pull the pin to reply)


    Comment

    • Peter Afonin

      #3
      Re: Error creating connection class

      Thank you, Frank.

      "Frank Oquendo" <frankopin@acad x.com> wrote in message
      news:eMmLFpLqDH A.976@tk2msftng p13.phx.gbl...[color=blue]
      > Peter Afonin wrote:[color=green]
      > > not all code paths return a value[/color]
      >
      > 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)
      >
      >[/color]


      Comment

      • Bruno Jouhier [MVP]

        #4
        Re: Error creating connection class

        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" <pafo@specialty pulltabs.com> a écrit dans le message de
        news:eZ$WsfLqDH A.3612@TK2MSFTN GP11.phx.gbl...[color=blue]
        > 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\c lConnect.cs(18) :
        > 'connect_class. clConnect.Creat eDataset(string )': not all code paths return[/color]
        a[color=blue]
        > value"
        >
        > I would really appreciate if you could tell me what am I doing wrong. The
        > code is below.
        >
        > using System;
        > using System.Data.Sql Client;
        > using System.Data;
        > namespace connect_class
        > {
        > /// <summary>
        > /// Summary description for Class1.
        > /// </summary>
        > public class clConnect
        > {
        > public clConnect()
        > {
        > }
        > // Create dataset
        > public DataSet CreateDataset(s tring sSQL)
        > {
        > string sConn="Data Source=Win2000; Initial Catalog=CustSer v;User
        > ID=mlut2;Passwo rd=dk%$one22";
        > SqlConnection Con = new SqlConnection() ;
        > SqlCommand Cmd = new SqlCommand();
        > SqlDataAdapter da = new SqlDataAdapter( );
        > DataSet ds = new DataSet("Report ");
        > try
        > {
        >
        > Cmd.CommandType =CommandType.Te xt;
        > Cmd.CommandText =sSQL;
        > Cmd.Connection= Con;
        > Cmd.CommandTime out=480;
        > da.SelectComman d=Cmd;
        > Con.ConnectionS tring=sConn;
        > Con.Open();
        > da.Fill(ds);
        > return ds;
        > }
        > catch(System.Ex ception)
        > {
        > }
        > finally
        > {
        > if (Con !=null)
        > Con.Close();
        > if (Cmd !=null)
        > Cmd.Dispose();
        > if (da !=null)
        > da.Dispose();
        > }
        > }
        > }
        > }
        >
        > Thank you,
        >
        > --
        > Peter Afonin
        >
        >[/color]


        Comment

        • Glenn B

          #5
          Re: Error creating connection class

          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 ( connectionStrin g ) )
          {
          //Do stuff with con
          }


          /CSharpSpecStart .asp

          Section 8.13

          Glenn

          "Peter Afonin" <pafo@specialty pulltabs.com> wrote in message
          news:eZ$WsfLqDH A.3612@TK2MSFTN GP11.phx.gbl...[color=blue]
          > 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\c lConnect.cs(18) :
          > 'connect_class. clConnect.Creat eDataset(string )': not all code paths return[/color]
          a[color=blue]
          > value"
          >
          > I would really appreciate if you could tell me what am I doing wrong. The
          > code is below.
          >
          > using System;
          > using System.Data.Sql Client;
          > using System.Data;
          > namespace connect_class
          > {
          > /// <summary>
          > /// Summary description for Class1.
          > /// </summary>
          > public class clConnect
          > {
          > public clConnect()
          > {
          > }
          > // Create dataset
          > public DataSet CreateDataset(s tring sSQL)
          > {
          > string sConn="Data Source=Win2000; Initial Catalog=CustSer v;User
          > ID=mlut2;Passwo rd=dk%$one22";
          > SqlConnection Con = new SqlConnection() ;
          > SqlCommand Cmd = new SqlCommand();
          > SqlDataAdapter da = new SqlDataAdapter( );
          > DataSet ds = new DataSet("Report ");
          > try
          > {
          >
          > Cmd.CommandType =CommandType.Te xt;
          > Cmd.CommandText =sSQL;
          > Cmd.Connection= Con;
          > Cmd.CommandTime out=480;
          > da.SelectComman d=Cmd;
          > Con.ConnectionS tring=sConn;
          > Con.Open();
          > da.Fill(ds);
          > return ds;
          > }
          > catch(System.Ex ception)
          > {
          > }
          > finally
          > {
          > if (Con !=null)
          > Con.Close();
          > if (Cmd !=null)
          > Cmd.Dispose();
          > if (da !=null)
          > da.Dispose();
          > }
          > }
          > }
          > }
          >
          > Thank you,
          >
          > --
          > Peter Afonin
          >
          >[/color]


          Comment

          • Peter Afonin

            #6
            Re: Error creating connection class

            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" <unexus2000@yah oo.co.uk> wrote in message
            news:#GCbBASqDH A.372@TK2MSFTNG P11.phx.gbl...[color=blue]
            > 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[/color]
            data[color=blue]
            > classes that your using should implement IDisposable.
            >
            > example
            >
            > using ( OleDbConnection con = new OleDbConnection ( connectionStrin g ) )
            > {
            > //Do stuff with con
            > }
            >
            >[/color]
            http://msdn.microsoft.com/library/de...us/csspec/html[color=blue]
            > /CSharpSpecStart .asp
            >
            > Section 8.13
            >
            > Glenn
            >
            > "Peter Afonin" <pafo@specialty pulltabs.com> wrote in message
            > news:eZ$WsfLqDH A.3612@TK2MSFTN GP11.phx.gbl...[color=green]
            > > 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[/color][/color]
            I'm[color=blue][color=green]
            > > getting the following error when trying to compile the project:
            > >
            > > "D:\C#_Projects \connect_class\ connect_class\c lConnect.cs(18) :
            > > 'connect_class. clConnect.Creat eDataset(string )': not all code paths[/color][/color]
            return[color=blue]
            > a[color=green]
            > > value"
            > >
            > > I would really appreciate if you could tell me what am I doing wrong.[/color][/color]
            The[color=blue][color=green]
            > > code is below.
            > >
            > > using System;
            > > using System.Data.Sql Client;
            > > using System.Data;
            > > namespace connect_class
            > > {
            > > /// <summary>
            > > /// Summary description for Class1.
            > > /// </summary>
            > > public class clConnect
            > > {
            > > public clConnect()
            > > {
            > > }
            > > // Create dataset
            > > public DataSet CreateDataset(s tring sSQL)
            > > {
            > > string sConn="Data Source=Win2000; Initial Catalog=CustSer v;User
            > > ID=mlut2;Passwo rd=dk%$one22";
            > > SqlConnection Con = new SqlConnection() ;
            > > SqlCommand Cmd = new SqlCommand();
            > > SqlDataAdapter da = new SqlDataAdapter( );
            > > DataSet ds = new DataSet("Report ");
            > > try
            > > {
            > >
            > > Cmd.CommandType =CommandType.Te xt;
            > > Cmd.CommandText =sSQL;
            > > Cmd.Connection= Con;
            > > Cmd.CommandTime out=480;
            > > da.SelectComman d=Cmd;
            > > Con.ConnectionS tring=sConn;
            > > Con.Open();
            > > da.Fill(ds);
            > > return ds;
            > > }
            > > catch(System.Ex ception)
            > > {
            > > }
            > > finally
            > > {
            > > if (Con !=null)
            > > Con.Close();
            > > if (Cmd !=null)
            > > Cmd.Dispose();
            > > if (da !=null)
            > > da.Dispose();
            > > }
            > > }
            > > }
            > > }
            > >
            > > Thank you,
            > >
            > > --
            > > Peter Afonin
            > >
            > >[/color]
            >
            >[/color]


            Comment

            • Bruno Jouhier [MVP]

              #7
              Re: Error creating connection class

              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" <pafo@specialty pulltabs.com> a écrit dans le message de
              news:uTLVMZVqDH A.2064@TK2MSFTN GP11.phx.gbl...[color=blue]
              > 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" <unexus2000@yah oo.co.uk> wrote in message
              > news:#GCbBASqDH A.372@TK2MSFTNG P11.phx.gbl...[color=green]
              > > 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[/color][/color]
              about[color=blue][color=green]
              > > disposing of unmanaged resources even if an exception occurs. All the[/color]
              > data[color=green]
              > > classes that your using should implement IDisposable.
              > >
              > > example
              > >
              > > using ( OleDbConnection con = new OleDbConnection ( connectionStrin g ) )
              > > {
              > > //Do stuff with con
              > > }
              > >
              > >[/color]
              >[/color]
              http://msdn.microsoft.com/library/de...us/csspec/html[color=blue][color=green]
              > > /CSharpSpecStart .asp
              > >
              > > Section 8.13
              > >
              > > Glenn
              > >
              > > "Peter Afonin" <pafo@specialty pulltabs.com> wrote in message
              > > news:eZ$WsfLqDH A.3612@TK2MSFTN GP11.phx.gbl...[color=darkred]
              > > > Hello:
              > > >
              > > > I'm trying to convert the connection class that I created in VB.Net[/color][/color][/color]
              for[color=blue][color=green][color=darkred]
              > > > ASP.Net application into C#. I'm doing something incorrectly, because[/color][/color]
              > I'm[color=green][color=darkred]
              > > > getting the following error when trying to compile the project:
              > > >
              > > > "D:\C#_Projects \connect_class\ connect_class\c lConnect.cs(18) :
              > > > 'connect_class. clConnect.Creat eDataset(string )': not all code paths[/color][/color]
              > return[color=green]
              > > a[color=darkred]
              > > > value"
              > > >
              > > > I would really appreciate if you could tell me what am I doing wrong.[/color][/color]
              > The[color=green][color=darkred]
              > > > code is below.
              > > >
              > > > using System;
              > > > using System.Data.Sql Client;
              > > > using System.Data;
              > > > namespace connect_class
              > > > {
              > > > /// <summary>
              > > > /// Summary description for Class1.
              > > > /// </summary>
              > > > public class clConnect
              > > > {
              > > > public clConnect()
              > > > {
              > > > }
              > > > // Create dataset
              > > > public DataSet CreateDataset(s tring sSQL)
              > > > {
              > > > string sConn="Data Source=Win2000; Initial Catalog=CustSer v;User
              > > > ID=mlut2;Passwo rd=dk%$one22";
              > > > SqlConnection Con = new SqlConnection() ;
              > > > SqlCommand Cmd = new SqlCommand();
              > > > SqlDataAdapter da = new SqlDataAdapter( );
              > > > DataSet ds = new DataSet("Report ");
              > > > try
              > > > {
              > > >
              > > > Cmd.CommandType =CommandType.Te xt;
              > > > Cmd.CommandText =sSQL;
              > > > Cmd.Connection= Con;
              > > > Cmd.CommandTime out=480;
              > > > da.SelectComman d=Cmd;
              > > > Con.ConnectionS tring=sConn;
              > > > Con.Open();
              > > > da.Fill(ds);
              > > > return ds;
              > > > }
              > > > catch(System.Ex ception)
              > > > {
              > > > }
              > > > finally
              > > > {
              > > > if (Con !=null)
              > > > Con.Close();
              > > > if (Cmd !=null)
              > > > Cmd.Dispose();
              > > > if (da !=null)
              > > > da.Dispose();
              > > > }
              > > > }
              > > > }
              > > > }
              > > >
              > > > Thank you,
              > > >
              > > > --
              > > > Peter Afonin
              > > >
              > > >[/color]
              > >
              > >[/color]
              >
              >[/color]


              Comment

              Working...