Why this function return value error?

Hi...
I have a class in which I am returning a SqlConnection object the code has been
written in a try -catch block which is given below.
while I am trying to compile it is genrating this error

"Dal.cs(15): 'DataComponents.connectionClass.SetConnection()': not all code paths return a value"

Please help me.

using System.Data .SqlClient ;

using System.Data ;

using System;

namespace DataComponents

{

///<summary>

/// Summary description for Dal.

///</summary>

publicclass connectionClass

{

staticstring connectionString="server=nic-dd;database=northwind;trusted_connection=true";

static SqlConnection dbConnect;

publicstatic SqlConnection SetConnection()

{

try

{

dbConnect=new SqlConnection(connectionString );

//throw new Exception () ;

return dbConnect;

}

catch(Exception Ex)

{

Console.WriteLine ("Error occured:{0}",Ex.Message );

}

finally

{

dbConnect.Close();

}

}

}

}

[2545 byte] By [AshokKumarRoy] at [2008-1-28]
# 1
Hey,
Becouse your function doesnt return a value in everytime....
think like this...
in try block...

class="txt4">

dbConnect=new SqlConnection(connectionString );
// if there is exception it will go to catch block.....
// so it wont visit the next line
// next line returns your value...

return dbConnect;



so you have to write


class
="txt4">

catch(Exception Ex)
{
Console.WriteLine ("Error occured:{0}",Ex.Message );
// and return null;

return null;
}



it solves your problem
good works...
Doga Oztuzun
UnquaLeX at 2007-8-21 > top of Msdn Tech,Visual C#,Visual C# Language...
# 2

Exactly ...Now its working..
Thank you so much Smile
regards ashok

AshokKumarRoy at 2007-8-21 > top of Msdn Tech,Visual C#,Visual C# Language...