<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<asp:Label ID="lblWarning" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td colspan="2" style="text-align:center;">
User Register
</td>
</tr>
<tr>
<td>Name : </td>
<td><input type="text" id="txtName" runat="server" /></td>
</tr>
<tr>
<td>Email Id : </td>
<td><input type="text" id="txtEmail" runat="server" /></td>
</tr>
<tr>
<td>Phone No : </td>
<td><input type="text" id="txtPhone" runat="server" /></td>
</tr>
<tr>
<td>
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
C# code
using System;
using System.Data.SqlClient;
using System.Configuration;
namespace Lab_1
{
public partial class user_register : System.Web.UI.Page
{
string sqlConStr = null;
SqlConnection conn = null;
SqlCommand cmd = null;
protected void Page_Load(object sender, EventArgs e)
{
sqlConStr = ConfigurationSettings.AppSettings["ConnectionString"];
conn = new SqlConnection(sqlConStr);
}
protected void btnSave_Click(object sender,EventArgs e)
{
if (checkEmail(txtEmail.Value.Trim()) < 7)
{
try
{
conn.Open();
string sql = "insert into tblUserRegister(name,email,phoneNo) values(@name,@email,@phoneNo)";
cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@name",txtName.Value);
cmd.Parameters.AddWithValue("@email", txtEmail.Value);
cmd.Parameters.AddWithValue("@phoneNo",txtPhone.Value);
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
lblWarning.Text = "User Register!";
txtName.Value = "";
txtEmail.Value = "";
txtPhone.Value = "";
}
}catch(Exception ex)
{
}
finally
{
conn.Close();
}
}
else
{
lblWarning.Text = "You are use "+txtEmail.Value+" email id to more than 7 times! Please try another one.";
}
}
private int checkEmail(string emailId)
{
try
{
conn.Open();
string sql = "select count(email) as countEmail from tblUserRegister where email='"+emailId+"'";
cmd = new SqlCommand(sql, conn);
SqlDataReader sRd = cmd.ExecuteReader();
if (sRd.HasRows)
{
sRd.Read();
return Int32.Parse(sRd["countEmail"].ToString());
}
}catch(Exception ex)
{
}
finally
{
conn.Close();
}
return 0;
}
}
}