Solved

loop on each SQL parameter and apply a format

Posted on 2012-03-16
3
425 Views
Last Modified: 2012-03-19
hello

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)
 end
next

thanks
0
Comment
Question by:Graham_Forbes
3 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 37730137
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
Next

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.
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37730607
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;
    
    try
    {
        connection = CreateConnection();

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

        // 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.
                                             InputOutput)
            {
                DBType paramType=parameter.DbType;
                ProcessType(paramType);
                //do your processing in processtype method.
                index++;
            }
        }
    }
    catch
    {
        if (connection != null && connection.State
                                == ConnectionState.Open)
            connection.Close();
        throw;
    }
}

Open in new window

0
 

Author Closing Comment

by:Graham_Forbes
ID: 37737099
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
                Try
                    p.Value() = FormatCurrency(p.Value(), 2)
                Catch

                End Try
            End If
        Next

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

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

thanks.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now