前台就一个登入控件
WEB.CONFIG里增加了句
<add name="SQLconnstr" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|music.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True" providerName="System.Data.SqlClient" />
    protected void Login1_Authenticate1(object sender, AuthenticateEventArgs e)
    {
        bool Authenticated = false;
        Authenticated = SiteLevelCustomAuthenticationMethod(Login1.UserName, Login1.Password);
        e.Authenticated = Authenticated;
        if (Authenticated == true)
        {
            Response.Redirect("Home.aspx");
        }    }
    private bool SiteLevelCustomAuthenticationMethod(string UserName, string Password)
    {
        bool boolReturnValue = false;
        SqlConnection Connection =  new SqlConnection(ConfigurationManager.ConnectionStrings["SQLconnStr"].ConnectionString);
        String strSQL = "Select * From user";
        SqlCommand command = new SqlCommand(strSQL, Connection);
        SqlDataReader Dr;
        Connection.Open();
        Dr = command.ExecuteReader();
        while (Dr.Read())
        {
            if ((UserName == Dr["name"].ToString()) & (Password == Dr["Paswd"].ToString()))
            {
                boolReturnValue = true;
            }
            Dr.Close();
            return boolReturnValue;
        }
    }运行时提示SiteLevelCustomAuthenticationMethod并非所有的代码路径都返回值 
这是怎么回事?

解决方案 »

  1.   

    把return boolReturnValue;写while外面试试
      

  2.   

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

  3.   

    第二个方法有问题
     private bool SiteLevelCustomAuthenticationMethod(string UserName, string Password)
        {
            bool boolReturnValue = false;
            SqlConnection Connection =  new SqlConnection(ConfigurationManager.ConnectionStrings["SQLconnStr"].ConnectionString);
            String strSQL = "Select * From user";
            SqlCommand command = new SqlCommand(strSQL, Connection);
            SqlDataReader Dr;
            Connection.Open();
            Dr = command.ExecuteReader();
            while (Dr.Read())
            {
                if ((UserName == Dr["name"].ToString()) & (Password == Dr["Paswd"].ToString()))
                {
                    boolReturnValue = true;
                }
                Dr.Close();
                
            }
    return boolReturnValue;
        }
      

  4.   

    return boolReturnValue;
      

  5.   

    return boolReturnValue;写在while循环后面。
      

  6.   

    恩..改好了..能运行出来了..就是用户名密码怎么输都是错误...
    断点后看了下..while 语句是运行了..就是没找到数据库中对应的..
    我是哪里错了
      

  7.   

    while (Dr.Read())
            {
                if ((UserName == Dr["name"].ToString()) & (Password == Dr["Paswd"].ToString()))
                {
                    boolReturnValue = true;
                }
             }
     Dr.Close();//循环完了再关闭,应该是这里的问题。
     return boolReturnValue;
      

  8.   

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

  9.   

    注意你写的这些  &&
      

  10.   

     private bool SiteLevelCustomAuthenticationMethod(string UserName, string Password)
        {
            bool boolReturnValue = false;
            SqlConnection Connection =  new SqlConnection(ConfigurationManager.ConnectionStrings["SQLconnStr"].ConnectionString);
            String strSQL = "Select * From user";
            SqlCommand command = new SqlCommand(strSQL, Connection);
            SqlDataReader Dr;
            Connection.Open();
            Dr = command.ExecuteReader();
            while (Dr.Read())
            {
                if ((UserName == Dr["name"].ToString()) & (Password == Dr["Paswd"].ToString()))
                {
                    boolReturnValue = true;
                }
                Dr.Close();
                        }
    return boolReturnValue;
        }
      

  11.   

    if ((UserName == Dr["name"].ToString())&&(Password == Dr["Paswd"].ToString()))
      

  12.   


    while (Dr.Read())
            {
                if ((UserName == Dr["name"].ToString()) & (Password == Dr["Paswd"].ToString()))
                {
                    boolReturnValue = true;
                }
                Dr.Close();
                return boolReturnValue;
            }很明显。如果dr为空的话。就没返回值了!
      

  13.   

    楼主,你不觉得你这样写的话对数据有很大的压力吗?为什么要查询出所有的User然后一一匹配呢?而且用Reader的话,一直和数据库连接,如果登录人数多的话,数据库不是要跑死?
    你可以写一个方法,将用户名和密码当做条件判断数据库中是否存在数据,来实现登录的。不要读出所有数据一一匹配,个人建议。
      

  14.   

    正确,或者在While后面再一句,return false;或return true;