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 = "";
}
}