Dear friends,
My program are almost done but what I'm facing an issues is how to write a duplicate code?
It is to show an error message once user had key in the Email duplicate entry.
below are my code in C#:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace WebPortal
{
public partial class DataEntry : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString2"].ConnectionString;
string str;
SqlCommand com;
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = ("select * from staff where Username = ('" + Session["Username"] + "')");
com = new SqlCommand(str, con);
object obj = Session["Username"];
SqlDataReader reader = com.ExecuteReader();
reader.Read();
Label1.Text = reader["Username"].ToString();
}
protected void Button7_Click(object sender, EventArgs e)
{
string connectionString = "Data Source=.\\SQLEXPRESS;"
+ "AttachDbFilename=\"C:\\Users\\KLSIT\\Documents\\Visual Studio 2008\\Projects\\WebPortal\\WebPortal\\App_Data\\emaildata.mdf\";"
+ "Integrated Security=True;"
+ "Connect Timeout=30;"
+ "User Instance=True";
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connectionString);
System.Data.SqlClient.SqlCommand cmd = conn.CreateCommand();
{
String FullName;
String FirstName;
String LastName;
cmd.Parameters.Add("Type", SqlDbType.NVarChar);
cmd.Parameters["Type"].Value = this.DropDownList2.Text;
cmd.Parameters.Add("Title", SqlDbType.NVarChar);
cmd.Parameters["Title"].Value = this.DropDownList1.Text;
cmd.Parameters.Add("FirstName", SqlDbType.NVarChar);
cmd.Parameters["FirstName"].Value = this.TextBox1.Text;
cmd.Parameters.Add("LastName", SqlDbType.NVarChar);
cmd.Parameters["LastName"].Value = this.TextBox2.Text;
cmd.Parameters.Add("Fullname", SqlDbType.NVarChar);
cmd.Parameters["Fullname"].Value = this.TextBox1.Text + " " + this.TextBox2.Text;
cmd.Parameters.AddWithValue("Email", SqlDbType.NVarChar);
cmd.Parameters["Email"].Value = this.TextBox3.Text.Trim();
cmd.Parameters.Add("Job_Title", SqlDbType.NVarChar);
cmd.Parameters["Job_Title"].Value = this.TextBox4.Text;
cmd.Parameters.Add("Company", SqlDbType.NVarChar);
cmd.Parameters["Company"].Value = this.TextBox5.Text;
cmd.Parameters.Add("Mobile_Phone", SqlDbType.NVarChar);
cmd.Parameters["Mobile_Phone"].Value = this.TextBox6.Text;
cmd.Parameters.Add("Office_Phone", SqlDbType.NVarChar);
cmd.Parameters["Office_Phone"].Value = this.TextBox7.Text;
cmd.Parameters.Add("Staff", SqlDbType.NVarChar);
cmd.Parameters["Staff"].Value = this.Label1.Text;
FirstName = TextBox1.Text;
LastName = TextBox2.Text;
FullName = TextBox1.Text + " " + TextBox2.Text;
cmd.CommandText = "INSERT INTO [Customer] ([Type], [Title], [Firstname], [Lastname], [Fullname], [Company], [Email], [Mobile_Phone], [Office_Phone], [Job_Title], [Staff]) VALUES (@Type, @Title, @Firstname, @Lastname, @Fullname, @Company, @Email, @Mobile_Phone, @Office_Phone, @Job_Title, @Staff)";
try
{
conn.Open();
cmd.ExecuteNonQuery();
ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "alert('Record Saved Successfully.');window.location='DataEntry.aspx';", true);
}
catch (Exception ex)
{
Response.Write("Oops!! Following error occured: " + ex.Message.ToString());
}
finally
{
conn.Close();
cmd.Dispose();
}
}
}
Please advice, thank you very much