Check if account and phone already exists in Database using C# in ASP.Net

Last Reply one month ago By dharmendr

Posted one month ago

This code supposed to verify if account and phone exist in database if yes throw message that the account and phone already in database, but the code only verifying account instead of account and phone

The could not work when i added two conditions Account_no and phone. It works with one condition but if more than one it does not work. It only checks with account_no while ignoring the phone number.

                                using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DB_"].ConnectionString))
                                {
                                    using (SqlCommand cmd = new SqlCommand("SELECT Account_No,Phone FROM Customer_Account WHERE Account_No = @Account_No AND Phone=@Phone ", con))
                                    {
                                        cmd.CommandType = CommandType.Text;
                                        // cmd.Parameters.AddWithValue("@Name", this.txtname.Text.Trim());
                                        cmd.Parameters.AddWithValue("@Account_No", this.txtaccountno.Text.Trim());

                                        cmd.Parameters.AddWithValue("@Phone", this.txtphone.Text.Trim());

                                        //  cmd.Parameters.AddWithValue("@Registered_Date", this.txtdate.Text.Trim());
                                        con.Open();
                                        string result = Convert.ToString(cmd.ExecuteScalar());
                                        con.Close();

                                        if (string.IsNullOrEmpty(result))
                                        {
                                            Insert(this.txtdate.Text.Trim(), this.ddllocation.SelectedItem.Text.Trim(), this.txtname.Text.Trim(), this.txtphone.Text.Trim(), this.txtemail.Text.Trim(), this.txtaddress.Text.Trim(), this.txtaccountno.Text.Trim(),this.ddlaccountype.SelectedItem.Text.Trim(), this.txtaccountofficer.Text.Trim(), this.txtverifiedby.Text.Trim(), this.ddlpaytype.SelectedItem.Text.Trim(), this.txtcharges.Text.Trim());
                                            // this.ddlitem.SelectedItem.Text = "";
                                            //this.txtqty.Text = "";
                                            // this.txtprice.Text = "";

                                            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Data Submitted Successfully ... !!')", true);
                                        }
                                        else
                                        {
                                            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Account Number Already Exists');", true);
                                        }
                                    }
                                }

 

Posted one month ago Modified on one month ago

Use below code. This code will check either Account or Phone exists or not. If any one exists then shows the message Account Number Already Exists.

using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DB_"].ConnectionString))
{
    using (SqlCommand cmd = new SqlCommand("SELECT COUNT(*) FROM Customer_Account WHERE Account_No = @Account_No OR Phone=@Phone", con))
    {
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddWithValue("@Account_No", this.txtaccountno.Text.Trim());
        cmd.Parameters.AddWithValue("@Phone", this.txtphone.Text.Trim());
        con.Open();
        int result = Convert.ToInt32(cmd.ExecuteScalar());
        con.Close();
        if (result == 0)
        {
            Insert(this.txtdate.Text.Trim(), this.ddllocation.SelectedItem.Text.Trim(), this.txtname.Text.Trim(), this.txtphone.Text.Trim(), this.txtemail.Text.Trim(), this.txtaddress.Text.Trim(), this.txtaccountno.Text.Trim(), this.ddlaccountype.SelectedItem.Text.Trim(), this.txtaccountofficer.Text.Trim(), this.txtverifiedby.Text.Trim(), this.ddlpaytype.SelectedItem.Text.Trim(), this.txtcharges.Text.Trim());
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Data Submitted Successfully ... !!')", true);
        }
        else
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Account Number Already Exists');", true);
        }
    }
}