Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 537
  • Last Modified:

Cannot implicity convert string type to decimal

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;
       
            oConn.Open();
            //use stored proc name here
            oCmd = new SqlCommand("ReadDRRIdCntr", oConn);
            oCmd.CommandType = CommandType.StoredProcedure;


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



            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);
            }
0
kwh3856
Asked:
kwh3856
  • 2
1 Solution
 
Arthur_WoodCommented:
change this line:
id = reader["id"].ToString();
to:

id = reader["id"].GetDecimal()
 
you are trying to assign a String value (reader["id"].ToString()) to a variable declared as decimal.
 
AW
0
 
DhaestCommented:
Try this:
id = (decimal)reader["id"].ToString();
0
 
DhaestCommented:
Also possible:

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

0
 
kwh3856Author Commented:
id = Convert.ToDecimal(reader["id"].ToString())

awesome!!!! that did it.

Thanks
Kenny
0

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now