c# - SqlCommand SQL statement being executed twice -
c# - SqlCommand SQL statement being executed twice -
i'm creating asp.net app using c# , every time seek sql insert using sqlcommand, command executed twice. implemented in post (sql insert query executed twice), still doesn't help.
here code:
protected void btn_add_click(object sender, eventargs e) { error.innerhtml = " "; sqlconnection myconn = new sqlconnection("data source=.\\sqlexpress;" + "initial catalog=simon;" + "persist security info=true;" + "user id=username;password=password"); string sqlstatement = @"insert [network_equipment] " + "([network_equipment_name], [network_equipment_type_id], [ip_address], [fqdn], [netbios_name], [building_id], [description])" + " values " + "(@network_equipment_name, @network_equipment_type_id, @ip_address, @fqdn, @netbios_name, @building_id, @description)"; sqlcommand sqlcmd = new sqlcommand(sqlstatement, myconn); sqlcmd.connection = myconn; sqlcmd.parameters.addwithvalue("@network_equipment_name", ((in_add_equipment.value == null) ? (object)dbnull.value : (object)in_add_equipment.value)); sqlcmd.parameters.addwithvalue("@network_equipment_type_id", ((ddl_equipment_type.selectedvalue == null) ? (object)dbnull.value : (object)ddl_equipment_type.selectedvalue)); sqlcmd.parameters.addwithvalue("@ip_address", ((in_ip_address.value == null) ? (object)dbnull.value : (object)in_ip_address.value)); sqlcmd.parameters.addwithvalue("@fqdn", ((in_fqdn.value == null) ? (object)dbnull.value : (object)in_fqdn.value)); sqlcmd.parameters.addwithvalue("@netbios_name", ((in_netbios.value == null) ? (object)dbnull.value : (object)in_netbios.value)); sqlcmd.parameters.addwithvalue("@building_id", ((ddl_building.selectedvalue == null) ? (object)dbnull.value : (object)ddl_building.selectedvalue)); sqlcmd.parameters.addwithvalue("@description", ((ta_description.value == null) ? (object)dbnull.value : (object)ta_description.value)); seek { myconn.open(); } grab (exception er) { error.innerhtml = er.tostring(); return; } seek { sqlcmd.executenonquery(); } grab (exception er) { error.innerhtml = er.tostring(); return; } error.innerhtml = "successfully added: " + in_add_equipment.value; sqlcmd.dispose(); sqlcmd = null; myconn.close(); in_add_equipment.value = ""; ddl_equipment_type.selectedindex = 0; in_ip_address.value = ""; in_fqdn.value = ""; in_netbios.value = ""; ddl_building.selectedindex = 0; ta_description.value = ""; gridview1.databind(); }
i've run in debug mode , sqlcmd.executenonquery() called once, yet have 2 identical entries beingness added.
what missing and/or doing wrong?
-------- edit --------
i used modified version of john ryan's code, still added twice.
here code goes this:
equipment.aspx
<%@ page language="c#" autoeventwireup="true" codebehind="equipment.aspx.cs" inherits="simon.equipment" %> <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script language="javascript" type="text/javascript"> // disables button specified , sets style disabled "look". function disablebuttononclick(obutton, sbuttontext, scssclass) { obutton.disabled = true; // set button disabled can't click on it. obutton.value = sbuttontext; // alter text of button. obutton.setattribute('classname', scssclass); // ie uses classname css property. obutton.setattribute('class', scssclass); // firefox, safari utilize class css property. (doesn't wound both). } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:scriptmanager id="scriptmanager1" runat="server"></asp:scriptmanager> <asp:updatepanel id="updatepanel2" runat="server"> <contenttemplate> <div id="error" runat="server"> </div> <div> <fieldset style="display: inline-block; padding: 5px; -moz-border-radius: 8px; border-radius: 8px;"> <legend style="text-decoration: underline;">[ add together equipment ]</legend> <table cellpadding="3px" style="border: 0px; width: 300px;"> <tbody> <tr> <td style="text-align: left;"><label for="in_add_equipment" style="white-space: nowrap;">equipment name</label></td> <td style="text-align: right;"><input id="in_add_equipment" runat="server" size="20" /></td> <td style="text-align: left;"><label for="ddl_equipment_type" style="white-space: nowrap;">equipment type</label></td> <td style="text-align: right;"> <asp:dropdownlist id="ddl_equipment_type" runat="server" datasourceid="network_equipment_type" datatextfield="network_equipment_type" datavaluefield="network_equipment_type_id"></asp:dropdownlist> </td> </tr> <tr> <td style="text-align: left;"><label for="in_ip_address" style="white-space: nowrap;">ip address</label></td> <td style="text-align: right;"><input id="in_ip_address" runat="server" size="20" class="onlynumeric" /></td> <td style="text-align: left;"><label for="in_fqdn" title="fully qualified domain name" style="white-space: nowrap;">fqdn</label></td> <td style="text-align: right;"><input id="in_fqdn" runat="server" size="20" /></td> </tr> <tr> <td style="text-align: left;"><label for="in_netbios" style="white-space: nowrap;">netbios name</label></td> <td style="text-align: right;"><input id="in_netbios" runat="server" size="20" /></td> <td style="text-align: left;"><label for="ddl_building" style="white-space: nowrap;">building</label></td> <td style="text-align: right;"> <asp:dropdownlist id="ddl_building" runat="server" datasourceid="buildings" datatextfield="building_name" datavaluefield="building_id"></asp:dropdownlist> </td> </tr> <tr> <td style="text-align: left;"><label for="ta_description" style="white-space: nowrap;">description</label></td> <td style="text-align: right;" colspan="3"><textarea id="ta_description" runat="server" cols="50" rows="3"></textarea></td> </tr> <tr> <td colspan="4" style="text-align: right;"> <asp:button id="btn_add" text="add" cssclass="ui-state-default ui-corner-all" runat="server" onclick="btn_add_click" /> </td> </tr> </tbody> </table> </fieldset> </div> </contenttemplate> </asp:updatepanel> <p></p> <fieldset style="display: inline-block; padding: 5px; -moz-border-radius: 8px; border-radius: 8px;"> <legend style="text-decoration: underline;">[ update equipment ]</legend> <asp:updatepanel id="updatepanel1" runat="server"> <contenttemplate> <asp:sqldatasource id="sqldatasource1" runat="server" connectionstring="<%$ connectionstrings:simon-writer %>" deletecommand="delete [network_equipment] [network_equipment_id] = @original_network_equipment_id" oldvaluesparameterformatstring="original_{0}" selectcommand="select * [all_network_equipment-simplified]" updatecommand="update [network_equipment] set [network_equipment_name] = @network_equipment_name, [network_equipment_type_id] = @network_equipment_type_id, [ip_address] = @ip_address, [fqdn] = @fqdn, [netbios_name] = @netbios_name, [building_id] = @building_id, [description] = @description, [active] = @active [network_equipment_id] = @original_network_equipment_id"> <deleteparameters> <asp:parameter name="original_network_equipment_id" type="int32" /> </deleteparameters> <insertparameters> <asp:parameter name="network_equipment_name" type="string" /> <asp:parameter name="network_equipment_type_id" type="int32" /> <asp:parameter name="ip_address" type="string" /> <asp:parameter name="fqdn" type="string" /> <asp:parameter name="netbios_name" type="string" /> <asp:parameter name="building_id" type="int32" /> <asp:parameter name="description" type="string" /> <asp:parameter name="active" type="boolean" /> </insertparameters> <updateparameters> <asp:parameter name="network_equipment_name" type="string" /> <asp:parameter name="network_equipment_type_id" type="int32" /> <asp:parameter name="ip_address" type="string" /> <asp:parameter name="fqdn" type="string" /> <asp:parameter name="netbios_name" type="string" /> <asp:parameter name="building_id" type="int32" /> <asp:parameter name="description" type="string" /> <asp:parameter name="active" type="boolean" /> <asp:parameter name="original_network_equipment_id" type="int32" /> </updateparameters> </asp:sqldatasource> <asp:gridview id="gridview1" runat="server" autogeneratecolumns="false" cellpadding="4" datakeynames="network_equipment_id" datasourceid="sqldatasource1" forecolor="#333333" gridlines="none"> <alternatingrowstyle backcolor="white" forecolor="#284775" /> <columns> <asp:boundfield datafield="network_equipment_id" headertext="network_equipment_id" readonly="true" sortexpression="network_equipment_id" visible="false" /> <asp:boundfield datafield="network_equipment_name" headertext="equipment name" sortexpression="network_equipment_name" /> <asp:boundfield datafield="network_equipment_type_id" headertext="network_equipment_type_id" sortexpression="network_equipment_type_id" visible="false" /> <asp:templatefield headertext="equipment type" sortexpression="network_equipment_type"> <edititemtemplate> <asp:dropdownlist id="dropdownlist1" runat="server" datasourceid="network_equipment_type" datatextfield="network_equipment_type" datavaluefield="network_equipment_type_id" selectedvalue='<%# bind("network_equipment_type_id") %>'> </asp:dropdownlist> </edititemtemplate> <itemtemplate> <asp:label id="label1" runat="server" text='<%# bind("network_equipment_type") %>'></asp:label> </itemtemplate> </asp:templatefield> <asp:boundfield datafield="ip_address" headertext="ip address" sortexpression="ip_address" /> <asp:boundfield datafield="fqdn" headertext="fqdn" sortexpression="fqdn" /> <asp:boundfield datafield="netbios_name" headertext="netbios name" sortexpression="netbios_name" /> <asp:boundfield datafield="building_id" headertext="building_id" sortexpression="building_id" visible="false" /> <asp:templatefield headertext="building" sortexpression="building_name"> <edititemtemplate> <asp:dropdownlist id="dropdownlist2" runat="server" datasourceid="buildings" datatextfield="building_name" datavaluefield="building_id" selectedvalue='<%# bind("building_id") %>'> </asp:dropdownlist> </edititemtemplate> <itemtemplate> <asp:label id="label2" runat="server" text='<%# bind("building_name") %>'></asp:label> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="description" sortexpression="description"> <edititemtemplate> <asp:textbox id="textbox3" runat="server" height="50px" text='<%# bind("description") %>' textmode="multiline" width="200px"></asp:textbox> </edititemtemplate> <itemtemplate> <asp:label id="label3" runat="server" text='<%# bind("description") %>'></asp:label> </itemtemplate> </asp:templatefield> <asp:checkboxfield datafield="active" headertext="active" sortexpression="active" /> <asp:commandfield showeditbutton="true" /> </columns> <editrowstyle backcolor="#999999" /> <footerstyle backcolor="#5d7b9d" font-bold="true" forecolor="white" /> <headerstyle backcolor="#5d7b9d" font-bold="true" forecolor="white" /> <pagerstyle backcolor="#284775" forecolor="white" horizontalalign="center" /> <rowstyle backcolor="#f7f6f3" forecolor="#333333" /> <selectedrowstyle backcolor="#e2ded6" font-bold="true" forecolor="#333333" /> <sortedascendingcellstyle backcolor="#e9e7e2" /> <sortedascendingheaderstyle backcolor="#506c8c" /> <sorteddescendingcellstyle backcolor="#fffdf8" /> <sorteddescendingheaderstyle backcolor="#6f8dae" /> </asp:gridview> <br /> <asp:sqldatasource id="network_equipment_type" runat="server" connectionstring="<%$ connectionstrings:simon-reader %>" selectcommand="select distinct * [network_equipment_types] order [network_equipment_type]"> </asp:sqldatasource> <asp:sqldatasource id="buildings" runat="server" connectionstring="<%$ connectionstrings:simon-reader %>" selectcommand="select distinct * [buildings] order [building_name]"> </asp:sqldatasource> </contenttemplate> </asp:updatepanel> </fieldset> </div> </form> </body> </html>
equipment.aspx.cs
namespace simon { public partial class equipment : system.web.ui.page { protected void page_load(object sender, eventargs e) { if (!page.ispostback) { postbackoptions optionssubmit = new postbackoptions(btn_add); btn_add.onclientclick = "disablebuttononclick(this, 'please wait...', 'disabled_button ui-corner-all'); "; btn_add.onclientclick += clientscript.getpostbackeventreference(optionssubmit); } } protected void btn_add_click(object sender, eventargs e) { error.innerhtml = " "; sqltransaction trans = null; using (sqlconnection myconn = new sqlconnection(configurationmanager.connectionstrings["simon-writer"].tostring())) { myconn.open(); trans = myconn.begintransaction(); string sqlstatement = "insert [network_equipment] " + "([network_equipment_name], [network_equipment_type_id], [ip_address], [fqdn], [netbios_name], [building_id], [description])" + " values " + "(@network_equipment_name, @network_equipment_type_id, @ip_address, @fqdn, @netbios_name, @building_id, @description)"; using (sqlcommand sqlcmd = new sqlcommand(sqlstatement, myconn)) { sqlcmd.transaction = trans; sqlcmd.connection = myconn; sqlcmd.parameters.addwithvalue("@network_equipment_name", ((in_add_equipment.value == null) ? (object)dbnull.value : (object)in_add_equipment.value)); sqlcmd.parameters.addwithvalue("@network_equipment_type_id", ((ddl_equipment_type.selectedvalue == null) ? (object)dbnull.value : (object)ddl_equipment_type.selectedvalue)); sqlcmd.parameters.addwithvalue("@ip_address", ((in_ip_address.value == null) ? (object)dbnull.value : (object)in_ip_address.value)); sqlcmd.parameters.addwithvalue("@fqdn", ((in_fqdn.value == null) ? (object)dbnull.value : (object)in_fqdn.value)); sqlcmd.parameters.addwithvalue("@netbios_name", ((in_netbios.value == null) ? (object)dbnull.value : (object)in_netbios.value)); sqlcmd.parameters.addwithvalue("@building_id", ((ddl_building.selectedvalue == null) ? (object)dbnull.value : (object)ddl_building.selectedvalue)); sqlcmd.parameters.addwithvalue("@description", ((ta_description.value == null) ? (object)dbnull.value : (object)ta_description.value)); seek { sqlcmd.executenonquery(); trans.commit(); } grab (exception er) { error.innerhtml = er.tostring(); return; } { myconn.close(); error.innerhtml = "successfully added: " + in_add_equipment.value; in_add_equipment.value = ""; ddl_equipment_type.selectedindex = 0; in_ip_address.value = ""; in_fqdn.value = ""; in_netbios.value = ""; ddl_building.selectedindex = 0; ta_description.value = ""; gridview1.databind(); } } } } } }
-------- edit 2 --------
i've tracked downwards causing "double execute" of sql statement piece of code:
protected void page_load(object sender, eventargs e) { if (!page.ispostback) { postbackoptions optionssubmit = new postbackoptions(btn_add); btn_add.onclientclick = "disablebuttononclick(this, 'please wait...', 'disabled_button ui-corner-all'); "; btn_add.onclientclick = clientscript.getpostbackeventreference(optionssubmit); } }
when comment out, info added once, button not disabled want. disabling button prevent people beingness impatient , clicking button multiple times.
suggestions?
here's get:
aspx
<asp:button id="btn_add" text="add" cssclass="ui-state-default ui-corner-all" runat="server" onclick="btn_add_click" />
aspx.cs
if (!page.ispostback) { postbackoptions optionssubmit = new postbackoptions(btn_add); btn_add.onclientclick = "disablebuttononclick(this, 'please wait...', 'disabled_button ui-corner-all'); "; btn_add.onclientclick += clientscript.getpostbackeventreference(optionssubmit); }
executes sql statement twice
aspx
<asp:button id="btn_add" text="add" cssclass="ui-state-default ui-corner-all" runat="server" />
aspx.cs
if (!page.ispostback) { postbackoptions optionssubmit = new postbackoptions(btn_add); btn_add.onclientclick = "disablebuttononclick(this, 'please wait...', 'disabled_button ui-corner-all'); "; btn_add.onclientclick += clientscript.getpostbackeventreference(optionssubmit); }
briefly changes "btn_add" in desired way, doesn't execute code "btn_add_click"
aspx
<asp:button id="btn_add" text="add" cssclass="ui-state-default ui-corner-all" runat="server" onclick="btn_add_click" />
aspx.cs
if (!page.ispostback) { postbackoptions optionssubmit = new postbackoptions(btn_add); btn_add.onclientclick = "disablebuttononclick(this, 'please wait...', 'disabled_button ui-corner-all'); "; //btn_add.onclientclick += clientscript.getpostbackeventreference(optionssubmit); }
changes "btn_add" in desired way, doesn't execute code "btn_add_click", , refreshes entire page equipment.aspx (normally equipment.aspx loaded div through jquery .load event)
few suggestions...
connection strings should stored in web.config file.
it's okay grab exceptions each step along way, utilize "using" statement. way don't have worry disposing object.
i recommend adding global.asax file project , catching application errors there. in grab statements, place line , "throw new exception.." , pass in specific exception it's easy grab , can describe error. recommend setting logging application block in enterprise library 5.0. having global "error" variable have not idea. if want larn enterprise library, download enterprise library labs examples.
i wouldn't utilize sql commands straight in code. utilize stored procedures, , have sql done on sql server itself. if have variables, set parameters on stored procedure. stored procedures more secure web application. might thought utilize database application block microsoft's enterprise library. have of code you, , need import library , know commands. handle of performance implications you.
if using using statement, no need close connection or dispose. removed in code.
if throw exception, no need "return" statement... next block stack grab statement (or statement) qualifies grab it. if there none, application_error in global.asax grab it. create sure handle unhandled exceptions in .net framework. microsoft broke .net 3.5 unhandled exception architecture, either have add together own module, or switch 1.1 architecture. or upgrade .net 4.0.
actual question: click event might getting called twice because you're using handler , have "onclick" event attribute specified in <asp:button source tag. in vb, word "handles" follow event method. in c#, handler in different spot, different types of syntax. called twice if have both there.
here's #1: <asp:button id="btn_add" text="add" cssclass="ui-state-default ui-corner-all" runat="server" onclick="btn_add_click" />
here's #2: btn_add.onclientclick = clientscript.getpostbackeventreference(optionssubmit);
if have onclick attribute on control, don't need add together special code (like onclientclick or event handlers). utilize 1 or other.
good luck you!
error.innerhtml = " "; string connstr = "data source=.\\sqlexpress;" + "initial catalog=simon;" + "persist security info=true;" + "user id=username;password=password"); using (sqlconnection myconn = new sqlconnection(connstr)) { string sqlstatement = @"insert [network_equipment] " + "([network_equipment_name], [network_equipment_type_id], [ip_address], [fqdn], [netbios_name], [building_id], [description])" + " values " + "(@network_equipment_name, @network_equipment_type_id, @ip_address, @fqdn, @netbios_name, @building_id, @description)"; sqlcommand sqlcmd = new sqlcommand(sqlstatement, myconn); sqlcmd.connection = myconn; sqlcmd.parameters.addwithvalue("@network_equipment_name", ((in_add_equipment.value == null) ? (object)dbnull.value : (object)in_add_equipment.value)); sqlcmd.parameters.addwithvalue("@network_equipment_type_id", ((ddl_equipment_type.selectedvalue == null) ? (object)dbnull.value : (object)ddl_equipment_type.selectedvalue)); sqlcmd.parameters.addwithvalue("@ip_address", ((in_ip_address.value == null) ? (object)dbnull.value : (object)in_ip_address.value)); sqlcmd.parameters.addwithvalue("@fqdn", ((in_fqdn.value == null) ? (object)dbnull.value : (object)in_fqdn.value)); sqlcmd.parameters.addwithvalue("@netbios_name", ((in_netbios.value == null) ? (object)dbnull.value : (object)in_netbios.value)); sqlcmd.parameters.addwithvalue("@building_id", ((ddl_building.selectedvalue == null) ? (object)dbnull.value : (object)ddl_building.selectedvalue)); sqlcmd.parameters.addwithvalue("@description", ((ta_description.value == null) ? (object)dbnull.value : (object)ta_description.value)); seek { myconn.open(); } grab (exception er) { //error.innerhtml = er.tostring(); //return; throw new exception(... } seek { sqlcmd.executenonquery(); } grab (exception er) { //error.innerhtml = er.tostring(); //return; throw new exception(... } error.innerhtml = "successfully added: " + in_add_equipment.value; } in_add_equipment.value = ""; ddl_equipment_type.selectedindex = 0; in_ip_address.value = ""; in_fqdn.value = ""; in_netbios.value = ""; ddl_building.selectedindex = 0; ta_description.value = ""; gridview1.databind();
c# sql sql-server-2008
Comments
Post a Comment