Solved

loop on each SQL parameter and apply a format

Posted on 2012-03-16
3
435 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

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

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: …
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

726 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