Link to home
Start Free TrialLog in
Avatar of kwh3856
kwh3856Flag for United States of America

asked on

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);
            }
Avatar of Arthur_Wood
Arthur_Wood
Flag of United States of America image

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
Avatar of Dirk Haest
Try this:
id = (decimal)reader["id"].ToString();
ASKER CERTIFIED SOLUTION
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kwh3856

ASKER

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

awesome!!!! that did it.

Thanks
Kenny