Cannot implicity convert string type to decimal

Posted on 2008-11-06
Last Modified: 2013-12-17
I am trying to save a field to a table but get the following error message

Cannot implicity convert string type to decimal

I know it is because I am mishandling a variable before I write it to the database, but I am not sure how to tell it what I want to do.

All I want to do is read a field call documentid which is a decimal (9,4) from a table.  Then add .001 to the value and then save it back to the table.  I know it is because I am using the Tostring() command but I am not sure of the syntax to correctly conver the value.  Can someone point me in the right direction.

Here is my code:

decimal id;------------------------------------------------------------------------Variable is defined here
            string taxid;
            string userid;
            string username;

            SqlCommand oCmd;
            SqlConnection oConn;
            SqlDataReader reader = null;

            oConn = new SqlConnection();
            oConn.ConnectionString = ConfigurationManager.ConnectionStrings["12331ConnectionString"].ConnectionString;
            //use stored proc name here
            oCmd = new SqlCommand("ReadDRRIdCntr", oConn);
            oCmd.CommandType = CommandType.StoredProcedure;

            // Get the data
            reader = oCmd.ExecuteReader();

            if (reader.HasRows)
                id = reader["id"].ToString();-----------------------------------------Here is where I get the error when I attempt to add
                id = id + .0001;
                Session.Add("id", id);
Question by:kwh3856
    LVL 44

    Expert Comment

    change this line:
    id = reader["id"].ToString();

    id = reader["id"].GetDecimal()
    you are trying to assign a String value (reader["id"].ToString()) to a variable declared as decimal.
    LVL 53

    Expert Comment

    Try this:
    id = (decimal)reader["id"].ToString();
    LVL 53

    Accepted Solution

    Also possible:

    id = Convert.ToDecimal(reader["id"].ToString())
    id = Decimal.Parse(reader["id"].ToString())


    Author Closing Comment

    id = Convert.ToDecimal(reader["id"].ToString())

    awesome!!!! that did it.


    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    746 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

    19 Experts available now in Live!

    Get 1:1 Help Now