943,987 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 18854
  • C# RSS
Feb 6th, 2007
0

solution for not all code paths return a value for bool

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
msamir is offline Offline
7 posts
since Feb 2007
Feb 6th, 2007
0

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

you need to move the return statement outside of the while loop.
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005
Feb 6th, 2007
0

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

C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 70
Solved Threads: 4
Junior Poster
RwCC is offline Offline
173 posts
since Jan 2006
Feb 8th, 2007
0

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

You should also have the DR.Close() statement outside of your loop. The general structure should be.

C# Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
reklawnnelg is offline Offline
3 posts
since Feb 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: conversion of string into binary
Next Thread in C# Forum Timeline: i need login code in C#





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC