c# - For loop only writing to my mssql stored procedure once? -
i have loop collects data c# web form , writes each item finds database via stored procedure.
the problem i'm having it's writing database once. have stepped through code in visual studio , inserted test variables check data there , being captured, is. because stored procedure executing correctly first time know it's working.
so think problem might how i've got try catch in loop?
or possibly else entirely - fresh pair of eyes , point me in right direction!
protected void log_hd_number() { ////write results db. sqlconnection conn = new sqlconnection("data source=;initial catalog=;integrated security=true"); sqlcommand cmd = conn.createcommand(); sqldatareader reader; cmd.commandtype = commandtype.storedprocedure; cmd.commandtext = "insert_requested_hd"; dictionary<string, string> hdsize = new dictionary<string, string>(); hdsize.add("harddisksizedata1", harddisksizedata1.text); hdsize.add("harddisksizedata2", harddisksizedata2.text); int numberrequested = 2; (int = 1; <= numberrequested; i++) { cmd.parameters.addwithvalue("@hd_size", hdsize["harddisksizedata" + i]); cmd.parameters.addwithvalue("@number_requested", numberrequested); cmd.parameters.addwithvalue("@vm_id", 15); try { conn.open(); reader = cmd.executereader(); reader.close(); } catch (exception exc) { } { if (conn.state != connectionstate.closed) conn.close(); } } }
edit:
sp:
alter procedure [dbo].[insert_requested_hd] -- add parameters stored procedure here @hd_size nvarchar(150), @number_requested int, @vm_id int begin -- set nocount on added prevent result sets -- interfering select statements. set nocount on; -- insert statements procedure here insert dbo.hard_disk_size ( hd_size, number_requested, vm_id ) values ( @hd_size, @number_requested, @vm_id )
you keep adding parameters cmd in loop without ever clearing old ones. maybe that's issue.
also i'm not sure can open conn after has been closed. think have make new one.
Comments
Post a Comment