locked
Help Please RRS feed

  • Question

  • User-527908287 posted

    I am trying to set a command that will do the following things at the click of a button:

    The first is to check an already existed e-mail in the database, and then if the e-mail is not in the database, it should go ahead and save the new e-mail, else it should state that the “e-mail entered already exists”.

    In that same class, there should be a command to reject empty textboxes.

    I will be glad if I get answers to this problem I am having in my project work.

    Thank you.

    My Aspx.cs code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;
    
    public partial class Login : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security=True");
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    
        protected void Button2_Click(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand();
            con.Open();
            cmd.CommandText = "Select * from [Table] where mail=@email";
            cmd.Parameters.AddWithValue("@email", emtxt.Text);
            cmd.Connection = con;
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                Label9.Visible = true;
                Label9.Text = "e-mail alreay exists";
                Label9.ForeColor = System.Drawing.Color.Red;
            }
        
           else if (emtxt.Text != "" & signutxt.Text != "" & passtxt.Text != "")
            {
                string ins = "insert into [Table](mail, user_name, pass) values('" + emtxt.Text + "','" + signutxt.Text + "','" + passtxt.Text + "')";
                {
                    SqlCommand com = new SqlCommand(ins, con);
                    con.Open();
                    com.ExecuteNonQuery();
                    info.Text = "Sign Up Successful";
                    info.ForeColor = System.Drawing.Color.Green;
                    emtxt.Text = "";
                    signutxt.Text = "";
                    passtxt.Text = "";
                    // Response.Redirect("Register.aspx");
                }
                con.Close();
    
            }
            else
            {
                info.ForeColor = System.Drawing.Color.Red;
                info.Text = "*All Fields Are Required*";
                emtxt.Text = "";
                signutxt.Text = "";
                passtxt.Text = "";
            }
        }
    

    Thursday, March 28, 2019 3:18 AM

All replies

  • User-893317190 posted

    Hi Donald416,

    From your code , I think it has meet your require , if the email has existed, don't insert and show error message , if any of mail, user_name, pass is empty ,don't insert and show error message.

    What's your problem?

    If you want to simplify your code , you could try the code below.

    if (emtxt.Text != "" & signutxt.Text != "" & passtxt.Text != "")
            {using (SqlConnection con = new SqlConnection(constr))
                    {
                        using (SqlCommand com = new SqlCommand("if not exists ( select count(*) from [table] where mail=@mail ) begin insert into [Table](mail, user_name, pass) values(@newmail, @username,@pass) end", con))
                        {
    // add your parameters
     con.Open();
                          com.ExecuteNonQuery();
    // you could know whether the record is inserted through the returned value of com.ExecuteNonQuery
    // if is -1, no record is inserted
                        }
                    }
               }

    Best regards,

    Ackerly Xu       

    Thursday, March 28, 2019 7:24 AM