I am trying to prevent a duplicate data entry into the entity database when inserting into the database. Currently heres my code for inserting into database. I have check many post online but not really sure which one to use as it seems really different. I am trying to prevent duplicate entry for PatientIc and display an error message for it. Thanks in advance!
protected void myGridview_RowCommand(object sender, GridViewCommandEventArgs e)
{
//Insert new Prescription
if (e.CommandName == "Insert")
{
Page.Validate("Add");
if (Page.IsValid)
{
var fRow = myGridview.FooterRow;
TextBox txtPatientName = (TextBox)fRow.FindControl("txtPatientName");
TextBox txtPatientIC = (TextBox)fRow.FindControl("txtPatientIc");
TextBox txtQuantity = (TextBox)fRow.FindControl("txtQuantity");
DropDownList ddType = (DropDownList)fRow.FindControl("ddType");
DropDownList ddState = (DropDownList)fRow.FindControl("ddState");
using (CareGiverEntities dc = new CareGiverEntities())
{
dc.Contacts.Add(new Contact
{
PatientName = txtPatientName.Text.Trim(),
PatientIc= txtPatientIC.Text.Trim(),
Quantity = txtQuantity.Text.Trim(),
MedicineTypeID = Convert.ToInt32(ddType.SelectedValue),
MedicineID = Convert.ToInt32(ddState.SelectedValue),
DatePrecribed= DateTime.Now
});
dc.SaveChanges();
PopulateContacts();
}
}
}
}
Codes for Grid view
DataKeyNames="ContactID,MedicineTypeID,MedicineID" CellPadding="10" CellSpacing="0"
ShowFooter="true"
CssClass="myGrid" HeaderStyle-CssClass="header" RowStyle-CssClass="trow1"
AlternatingRowStyle-CssClass="trow2" OnRowCommand="myGridview_RowCommand" OnRowCancelingEdit="myGridview_RowCancelingEdit" OnRowDeleting="myGridview_RowDeleting" OnRowEditing="myGridview_RowEditing" OnRowUpdating="myGridview_RowUpdating" AllowPaging="True" AllowSorting="True" >
Display="Dynamic" ValidationGroup="edit" ControlToValidate="txtPatientName">Required
ForeColor="Red" Display="Dynamic" ValidationGroup="Add" ControlToValidate="txtPatientName">Required
Display="Dynamic" ValidationGroup="edit" ControlToValidate="txtPatientIc">Required
ForeColor="Red" Display="Dynamic" ValidationGroup="Add" ControlToValidate="txtPatientIC">Required
Display="Dynamic" ForeColor="Red" ValidationGroup="edit" ControlToValidate="txtQuantity">Required
ForeColor="Red" Display="Dynamic" ValidationGroup="Add" ControlToValidate="txtQuantity">Required
OnSelectedIndexChanged="ddCountry_SelectedIndexChanged">
ForeColor="Red" Display="Dynamic" ValidationGroup="edit" ControlToValidate="ddType" InitialValue="0">
Required
ForeColor="Red" Display="Dynamic" ValidationGroup="Add" ControlToValidate="ddType" InitialValue="0">Required
ForeColor="Red" Display="Dynamic" ValidationGroup="edit" ControlToValidate="ddState" InitialValue="0">
Required
ForeColor="Red" Display="Dynamic" ValidationGroup="Add" ControlToValidate="ddState"
InitialValue="0">Required
|
|
1 Reply
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Nitin SontakkePosted Feb 6, 2017, 2:58 AM