I keep getting this error:
c:\inetpub\wwwroot\Phoenix\Login.aspx.cs(85): 'Phoenix.WebForm1.DBConnection(string, string)': not all code paths return a value
I have already tried putting everything in the DBConnection function to comments but still the problem persisted. This is crucial that i get this working. I have watched examples from the guides in daniweb but they don't give working answers, everything is either in the wrong programming language or connecting to sql server instead of access database. My login needs to be done with C# and needs to check password from access database. If anyone can tell me what is wrong with my code i would appreciate it.
Here is the code:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
//Loginia varten lisätyt
using System.Web.Security; //Required class for authentication
using System.Configuration; //Required for web.config appsettings
using System.Data.OleDb; //required for access database import
namespace Phoenix
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox txtUserName;
protected System.Web.UI.WebControls.RequiredFieldValidator rvUserValidator;
protected System.Web.UI.WebControls.TextBox txtPassword;
protected System.Web.UI.WebControls.RequiredFieldValidator rvPasswordValidator;
protected System.Web.UI.WebControls.Button cmdSubmit;
protected System.Web.UI.WebControls.Label lblMessage;
protected System.Web.UI.WebControls.Label lblMessage2;
protected System.Web.UI.WebControls.ValidationSummary Validationsummary1;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void cmdSubmit_Click(object sender, System.EventArgs e)
{
if (Page.IsValid) //Meaning the control validation was succesful
{ //Connect to database for user validation
if (DBConnection(txtUserName.Text.Trim(), txtPassword.Text.Trim()))
{ //Redirect to default.aspx
FormsAuthentication.RedirectFromLoginPage (txtUserName.Text, false);
}
else //Username and/or password was invalid
{
lblMessage.Text = "Invalid Login, please try again!";
//Increment the LoginCount (attempts)
//Session("LoginCount") = CInt(Session("LoginCount")) + 1
//Determine the Number of Tries
//If Session("LoginCount").Equals(intMaxLoginAttempts) Then
//Response.Redirect("Denied.aspx")
//End If
//If CInt(Session("Num_of_Tries")) > 2 Then ' ||||| If Exceeds then Deny!
//esponse.Redirect("Denied.aspx")
//End If
}
}
}
private bool DBConnection(string strUserName, string strPassword)
{
//First is the Connection Object for an Access DB
OleDbConnection MyConn = new OleDbConnection(ConfigurationSettings.AppSettings["strConn"]);
//Create oleDb command object
//Pass in stored procedure
//Set CommandType to stored procedure
OleDbCommand MyCmd = new OleDbCommand("sp_ValidateUser", MyConn);
//To access a stored procedure in access - requires a command object
MyCmd.CommandType = CommandType.StoredProcedure;
//Create parameter objects for values passed in
OleDbParameter objParam1, objParam2;
//Add the parameters to the parameters collection of the
//command object, and set their datatypes (OleDb in this case)
objParam1 = MyCmd.Parameters.Add("@UserName", OleDbType.Char);
objParam2 = MyCmd.Parameters.Add("@Password", OleDbType.Char);
//Set the direction of the parameters...input, output, etc
objParam1.Direction = ParameterDirection.Input;
objParam2.Direction = ParameterDirection.Input;
//Set the value(s) of the parameters to the passed in values
objParam1.Value = strUserName;
objParam2.Value = strPassword;
try //Basic try-catch block
{ //Check if connection to DB is already open, if not the open a connection
if(MyConn.State == ConnectionState.Closed)
{ //DB not already open, so lets open it
MyConn.Open();
}
//Create oleDb data reader
OleDbDataReader objReader;
objReader = MyCmd.ExecuteReader(CommandBehavior.CloseConnection);
//Close the reader and the connection closes with it
while(objReader.Read())
{
if((int)objReader.GetValue(0) != 1)
{
lblMessage.Text = "Invalid Login!";
return false;
}
else
{
objReader.Close();
return true;
}
}
}
catch(Exception ex)
{
lblMessage2.Text = "Error Connecting to the database!";
return false;
}
}
}
}