如何从 Web 引用调用 CreateUser 方法
我已经没有什么可以尝试的了,所以我希望有人可以帮助我。
我正在创建一个“注册”页面。当用户单击“注册”按钮时,他们的信息(电子邮件、用户名、密码)需要存储到 Access 数据库中。
我创建了一个网络引用( 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;
}
对此的任何帮助或建议将不胜感激!
I've ran out of things to try, so I hope someone can help me with this.
I'm creating a "Register" page. When a user clicks the "Register" button, their information (email, username, password) needs to be stored into an Access database.
I've created a web reference ( http://localhost:36938/usconWebServices/membershipService.asmx ) and trying to call the "CreateUser" function in the register.aspx.cs, but can't seem to get it right.
Here's what I have for 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");
}
}
}
The 3rd line starting with "if..." is giving me an error: "No over for method 'CreateUser' takes 3 arguements." I've tried taking out the parameters as well as the entire line, but still didn't work.
And here's what I have for 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;
}
Any help or advice on this will be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CreateUser() 函数需要四个参数(用户 newUser、字符串 emailAddress、字符串 username、字符串 userPassword)。但是当您调用该方法时,您只传递三个参数。
更改
为传递所有四个参数。
显然你缺少第一个参数,即用户类的对象。
或者将函数定义更改
为
仅接受三个参数
The CreateUser() function expects four parameters (user newUser, string emailAddress, string username, string userPassword). But when you are calling that method, you are passing only three parameters.
Change
to pass all the four parameters.
Obviously you are missing the first parameter, the object of user class.
Or change your function definition
to
which accepts only three parameters