i have 2 stored procedures (inserttocustomer) , (inserttorepair) need run under 1 button click event. when click button inserttorepair procedure works , stores in 'repair' table none of info customers gets stored in 'customer' table
below vb.net code this
protected sub button1_click(sender object, e eventargs) handles btnbookrepair.click 'declare variables dim bookedinby, devicetype, bookedindate, fname, lname, address, contactnum, altcontactnum, devicename, accessories, devicepassword, repairtype, technicalnotes, repairstatus, completednotes string dim trackingnum integer 'setting date bookedindate = txtbookedindate.text 'assigning variables bookedinby = ddlbookedinby.selecteditem.text devicetype = ddldevicetype.selecteditem.text trackingnum = txttrackingnumber.text fname = txtfname.text lname = txtlname.text address = txtaddress.text contactnum = txtcontactnum.text altcontactnum = txtaltcontactnum.text devicename = txtdevicename.text accessories = ddlaccessories.selecteditem.text devicepassword = txtdevicepassword.text repairtype = ddlrepairtype.selecteditem.text technicalnotes = txttechnical.text repairstatus = ddlrepairstatus.selecteditem.text completednotes = txtcompletednotes.text dim sqlcs string sqlcs = configurationmanager.connectionstrings("database").connectionstring try using sqlconn new sqlconnection(sqlcs) dim sqlcmd new sqlcommand sqlcmd.connection = sqlconn sqlcmd.commandtext = "inserttorepair" sqlcmd.commandtype = data.commandtype.storedprocedure sqlconn.open() sqlcmd.executenonquery() sqlcmd.parameters.addwithvalue("@tracking_number", trackingnum) sqlcmd.parameters.addwithvalue("@booked_in_by", bookedinby) sqlcmd.parameters.addwithvalue("@device_type", devicetype) sqlcmd.parameters.addwithvalue("@booked_in_date", bookedindate) sqlcmd.parameters.addwithvalue("@device_name", devicename) sqlcmd.parameters.addwithvalue("@accessories", accessories) sqlcmd.parameters.addwithvalue("@devicepassword", devicepassword) sqlcmd.parameters.addwithvalue("@repair_type", repairtype) sqlcmd.parameters.addwithvalue("@technical_notes", technicalnotes) sqlcmd.parameters.addwithvalue("@completed_notes", completednotes) sqlcmd.parameters.addwithvalue("@repairstatus", repairstatus) sqlcmd.executenonquery() sqlcmd.commandtext = "inserttocustomer" sqlcmd.commandtype = data.commandtype.storedprocedure sqlcmd.parameters.addwithvalue("@first_name", fname) sqlcmd.parameters.addwithvalue("@last_name", lname) sqlcmd.parameters.addwithvalue("@contactnumber", contactnum) sqlcmd.parameters.addwithvalue("@alternative_contact_number", altcontactnum) sqlcmd.parameters.addwithvalue("@customeraddress", address) sqlconn.close() end using catch ex exception statuslabel.text = ex.tostring end try end sub
three issues code see are:
- executing
sqlcmd.executenonquery()
prematurely - that's last operation performed after settingsqlcommand
object you'd do - re-use of
sqlcommand
object 2 different operations - not wrapping
sqlcommand
objects inusing
statement
edit:
as @andrew morton has commented, may meant re-use parameters 1 query next, in case 2 sqlcommand
using
statements should merged.
protected sub button1_click(sender object, e eventargs) handles btnbookrepair.click 'declare variables dim bookedinby, devicetype, bookedindate, fname, lname, address, contactnum, altcontactnum, devicename, accessories, devicepassword, repairtype, technicalnotes, repairstatus, completednotes string dim trackingnum integer 'setting date bookedindate = txtbookedindate.text 'assigning variables bookedinby = ddlbookedinby.selecteditem.text devicetype = ddldevicetype.selecteditem.text trackingnum = txttrackingnumber.text fname = txtfname.text lname = txtlname.text address = txtaddress.text contactnum = txtcontactnum.text altcontactnum = txtaltcontactnum.text devicename = txtdevicename.text accessories = ddlaccessories.selecteditem.text devicepassword = txtdevicepassword.text repairtype = ddlrepairtype.selecteditem.text technicalnotes = txttechnical.text repairstatus = ddlrepairstatus.selecteditem.text completednotes = txtcompletednotes.text dim sqlcs string sqlcs = configurationmanager.connectionstrings("database").connectionstring try using sqlconn new sqlconnection(sqlcs) sqlconn.open() using sqlcmd new sqlcommand sqlcmd.connection = sqlconn sqlcmd.commandtext = "inserttorepair" sqlcmd.commandtype = data.commandtype.storedprocedure sqlcmd.parameters.addwithvalue("@tracking_number", trackingnum) sqlcmd.parameters.addwithvalue("@booked_in_by", bookedinby) sqlcmd.parameters.addwithvalue("@device_type", devicetype) sqlcmd.parameters.addwithvalue("@booked_in_date", bookedindate) sqlcmd.parameters.addwithvalue("@device_name", devicename) sqlcmd.parameters.addwithvalue("@accessories", accessories) sqlcmd.parameters.addwithvalue("@devicepassword", devicepassword) sqlcmd.parameters.addwithvalue("@repair_type", repairtype) sqlcmd.parameters.addwithvalue("@technical_notes", technicalnotes) sqlcmd.parameters.addwithvalue("@completed_notes", completednotes) sqlcmd.parameters.addwithvalue("@repairstatus", repairstatus) sqlcmd.executenonquery() end using using sqlcmd new sqlcommand sqlcmd.connection = sqlconn sqlcmd.commandtext = "inserttocustomer" sqlcmd.commandtype = data.commandtype.storedprocedure sqlcmd.parameters.addwithvalue("@first_name", fname) sqlcmd.parameters.addwithvalue("@last_name", lname) sqlcmd.parameters.addwithvalue("@contactnumber", contactnum) sqlcmd.parameters.addwithvalue("@alternative_contact_number", altcontactnum) sqlcmd.parameters.addwithvalue("@customeraddress", address) sqlcmd.executenonquery() end using end using catch ex exception statuslabel.text = ex.tostring end try end sub
Comments
Post a Comment