solution for not all code paths return a value for bool

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2007
Posts: 7
Reputation: msamir is an unknown quantity at this point 
Solved Threads: 0
msamir msamir is offline Offline
Newbie Poster

solution for not all code paths return a value for bool

 
0
  #1
Feb 6th, 2007
i have creted this code for login control:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
publicpartialclass_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
bool Autheticated = false;
Autheticated = SiteLevelCustomAutheticationMethod(Login1.UserName, Login1.Password);
e.Authenticated = Autheticated;
if (Autheticated == true)
{
Response.Write("Home.aspx");
}

}
private bool SiteLevelCustomAutheticationMethod (string UserName, string Password)
{
bool boolReturnValue = false;
string scon = "SERVER=.; INITIAL CATALOG=Villaplus; UID=sa; PWD=ash; ";
SqlConnection con = new SqlConnection(scon);
String strSQL = "Select * From Login";
SqlCommand com = new SqlCommand(strSQL, con);
SqlDataReader Dr;
con.Open();
Dr = com.ExecuteReader();
while (Dr.Read())
{
if ((UserName == Dr["name"].ToString()) & (Password == Dr["Password"].ToString()))
{
boolReturnValue = true;
}

Dr.Close();

return boolReturnValue;
}

}
}



but it gives error:

'_Default.SiteLevelCustomAutheticationMethod(string, string)': not all code paths return a value .
please give solution for this.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: solution for not all code paths return a value for bool

 
0
  #2
Feb 6th, 2007
you need to move the return statement outside of the while loop.
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 173
Reputation: RwCC is an unknown quantity at this point 
Solved Threads: 4
RwCC's Avatar
RwCC RwCC is offline Offline
Junior Poster

Re: solution for not all code paths return a value for bool

 
0
  #3
Feb 6th, 2007
  1. while (Dr.Read())
  2. {
  3. if ((UserName == Dr["name"].ToString()) & (Password == Dr["Password"].ToString()))
  4. {
  5. boolReturnValue = true;
  6. }
  7.  
  8. Dr.Close();
  9.  
  10.  
  11. }
  12. return boolReturnValue;

Have the code that way and it should work.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 3
Reputation: reklawnnelg is an unknown quantity at this point 
Solved Threads: 0
reklawnnelg reklawnnelg is offline Offline
Newbie Poster

Re: solution for not all code paths return a value for bool

 
0
  #4
Feb 8th, 2007
You should also have the DR.Close() statement outside of your loop. The general structure should be.

  1. Create and Open connection
  2. Create and execute a command to get a datareader
  3. Read next record
  4. Perform actions for each record.
  5. Close the reader
  6. Close the connection
  7. Return the result.

If you want to break out of the loop to return before reading the whole resultset, then use the break keyword. It will drop you outside of the loop.
Last edited by reklawnnelg; Feb 8th, 2007 at 10:00 am. Reason: Fix formatting
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC