【问题标题】:'SaveUser(string)' :Not all code paths return a value'SaveUser(string)' :并非所有代码路径都返回值
【发布时间】:2017-08-13 23:48:56
【问题描述】:

请帮忙!这是我得到的最后一个需要修复的错误。“SaveUser”得到错误'clsDataLayer.SaveUser(string,string,string,string)':并非所有代码路径都返回值

// This function saves the user login
    public static bool SaveUser(string Database, string UserName, string UserPassword, string SecurityLevel)
    {
        bool recordSaved;

        try
        {
            // Add your comments here
            OleDbConnection conn = new OleDbConnection("PROVIDER=Microsoft.ACE.OLEDB.12.0;" +
            "Data Source=" + Database);

            conn.Open();
            OleDbCommand command = conn.CreateCommand();
            string strSQL;

            strSQL = "Insert into tblUserLogin (UserName, UserPassword, SecurityLevel) values ('" +
            UserName + "', '" + UserPassword + "', '" + SecurityLevel + "')";

            command.CommandType = CommandType.Text;
            command.CommandText = strSQL;

            command.ExecuteNonQuery();

            conn.Close();
            recordSaved = true;



        }
        catch (Exception ex) { }

}

这是这个过程的 if else

protected void btnAddUser_Click(object sender, EventArgs e)
    {
        if (clsDataLayer.SaveUser(Server.MapPath("PayrollSystem_DB.accdb"),
UserName.Text, UserPassword.Text, ddlSecurityLevel.SelectedValue))
        {

            lblDisplay.Text = "The user was successfully added!";
            grdUsers.DataBind();
        }

        else
            lblDisplay.Text = "The user was not added!";


    }

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    你没有返回任何东西。而不是

    recordSaved = true;
    

    你可以这样做:

    return true;
    

    此外,当您捕获异常时,您还需要从 catch 块中返回一些内容,例如

    catch (Exception ex) { return false; }
    

    【讨论】:

      猜你喜欢
      • 2016-06-01
      • 2011-12-17
      • 1970-01-01
      • 1970-01-01
      • 2021-11-08
      • 2013-10-06
      • 2016-02-14
      相关资源
      最近更新 更多