【发布时间】:2015-07-13 09:25:26
【问题描述】:
我正在尝试将我的 sql 连接移动到类文件中的方法并返回一个值以检查数据库中是否存在用户名并在页面中显示为文本以向其他人显示该用户名存在。我应该如何修改我的 aspx .cs 页面以在 aspx 代码页中将错误显示为标签?
我在 aspx .cs 页面上的原始代码:
string connectionString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(connectionString))
{
con.Open();
bool exists = false;
// create a command to check if the username exists
using (SqlCommand cmd = new SqlCommand("select count(*) from Member where UserName = @UserName", con))
{
cmd.Parameters.AddWithValue("UserName", UNameTxtBox.Text);
exists = (int)cmd.ExecuteScalar() > 0;
}
// if exists, show a message error
if (exists)
ExistLabel.Text = "UserName exists";
我的类文件代码:
public static string searchusername(string username)
{
connectionString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(connectionString);
{
conn.Open();
bool exists = false;
using (SqlCommand comm = new SqlCommand("select count(*) from Member where UserName = @UserName", conn))
{
comm.Parameters.AddWithValue("@username", username);
exists = (int)comm.ExecuteScalar() > 0;
}
if (exists)
{
return "exist";
}
return null;
}
以及我的 aspx cs 文件中的代码:
MemberDB.searchusername(UNameTxtBox.Text);
【问题讨论】:
-
错误说明了什么?
-
没有错误,但是如果用户名存在于数据库中,我如何将存在传递回aspx页面并显示它?
-
你应该提供你的aspx/设计器页面代码,你想把它放在哪里显示..
-
我想在设计器页面中显示为标签。
标签: c# asp.net sql-server