asp.net - Exception handling in C# and ASP .Net -
i have page , sql db behind sql table not allowed duplicate names addresses. connect db , trying implement try catch stamen working fore reason doesn't want show error message
html
<%@ page language="c#" autoeventwireup="true" codefile="admaddstreet.aspx.cs" inherits="admaddstreet" %> <!doctype html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> add new address<br /> <asp:textbox id="txbaddress" runat="server" height="22px" width="348px"></asp:textbox> <br /> <br /> <asp:button id="button1" runat="server" text="button" onclick="button1_click1" width="353px" /> <br /> <br /> <asp:label id="displaymessage" runat="server" style="color: #ff0000" visible="false" /> <asp:label id="displaymessage0" runat="server" style="color: #ff0000" visible="false" /> <br /> <asp:sqldatasource id="sqldatasource1" runat="server" connectionstring="<%$ connectionstrings:ticketsconnectionstring %>" deletecommand="delete [streets] [strid] = @original_strid" insertcommand="insert [streets] ([strname]) values (@strname)" oldvaluesparameterformatstring="original_{0}" selectcommand="select * [streets]" updatecommand="update [streets] set [strname] = @strname [strid] = @original_strid"> <deleteparameters> <asp:parameter name="original_strid" type="int32" /> </deleteparameters> <insertparameters> <asp:parameter name="strname" type="string" /> </insertparameters> <updateparameters> <asp:parameter name="strname" type="string" /> <asp:parameter name="original_strid" type="int32" /> </updateparameters> </asp:sqldatasource> <br /> <asp:gridview id="gridview1" runat="server" allowpaging="true" allowsorting="true" autogeneratecolumns="false" datakeynames="strid" datasourceid="sqldatasource1"> <columns> <asp:commandfield showdeletebutton="true" showeditbutton="true" /> <asp:boundfield datafield="strname" headertext="strname" sortexpression="strname" /> </columns> </asp:gridview> </div> </form> </body> </html>
and .aspx
using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; using system.data; using system.data.sqlclient; using system.configuration; public partial class admaddstreet : system.web.ui.page { protected void page_load(object sender, eventargs e) { } string connectionstring = configurationmanager.connectionstrings["ticketsconnectionstring"].connectionstring; protected void button1_click1(object sender, eventargs e) { try { dataset ds = new dataset(); sqlconnection con = new sqlconnection(connectionstring); sqlcommand cmd = new sqlcommand("insertintostreets", con); cmd.commandtype = commandtype.storedprocedure; cmd.parameters.add("@strname", sqldbtype.nvarchar).value = txbaddress.text; cmd.parameters["@strname"].value = txbaddress.text; con.open(); cmd.executenonquery(); con.close(); displaymessage.text = "record inserted."; displaymessage.visible = true; gridview1.databind(); txbaddress.text = string.empty; } catch { displaymessage0.text = "record exist."; displaymessage.visible = true; } } }
can please point me problem in try...catch, , maybe possible solution it?
well you're setting message you're never making visible:
displaymessage0.text = "record exist."; // different field ... displaymessage.visible = true;
what believe want is:
displaymessage0.text = "record exist."; displaymessage0.visible = true;
note: understood question code enters catch
Comments
Post a Comment