Ticket Booking System: Database access issue

痞子三分冷 提交于 2020-01-11 14:11:24

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!