当我输入已存在的数据时,为什么我的数据库逻辑不会抛出异常?

时间:2015-04-16 03:46:16

标签: asp.net database registration asp.net-webpages

我有一个链接到数据库的小型ASP.NET注册页面。如果用户输入数据库中已存在的用户名,则它应显示“用户已存在”,但它没有这样做:

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());
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

我认为你应该先尝试

 If ( temp > 0)
    {
    }

还调试以查看sql查询返回的内容

答案 1 :(得分:0)

少数事情。

  1. 您需要在插入数据前进行检查。
  2. 如果用户名仍然存在,则不会阻止输入相同的数据
  3. 您可以检查前1而不是计数。

    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 ;
      }
    
  4. 在插入数据之前检查一下。

      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());
            }