I have a for loop that collects the data from my C# web form and writes each item that it finds to a database via a stored procedure.
The problem I'm having is that it's writing to the database only once. I have stepped through the code in visual studio and inserted test variables to check that all the data is there and is being captured, which it is. Also because the stored procedure is executing correctly the first time I know that it's working.
So I think the problem might be with how I've got the try catch in the for loop?
Or possibly something else entirely - I could really do with a fresh pair of eyes and someone to point me in the right direction!
protected void log_hd_number()
{
////write results to 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;
for (int i = 1; i <= 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)
{
}
finally
{
if (conn.State != ConnectionState.Closed)
conn.Close();
}
}
}
EDIT:
SP:
ALTER PROCEDURE [dbo].[insert_requested_hd]
-- Add the parameters for the stored procedure here
@hd_size nvarchar(150),
@number_requested int,
@vm_id int
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
INSERT INTO dbo.hard_disk_size
(
hd_size,
number_requested,
vm_id
)
VALUES
(
@hd_size,
@number_requested,
@vm_id
)