kwh3856
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;----------------------- ---------- ---------- ---------- ---------- ---------V ariable is defined here
string taxid;
string userid;
string username;
SqlCommand oCmd;
SqlConnection oConn;
SqlDataReader reader = null;
oConn = new SqlConnection();
oConn.ConnectionString = ConfigurationManager.Conne ctionStrin gs["12331C onnectionS tring"].Co nnectionSt ring;
oConn.Open();
//use stored proc name here
oCmd = new SqlCommand("ReadDRRIdCntr" , oConn);
oCmd.CommandType = CommandType.StoredProcedur e;
// Get the data
reader = oCmd.ExecuteReader();
reader.Read();
if (reader.HasRows)
{
id = reader["id"].ToString();-- ---------- ---------- ---------- ---------H ere is where I get the error when I attempt to add
id = id + .0001;
Session.Add("id", id);
}
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;-----------------------
string taxid;
string userid;
string username;
SqlCommand oCmd;
SqlConnection oConn;
SqlDataReader reader = null;
oConn = new SqlConnection();
oConn.ConnectionString = ConfigurationManager.Conne
oConn.Open();
//use stored proc name here
oCmd = new SqlCommand("ReadDRRIdCntr"
oCmd.CommandType = CommandType.StoredProcedur
// Get the data
reader = oCmd.ExecuteReader();
reader.Read();
if (reader.HasRows)
{
id = reader["id"].ToString();--
id = id + .0001;
Session.Add("id", id);
}
Try this:
id = (decimal)reader["id"].ToSt ring();
id = (decimal)reader["id"].ToSt
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
id = Convert.ToDecimal(reader[" id"].ToStr ing())
awesome!!!! that did it.
Thanks
Kenny
awesome!!!! that did it.
Thanks
Kenny
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