Solved

loop on each SQL parameter and apply a format

Posted on 2012-03-16
3
436 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 75

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

696 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