简体   繁体   中英

Specified cast is not valid when converting from decimal(10,2) to double

I am retriving sum of all tution fees from the database where it is equal to the entered admission number. Data type for the columns in the SQL SERVER is decimal(10,2). When i want to convert to double it is showing error "Specified cast is not valid". How to convert to double?

SqlConnection con6 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
                    con6.Open();
                    string query = "SELECT sum(tutionfee) FROM fees where admno='"+admissionnumber.Text+"'";
                    SqlCommand comSelect = new SqlCommand(query, con6);
                    double ID = (double)comSelect.ExecuteScalar();

You need to actually convert the value in C#. double and decimal are two different types in C#. The former is a built-in type and the latter is just a struct that is implemented mostly in C# itself. Since they aren't the same, you can't just cast them. The easiest way, in this case, is to use the Convert class:

Convert.ToDouble((decimal)comSelect.ExecuteScalar());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM