c# - How to handle "Violation of UNIQUE KEY constraint" exception in asp.net web application -
<alternatingrowstyle backcolor="#f7f7f7" horizontalalign="justify" wrap="false" /> <columns> <asp:commandfield showdeletebutton="true" buttontype="image" /> <asp:commandfield showeditbutton ="true" buttontype="image" /> <asp:templatefield headertext="name" sortexpression="name"> <edititemtemplate> <asp:textbox id="textbox1" runat="server" text='<%# bind("name") %>'></asp:textbox> </edititemtemplate> <itemtemplate> <asp:label id="label1" runat="server" text='<%# bind("name") %>'></asp:label> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="email" sortexpression="email"> <edititemtemplate> <asp:textbox id="textbox2" runat="server" text='<%# bind("email") %>'></asp:textbox> </edititemtemplate> <itemtemplate> <asp:label id="label2" runat="server" text='<%# bind("email") %>'></asp:label> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="occupation" sortexpression="occupation"> <edititemtemplate> <asp:textbox id="textbox3" runat="server" text='<%# bind("occupation") %>'></asp:textbox> </edititemtemplate> <itemtemplate> <asp:label id="label3" runat="server" text='<%# bind("occupation") %>'></asp:label> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="role" sortexpression="role"> <edititemtemplate> <asp:dropdownlist id="roles" runat="server" text='<%# bind("roles") %>'> <asp:listitem selected="true" text="user" value="user"></asp:listitem> <asp:listitem selected="false" text="admin" value="admin"></asp:listitem> </asp:dropdownlist> </edititemtemplate> <itemtemplate> <asp:label id="label7" runat="server" text='<%# bind("roles") %>'></asp:label> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="address" sortexpression="address"> <edititemtemplate> <asp:textbox id="textbox4" runat="server" text='<%# bind("address") %>'></asp:textbox> </edititemtemplate> <itemtemplate> <asp:label id="label4" runat="server" text='<%# bind("address") %>'></asp:label> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="mobile" sortexpression="mobile"> <edititemtemplate> <asp:textbox id="textbox5" runat="server" text='<%# bind("mobile") %>'></asp:textbox> </edititemtemplate> <itemtemplate> <asp:label id="label5" runat="server" text='<%# bind("mobile") %>'></asp:label> </itemtemplate> </asp:templatefield> </columns> <editrowstyle horizontalalign="center" wrap="false" /> <footerstyle backcolor="#b5c7de" forecolor="#4a3c8c" /> <headerstyle backcolor="#4a3c8c" font-bold="true" forecolor="#f7f7f7" /> <pagerstyle backcolor="#e7e7ff" forecolor="#4a3c8c" horizontalalign="right" /> <rowstyle backcolor="#e7e7ff" forecolor="#4a3c8c" /> <selectedrowstyle backcolor="#738a9c" font-bold="true" forecolor="#f7f7f7" /> <sortedascendingcellstyle backcolor="#f4f4fd" /> <sortedascendingheaderstyle backcolor="#5a4c9d" /> <sorteddescendingcellstyle backcolor="#d8d8f0" /> <sorteddescendingheaderstyle backcolor="#3e3277" /> </asp:gridview> <asp:sqldatasource id="sqldatasource1" runat="server" connectionstring="<%$ connectionstrings:ustravelconnectionstring %>" deletecommand="delete [users] [id] = @id" insertcommand="insert [users] ([name], [email], [occupation],[roles], [address], [mobile]) values (@name, @email, @occupation,@roles, @address, @mobile)" selectcommand="select [name], [email], [occupation],[roles], [address], [mobile], [id] [users]" updatecommand="update [users] set [name] = @name, [email] = @email, [occupation] = @occupation, [roles]=@roles, [address] = @address, [mobile] = @mobile [id] = @id"> <deleteparameters> <asp:parameter name="id" type="int32" /> </deleteparameters> <insertparameters> <asp:parameter name="name" type="string" /> <asp:parameter name="email" type="string" /> <asp:parameter name="occupation" type="string" /> <asp:parameter name="roles" type="string" /> <asp:parameter name="address" type="string" /> <asp:parameter name="mobile" type="string" /> </insertparameters> <updateparameters> <asp:parameter name="name" type="string" /> <asp:parameter name="email" type="string" /> <asp:parameter name="occupation" type="string" /> <asp:parameter name="roles" type="string" /> <asp:parameter name="address" type="string" /> <asp:parameter name="mobile" type="string" /> <asp:parameter name="id" type="int32" /> </updateparameters> </asp:sqldatasource> <br /> <p runat="server" id="norowsmsg" sytle="display:none"></p> </form>
email field has unique key constraint in tables. getting difficulty in handling exception , outputting user friendly message informs update new emailid
while updating email.
one possible way introduce validation between "insert click" , actual db insert action.
you can adding inserting event handler sql data source.
oninserting="sqldatasource_inserting"
once have done that, define handler as:
protected void sqldatasource_inserting(object sender, sqldatasourcecommandeventargs e) { var emailtobeinserter = e.command.parameters["@email"].value; // db lookup call , validate if unique. // if not unique, cancel insert , display error message. e.cancel = true; // if email exists in db. // if unique, nothing. }
doing explicit validation has cost of additional db calls, better alternative letting "insert" logic run through , throw "unique key" , other such exceptions.
Comments
Post a Comment