?
Solved

loop on each SQL parameter and apply a format

Posted on 2012-03-16
3
Medium Priority
?
455 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 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

839 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