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;
public partial class _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.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

while (Dr.Read())
{
if ((UserName == Dr["name"].ToString()) & (Password == Dr["Password"].ToString()))
{
boolReturnValue = true;
}

Dr.Close();


}
return boolReturnValue;

Have the code that way and it should work.

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

Create and Open connection
Create and execute a command to get a datareader
    Read next record
Perform actions for each record.
Close the reader
Close the connection
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.

 public bool FindExistItem(string cardid)
    {
        if (CartTable.Rows.Count > 0)
        {
            foreach (DataRow drow in CartTable.Rows)
            {
                if (cardid == drow["ProductId"].ToString())
                {
                    return true;
                }
            }
        }
    }

    i have write this function in c# which shows me error not all code path return a value... plz help me

First, you couldn't create a new thread for this, you had to find a six year old post to reply to?

Second, what value is returned when CartTable.Rows.Count == 0?

public bool FindExistItem(string cardid)
    {
        int r2 = 0;
        if (CartTable.Rows.Count > 0)
        {
            foreach (DataRow drow in CartTable.Rows)
            {
                if (cardid == drow["ProductId"].ToString())
                {
                    r2=1;
                }
            }
        }
        if (r2 == 1)
        {
            return true;
        }
        else
        {
            return false;
        }
        AddToSession();
    }
This article has been dead for over six months. Start a new discussion instead.