I have a code that insert a data to DB and i want to show message after inserting data to DB And i want to avoid insert duplicate data to DB and show error message that data is duplicate.
this is my code for inserting data to DB:
<asp:SqlDataSource ID="sqlDtSrcEmployees" runat="server"
ConnectionString="<%$ ConnectionStrings:KDUIS-v1ConnectionString %>"
InsertCommand="INSERT INTO [IMS_Person] ([GuidId], [FirstName], [LastName], [FatherName], [NationalCode], [BirthDate], [CertificateNo], [BirthPlace], [BirthCertificatePlace], [Gender], [ReligionId], [MaritalId], [Mobile], [Email], [CreateDate], [Description])
VALUES (@GuidId, @FirstName, @LastName, @FatherName, @NationalCode, @BirthDate, @CertificateNo, @BirthPlace, @BirthCertificatePlace, @Gender, @ReligionId, @MaritalID, @Mobile, @Email, @CreateDate,@Description)"
OnInserting="sqlDtSrcEmployees_Inserting">
<InsertParameters>
<asp:Parameter Name="GuidId" />
<asp:ControlParameter ControlID="txtName" Name="FirstName" PropertyName="Text"
Type="String" />
<asp:ControlParameter ControlID="txtFamily" Name="LastName" PropertyName="Text"
Type="String" />
<asp:ControlParameter ControlID="txtFathername" Name="FatherName" PropertyName="Text"
Type="String" />
<asp:ControlParameter ControlID="txtNationalcode" Name="NationalCode" PropertyName="Text"
Type="String" />
<asp:Parameter Name="BirthDate" Type="DateTime" />
<asp:ControlParameter ControlID="txtCertificateNo" Name="CertificateNo" PropertyName="Text"
Type="String" />
<asp:ControlParameter ControlID="txtBirthPlace" Name="BirthPlace" PropertyName="Text"
Type="String" />
<asp:ControlParameter ControlID="txtBirthCertificatePlace" Name="BirthCertificatePlace" PropertyName="Text"
Type="String" />
<asp:Parameter Name="Gender" Type="Int32" />
<asp:Parameter Name="ReligionId" Type="Int32" />
<asp:Parameter Name="MaritalID" Type="Int32" />
<asp:ControlParameter ControlID="txtMobile" Name="Mobile" PropertyName="Text"
Type="String" />
<asp:ControlParameter ControlID="txtEmail" Name="Email" PropertyName="Text"
Type="String" />
<asp:Parameter Name="CreateDate" Type="DateTime" />
<asp:ControlParameter ControlID="txtDescription" Name="Description" PropertyName="Text"
Type="String" />
</InsertParameters>
</asp:SqlDataSource>
protected void sqlDtSrcEmployees_Inserting(object sender, SqlDataSourceCommandEventArgs e)
{
Person obj = new Person();
int genderid = obj.Gender(listGender.Text);
int ReligionId = obj.Religion(listReligion.Text);
int MaritalID = obj.Marital(listMaritial.Text);
try
{
int day = Convert.ToInt32(txtday.Text);
int month = Convert.ToInt32(txtmonth.Text);
int year = Convert.ToInt32(txtyear.Text);
System.Globalization.PersianCalendar faDate = new System.Globalization.PersianCalendar();
e.Command.Parameters["@Birthdate"].Value = faDate.ToDateTime(year, month, day, 23, 0, 0, 0);
e.Command.Parameters["GuidId"].Value = obj.GenerateGuid();
e.Command.Parameters["@genderid"].Value = obj.Gender(listGender.Text);
e.Command.Parameters["@ReligionId"].Value = obj.Religion(listReligion.Text);
e.Command.Parameters["@MaritalID"].Value = obj.Marital(listReligion.Text);
e.Command.Parameters["@CreateDateTime"].Value = obj.CreateDateTime();
}
catch { }
}
protected void Button1_Click(object sender, EventArgs e)
{
sqlDtSrcEmployees.Insert();
}
how can i do this correctly?