TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Xiu Han
NA
2
565
Prevent multiple duplicate data entry into entities database
Feb 6 2017 2:50 AM
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
<asp:GridView ID="myGridview" runat="server" AutoGenerateColumns="false"
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" >
<EmptyDataTemplate>No results found.</EmptyDataTemplate>
<Columns>
<asp:TemplateField SortExpression="FullName">
<HeaderTemplate>Patient Name</HeaderTemplate>
<ItemTemplate><%#Eval("PatientName") %></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtPatientName" runat="server" Text='<%#Bind("PatientName") %>' />
<asp:RequiredFieldValidator ID="rfCPEdit" runat="server" ForeColor="Red" ErrorMessage="*"
Display="Dynamic" ValidationGroup="edit" ControlToValidate="txtPatientName">Required</asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtPatientName" runat="server"></asp:TextBox><br />
<asp:RequiredFieldValidator ID="rfCP" runat="server" ErrorMessage="*"
ForeColor="Red" Display="Dynamic" ValidationGroup="Add" ControlToValidate="txtPatientName">Required</asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>Patient IC</HeaderTemplate>
<ItemTemplate><%#Eval("PatientIc") %></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtPatientIc" runat="server" Text='<%#Bind("PatientIc") %>' />
<asp:RequiredFieldValidator ID="rfCOEdit" runat="server" ForeColor="Red" ErrorMessage="*"
Display="Dynamic" ValidationGroup="edit" ControlToValidate="txtPatientIc">Required</asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtPatientIc" runat="server"></asp:TextBox><br />
<asp:RequiredFieldValidator ID="rfCO" runat="server" ErrorMessage="*"
ForeColor="Red" Display="Dynamic" ValidationGroup="Add" ControlToValidate="txtPatientIC">Required</asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>Quantity</HeaderTemplate>
<ItemTemplate><%#Eval("Quantity") %></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtQuantity" runat="server" Text='<%#Bind("Quantity") %>' />
<asp:RequiredFieldValidator ID="rfCNEdit" runat="server" ErrorMessage="*"
Display="Dynamic" ForeColor="Red" ValidationGroup="edit" ControlToValidate="txtQuantity">Required</asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtQuantity" runat="server"></asp:TextBox><br />
<asp:RequiredFieldValidator ID="rfCN" runat="server" ErrorMessage="*"
ForeColor="Red" Display="Dynamic" ValidationGroup="Add" ControlToValidate="txtQuantity">Required</asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>Medicine Type</HeaderTemplate>
<ItemTemplate><%#Eval("MedicineType") %></ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddType" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="ddCountry_SelectedIndexChanged">
<asp:ListItem Text="Select Medicine type" Value="0"></asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="rfCEdit" runat="server" ErrorMessage="*"
ForeColor="Red" Display="Dynamic" ValidationGroup="edit" ControlToValidate="ddType" InitialValue="0">
Required
</asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddType" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddCountry_SelectedIndexChanged">
<asp:ListItem Text="Select Medicine Type" Value="0"></asp:ListItem>
</asp:DropDownList>
<br />
<asp:RequiredFieldValidator ID="rfC" runat="server" ErrorMessage="*"
ForeColor="Red" Display="Dynamic" ValidationGroup="Add" ControlToValidate="ddType" InitialValue="0">Required</asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>Medicine Name</HeaderTemplate>
<ItemTemplate><%#Eval("MedicineName") %></ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddState" runat="server">
<asp:ListItem Text="Select Medicine" Value="0"></asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="rfSEdit" runat="server" ErrorMessage="*"
ForeColor="Red" Display="Dynamic" ValidationGroup="edit" ControlToValidate="ddState" InitialValue="0">
Required
</asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddState" runat="server">
<asp:ListItem Text="Select Medicine" Value="0"></asp:ListItem>
</asp:DropDownList><br />
<asp:RequiredFieldValidator ID="rfS" runat="server" ErrorMessage="*"
ForeColor="Red" Display="Dynamic" ValidationGroup="Add" ControlToValidate="ddState"
InitialValue="0">Required</asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lbEdit" runat="server" CommandName="Edit">Edit</asp:LinkButton>
|
<asp:LinkButton ID="lbDelete" runat="server" CommandName="Delete" OnClientClick="return confirm('Are you confirm?')">Delete</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lbUpdate" runat="server" CommandName="Update" ValidationGroup="edit">Update</asp:LinkButton>
|
<asp:LinkButton ID="lbCancel" runat="server" CommandName="Cancel">Cancel</asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="btnInsert" runat="server" Text="Prescribe" CommandName="Insert" ValidationGroup="Add" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Reply
Answers (
1
)
how to insert data using oracle data base in mvc4/5 ?
which method of Exception handling should i use ?