问题
I'm creating a Bus Ticket booking system. I have created a database called traveller and two tables named Useriden and BusDB respectively. In the aspx.cs file(Sign Up page), I'm checking for duplicate usernames but it simply navigates to the next page
I've tried everything but I'm unable to resolve this issue. I'm not getting any errors or warnings.
protected void BtnConfirmSignup_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=(localdb)\\MSSQLlocalDB;Initial Catalog=traveller;Integrated Security=True;Pooling=False");
SqlCommand cmd;
SqlDataReader read;
/*DEBUG: Prevent duplicate usernames*/
try
{
Session["user"] = TxtUsrName.Text;
Session["pass"] = TxtPsswd.Text;
Session["email"] = TxtEmail.Text;
cmd = new SqlCommand("select Name from Useriden",con);
con.Open();
read = cmd.ExecuteReader();
while (read.Read()) {
if ((read["Name"].ToString()).Equals(TxtUsrName.Text))
{
throw new Exception("Invalid Username");
}
else
{
Response.Redirect("SignUpNext.aspx");
}
}
}
catch (Exception ex) {
LabelUserName.Visible = true;
LabelUserName.Text = ex.Message;
con.Close();
ViewState["Caption"]=ex.Message;
}
}
On clicking the confirm button, it should check for duplicate usernames. I already have one record with the name "Faf" in my table Useriden. When I try to sign up using the same username, it's not throwing an exception instead it navigates to SignUp.aspx.
The duplicate check is only valid if there's one record. I'll modify the logic so that it works for more than record but right now, it's not even working for a single record.
来源:https://stackoverflow.com/questions/58551657/ticket-booking-system-database-access-issue