我在网络表单中有三个文本框,用户必须输入用户名,密码和IC号才能注册。如果输入的IC编号与数据库中任何记录的IC值相匹配,它将更新用户输入的用户名和密码到该记录的用户名字段和密码字段中,该字段具有匹配的IC。更新部分很好,但我想要做的是如果数据库中输入的nric的用户名和密码的记录被填满,我想在标签中有一个错误,说这个NRIC已经有一个用户名和密码。帮助
等等,如果我在NRIC框中输入S9583728F,并且想要注册一个帐户,但由于在我的表中S9583728F已经有一个用户名和密码,即settsser和ddddd,我希望它能给我,这个NRIC已经拥有用户名和密码。
错误
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Web.Security;
using System.Globalization;
using System.Text;
using System.Threading;
public partial class register : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnCreate_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sacpConnectionString"].ConnectionString))
{
try
{
SqlCommand cmd = new SqlCommand();
Guid guid;
guid = Guid.NewGuid();
string sql = @"UPDATE patient
SET
pUserName = @pUserName,
pPassword = @pPassword
WHERE pIC = @pIC";
cmd.Parameters.AddWithValue("@pIC", txtIC.Value);
cmd.Parameters.AddWithValue("@pUsername", txtUsername.Value);
cmd.Parameters.AddWithValue("@pPassword", txtPassword.Value);
cmd.Connection = con;
cmd.CommandText = sql;
con.Open();
cmd.ExecuteNonQuery();
cmd.CommandText = "SELECT patientID, pUsername, pPassword FROM patient WHERE pIC = @pIC;";
int id = (cmd.ExecuteScalar() != null) ? Convert.ToInt32(cmd.ExecuteScalar()) : 0;
if (id > 0)
{
Session.Add("ID", id);
Session.Add("Username", txtUsername.Value);
Session.Add("Password", txtPassword.Value);
FormsAuthentication.SetAuthCookie(txtUsername.Value, true);
Response.Redirect("registered.aspx");
}
else
{
lblErrorMessage.Text = "IC does not exist";
}
}
/*
catch (Exception)
{
lblErrorMessage.Text = "IC does not exist";
}
*/
finally
{
con.Close();
}
}
}
}
}
答案 0 :(得分:0)
只需更改UPDATE
查询,如下所示:
string sql = @"UPDATE patient
SET
pUserName = @pUserName,
pPassword = @pPassword
WHERE pIC = @pIC and pUserName='' and pPassword =''";
解决方案2:编写单独的函数,如果返回false则继续UPDATE,否则显示警告消息。
boolean IsIDExist()
{
SqlCommand cmd = new SqlCommand();
string sql = @"SELECT count(*) from patient
WHERE pIC = @pIC" and pUserName!='' and pPassword !=''";
cmd.Parameters.AddWithValue("@pIC", txtIC.Value);
cmd.Connection = con;
cmd.CommandText = sql;
con.Open();
if(Convert.ToInte32(cmd.ExecuteScalar())>0)
return true;
return false;
}
完整代码:
boolean IsIDExist()
{
SqlCommand cmd = new SqlCommand();
string sql = @"SELECT count(*) from patient
WHERE pIC = @pIC" and pUserName!='' and pPassword !=''";
cmd.Parameters.AddWithValue("@pIC", txtIC.Value);
cmd.Connection = con;
cmd.CommandText = sql;
con.Open();
if(Convert.ToInte32(cmd.ExecuteScalar())>0)
return true;
return false;
}
if (Page.IsValid)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sacpConnectionString"].ConnectionString))
{
try
{
SqlCommand cmd = new SqlCommand();
Guid guid;
guid = Guid.NewGuid();
string sql = @"UPDATE patient
SET
pUserName = @pUserName,
pPassword = @pPassword
WHERE pIC = @pIC and pUserName='' and pPassword=''";
cmd.Parameters.AddWithValue("@pIC", txtIC.Value);
cmd.Parameters.AddWithValue("@pUsername", txtUsername.Value);
cmd.Parameters.AddWithValue("@pPassword", txtPassword.Value);
cmd.Connection = con;
cmd.CommandText = sql;
con.Open();
cmd.ExecuteNonQuery();
if (!IsIDExist())
{
Session.Add("ID", id);
Session.Add("Username", txtUsername.Value);
Session.Add("Password", txtPassword.Value);
FormsAuthentication.SetAuthCookie(txtUsername.Value, true);
Response.Redirect("registered.aspx");
}
else
{
lblErrorMessage.Text = "IC Already Exist";
}
}
/*
catch (Exception)
{
lblErrorMessage.Text = "IC does not exist";
}
*/
finally
{
con.Close();
}
}
}