loop on each SQL parameter and apply a format


I have a aspx web page with around 300 textbox's which each map to a SQL parameter. rather than referencing each parameter to apply a format, I was hoping I could do something like:

for each singleparameter in cmdselect.parameters
  if singleparameter like '%.%' then
' it has a decimal place, must be a currency
      singleparameter = FormatCurrency(singleparameter,2)

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kaufmed 👽Commented:
You should be able to do that. Perhaps in this manner?

For Each p As SqlParameter In cmdselect.Parameters
    If p.Value IsNot Nothing AndAlso p.Value IsNot DBNull.Value AndAlso p.Value.ToString().Contains(".") Then
        p.Value = Convert.ToDecimal(p.Value).ToString("C")
    End If

Open in new window

Be sure to add error checking. The above could do with some--specifically near the call to Convert.ToDecimal. If you're sure none of your parameters are text strings which may have a period within them, then perhaps you'll be OK.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
here is a snippet that fetches parameters from db, iterates over the collection and checks DBType for each parameter.

Next you can easily write the processing logic on the basis of type of sql parameters

private void fetchparameters()
SqlConnection connection = null;
        connection = CreateConnection();

        // Create Command
        SqlCommand command = connection.CreateCommand();
        command.CommandType = CommandType.StoredProcedure;
        command.CommandText = storedProcedureName;
        // Open Connection
        // Discover Parameters for Stored Procedure
        // Populate command.Parameters Collection.
        // Causes Rountrip to Database.

        // Initialize Index of parameterValues Array
        int index = 0;
        // Populate the Input Parameters With Values Provided        
        foreach (SqlParameter parameter in command.Parameters)
            if (parameter.Direction == ParameterDirection.Input ||
                 parameter.Direction == ParameterDirection.
                DBType paramType=parameter.DbType;
                //do your processing in processtype method.
        if (connection != null && connection.State
                                == ConnectionState.Open)

Open in new window

Graham_ForbesAuthor Commented:
thanks for this, here is the actual code I used:
        For Each p As SqlParameter In cmdselect.Parameters
            If p.Value IsNot Nothing AndAlso p.DbType.ToString = "Currency" Then
                    p.Value() = FormatCurrency(p.Value(), 2)

                End Try
            End If

I had to use the TRY Catch because some values returned an error:

formatexception format string can be only "G" "g"

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.