anil penumacha
anil penumacha

Reputation: 3

Why isn't my database logic throwing an exception when I enter data that already exists?

I have a small ASP.NET registration page linked to a database. If the user enters the username that already exists in the database, then it should display "user already exists", but it is not doing that:

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if(IsPostBack)
        {
            SqlConnection conn =new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
            conn.Open();

            string check = "Select Count(*) from Registration where UserName = '"+TextBoxUN.Text+"';";

            SqlCommand comm = new SqlCommand(check, conn);

            int temp = Convert.ToInt32(comm.ExecuteScalar().ToString());
            if (temp == 1)
            {
                Response.Write("User already exists!!");

            }
            conn.Close();
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        if (this.DropDownListCountry.SelectedValue == "-Select-" && this.DropDownListAge.SelectedValue == "-Select-")
        {
            Response.Write("Select Country and age!");
        }
        else if(this.DropDownListCountry.SelectedValue == "-Select-" && this.DropDownListAge.SelectedValue != "-Select-")
        {
            Response.Write("Select Country!");
        }
        else if (this.DropDownListCountry.SelectedValue != "-Select-" && this.DropDownListAge.SelectedValue == "-Select-")
        {
            Response.Write("Select Age!");
        }
        else
        {
            try
            {
                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
                conn.Open();
                string insertQ = "insert into Registration(UserName,Email,Password,Country,Age) values ('" + TextBoxUN.Text + "','" + TextBoxEmail.Text + "','" + TextBoxPass.Text + "','" + DropDownListCountry.SelectedItem.ToString() + "','" + DropDownListAge.SelectedItem.ToString() + "');";
                SqlCommand comm = new SqlCommand(insertQ, conn);
                comm.ExecuteNonQuery();
                Response.Redirect("Display.aspx");

                conn.Close();
            }
            catch(Exception ex)
            {
                Response.Write("Error : " + ex.ToString());
            }
        }
    }
}

Upvotes: 0

Views: 91

Answers (2)

sudhansu63
sudhansu63

Reputation: 6180

Few Things.

  1. You need to check this before inserting the data.
  2. You are not preventing entering the same data if the username still exists
  3. You can check top 1 instead of count.

    private bool IsUserExists()
    { 
      bool UserExists = false;
      SqlConnection conn =new    SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
            conn.Open();
    
            string check = "Select Count(*) from Registration where UserName = '"+TextBoxUN.Text+"';";
    
            SqlCommand comm = new SqlCommand(check, conn);
    
            int temp = Convert.ToInt32(comm.ExecuteScalar().ToString());
            if (temp >= 1)
            {
                UserExists = true;
                Response.Write("User already exists!!");
    
            }
            conn.Close();
      }
     return UserExists ;
      }
    

Check this before inserting the data.

  try
        {  
           if(UserExists())
             return;  //Skips further code when user exists.


            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
            conn.Open();
            string insertQ = "insert into Registration(UserName,Email,Password,Country,Age) values ('" + TextBoxUN.Text + "','" + TextBoxEmail.Text + "','" + TextBoxPass.Text + "','" + DropDownListCountry.SelectedItem.ToString() + "','" + DropDownListAge.SelectedItem.ToString() + "');";
            SqlCommand comm = new SqlCommand(insertQ, conn);
            comm.ExecuteNonQuery();
            Response.Redirect("Display.aspx");

            conn.Close();
        }
        catch(Exception ex)
        {
            Response.Write("Error : " + ex.ToString());
        }

Upvotes: 0

Learning
Learning

Reputation: 20001

I think you should try first

 If ( temp > 0)
    {
    }

also debug to see what is returned by the sql query

Upvotes: 1

Related Questions