簡體   English   中英

如何從Webreference調用CreateUser方法

[英]How to call the CreateUser method from a webreference

我已經嘗試不了所有東西了,所以我希望有人可以幫助我。

我正在創建“注冊”頁面。 當用戶單擊“注冊”按鈕時,其信息(電子郵件,用戶名,密碼)需要存儲到Access數據庫中。

我創建了一個Web參考( http:// localhost:36938 / usconWebServices / membershipService.asmx ),並嘗試在register.aspx.cs中調用“ CreateUser”功能,但似乎無法正確實現。

這是register.aspx.cs的內容:

protected void btnRegister_Click(object sender, EventArgs e)
{
    wrefMembership.membershipService ws = new wrefMembership.membershipService();

    if (ws.CreateUser(txtEmailReg.Text, txtUsernameReg, txtPasswordReg))
    {

        try
        {
            Response.Redirect("mainContent.aspx");
        }
        catch (Exception er)
        {
            Response.Write("<b>Something really bad happened... Please try again.</b>");
        }
        finally
        {
            Response.Redirect("~/membersOnly/mainContent.aspx");
        }
    }
}

以“ if ...”開頭的第三行給我一個錯誤:“方法'CreateUser'的結束不接受3個爭論。” 我試過取出參數以及整行,但仍然沒有用。

這是我對membershipService.cs的要求:

[WebMethod(Description = "Create a new user, pass on a user object, returns a result string with information about processing result")]
public string CreateUser(user newUser, string emailAddress, string username, string userPassword)
{
    string query = "";
    DataSet ds = new DataSet();
    DataRow dr;
    int count;
    string result = "";

    try
    {
        //define query
        query = "SELECT * FROM UserInfo WHERE emailAddress='" + newUser.emailAddress + "'";
        ds = DataAccessService.RunSimpleQuery(query);

        if (ds.Tables[0].Rows.Count <= 0)
        {
            dr = ds.Tables[0].NewRow();
            dr["emailAddress"] = newUser.emailAddress;
            dr["username"] = newUser.username;
            dr["userPassword"] = userPassword;
            dr["registerDate"] = DateTime.Now;
            ds.Tables[0].Rows.Add(dr);
            result = DataAccessService.RunInsertCommand(query, ds);

            try
            {
                ds = DataAccessService.RunSimpleQuery(query);
                count = ds.Tables[0].Rows.Count; //last row is the new row!
                if (count > 0)
                {
                    dr = ds.Tables[0].Rows[count - 1];
                    result = "[000] OK: userID=" + dr["userID"].ToString();
                }
                else
                {
                    result = "[004] ERROR: User ID not found"; //user ID not found
                }
            }
            catch (Exception ex)
            {
                result = "ERROR: Could not update database: " + ex.Message + " *** ";
            }
        }
        else
        {
            result = "[003] ERROR: This e-mail account has already been registered with us."; //e-mail account already exists
        }
    }
    catch (Exception ex)
    {
        result = "[002] ERROR: " + query + Environment.NewLine + ex.Message + ex.StackTrace; //error
    }

    return result;
}

任何幫助或建議對此將不勝感激!

CreateUser()函數需要四個參數(用戶newUser,字符串emailAddress,字符串用戶名,字符串userPassword)。 但是,當您調用該方法時,僅傳遞了三個參數。

更改

if (ws.CreateUser(txtEmailReg.Text, txtUsernameReg, txtPasswordReg))

傳遞所有四個參數。

user newUser=new Users();
if (ws.CreateUser(newUser,txtEmailReg.Text, txtUsernameReg.Text, txtPasswordReg.Text))

顯然,您缺少第一個參數,即用戶類的對象。

或更改您的功能定義

public string CreateUser(user newUser, string emailAddress, string username, string userPassword)

public string CreateUser(string emailAddress, string username, string userPassword)

僅接受三個參數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM