Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As @Sparky suggests, the problem is that you continue to add parameters to the insertion command. There are several other ways in which the code could be improved, however. These improvements would remove the need to clear the parameters and would help to make sure you don't leave disposable resources undisposed.</p> <p>First - use the <code>using</code> statement for your disposable objects. This removes the need for the explicit Close (btw, only one of Close/Dispose is needed for the connection as I believe Dispose calls Close). Second, simply create a new command for each insertion. This will prevent complex logic around resetting the parameters and, possibly, handling error states for the command. Third, check the results of the insertion to make sure it succeeds. Fourth, explicitly catch a SqlException - you don't want to accidentally hide unexpected errors in your code. If it's necessary to make sure all exceptions don't bubble up, consider using multiple exception handlers and "doing the right thing" for each case - say logging with different error levels or categories, aborting the entire operation rather than just this insert, etc. Lastly, I would use better variable names. In particular, avoid appending numeric identifiers to generic variable names. This makes the code harder to understand, both for others and for yourself after you've let the code sit for awhile.</p> <p>Here's my version. Note there are several other things that I might do such as make the string literals into appropriately named constants. Introduce a strongly-typed wrapper around the ConfigurationManager object to make testing easier. Remove the underscores from the variable names and use camelCase instead. Though those are more stylistic in nature, you might want to consider them as well.</p> <pre><code>var connectionString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString; using (var newConnection = new SqlConnection(connectionString)) { newConnection.Open(); using (var selectCommand = newConnection.CreateCommand()) { selectCommand.CommandType = CommandType.Text; select.CommandText ="select request_no from dbo.Request_List where group_no = @groupNumber"; selectCommand.Parameters.AddWithValue("groupNumber", group_no); using (dataReader = (SqlDataReader)newCommand.ExecuteReader()) { while (reader.HasRows &amp;&amp; reader.Read()) { using (var insertCommand = newConnection.CreateCommand()) { insertCommand.CommandType = CommandType.StoredProcedure; insertCommand.CommandText = "Voucher_Request_Connection"; var request_no = (int)reader["request_no"]; insertCommand.Parameters.Add("@serial_no", serial_no); insertCommand.Parameters.Add("@request_no", request_no); try { if (insertCommand.ExecuteNonQuery() == 1) { MessageBox.Show("Connection Updated");//just to check the status.tempory } else { MessageBox.Show("Connection was not updated " + request_no); } } catch (SqlException xcep) { MessageBox.Show(xcep.Message); } MessageBox.Show(request_no.ToString());// } } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload