Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 632619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:05:47+00:00 2026-05-13T20:05:47+00:00

I have a stored procedure. One of its input parameters is expecting a char(8).

  • 0

I have a stored procedure. One of its input parameters is expecting a char(8). I try to convert a string “AAA” to this particular parameter type, which is a DBType.AnsiStringFixedLength.

object v = Convert.ChangeType("AAA", param.DbType.GetTypeCode()); 
// param is AnsiStringFixedLength

However, all I get is an exception: Input string was not in a correct format.
And the stack trace says: at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) […]

Why is System.Convert trying to convert a string into a number, even though the prodecure’s parameter is expecting a char(8)? How do I solve this? I don’t want to use one huge switch case mapping all SQL types to CLR types…

EDIT:
This is the code in question: (A generic method to call any MS SQL stored procedure)

using (SqlConnection conn = new SqlConnection(this.config.ConnectionString))
{
    using (SqlCommand cmd = conn.CreateCommand())
    {
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = this.config.StoredProcedureName;

        conn.Open();
        SqlCommandBuilder.DeriveParameters(cmd);

        foreach (SqlParameter param in cmd.Parameters)
        {
            if (param.Direction == ParameterDirection.Input ||
                 param.Direction == ParameterDirection.InputOutput)
            {
                try
                {
                    string rawParam = param.ParameterName.Replace("@", "");
                    if (this.config.Parameters.ContainsKey(rawParam))
                    {
                        try
                        {
                            param.Value = Convert.ChangeType(this.config.Parameters[rawParam],
                                param.DbType.GetTypeCode());
                        }
                        catch(Exception oops)
                        {
                            throw new Exception(string.Format("Could not convert to '{0}'.", param.DbType), oops);
                        }
                    }
                    else
                        throw new ArgumentException("parameter's not available");
                }
                catch (Exception e)
                {
                    throw;
                }
            }
        }  
        cmd.ExecuteNonQuery();
    }
}

The actual parameter values are provided by this.config.Parameters – all of them are strings. I iterate through SqlCommand’s parameter list and set them accordingly. Converting the string values to the parameter’s Sql type is necessary here, and as far as I can see, the Sql type is provided by param.DBType.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:05:47+00:00Added an answer on May 13, 2026 at 8:05 pm

    You seem to mix up some things here, or I don’t get what you try to do. The DbType (an enumeration) inherits Enum and that implements IConvertible -> You can call GetTypeCode(). But – you are now calling Enum.GetTypeCode(), which returns the underlying type. If you didn’t specify it (and DbType didn’t) any Enum is backed by an int.

    What are you trying to solve with the code anyway? Why would you want to change the type of a string if the parameter is a string (although with a fixed length)?


    Looking at the question some more it seems even more odd. You have an object v (probably for value?) – what do you care about the type?

    object v1 = "Foo";

    object v1 = 42;

    What is the difference for you? I guess you want to pass the values to something else, but – if you only reference the value as object you might still need to cast it.

    Please update your question and explain what you really want to do, what you expect to gain.


    Regarding the comment:

    I’m using Convert.ChangeType(object
    value, TypeCode typeCode), so it’s not
    really converting into an Enum/int. At
    least that’s what I thought…

    See above: DbType.GetTypeCode() is not what you want. Try it, give me the benefit of the doubt: What do you expect to get from DbType.AnsiStringFixedLength.GetTypeCode()? What is the actual result, if you try it?

    Now to your code: You try to set the SqlParameter.Value property to the “correct” type. Two things: According to the documentation you probably want to set the SqlParameter.SqlValue, which is the value using SQL types according to the docs. SqlParameter.Value, on the other hand, is the value using CLR types and allows to infer both DbType and SqlValue. Sidenote, implementation detail: The SqlParameter.SqlValue setter just calls the setter of SqlParameter.Value again…

    I would expect that the ADO.NET stuff converts the value on its own, if at all possible. What error are you getting without jumping through this hoops?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a crystal report that gets its data from one stored procedure, this
I have a mysql stored procedure from this ( google book ), and one
I have one package contains many Stored Procedure for populating diffrent tables. This package
I have created one stored procedure and its functioning well, what I am wanting
I have a stored procedure that uses several input parameters and insert them into
I have a stored procedure which executes another procedure inside it. The second one
I have a stored procedure that takes no parameters, and it returns two fields.
I have a stored procedure that logs some data, how can I call this
I'm using entity framework 4. I have a stored procedure that just updates one
Helo, My question is I have one Stored Procedure in SQL Server that returns

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.