I am using SQL Server 2000, VS 2003
I have Education table in which there is a field CGPA having float data type (null allowed) I retreive the data from SQL server using stroed proc and store it in SqlDataReader dr while reading if CGPA contains 0 then it raises an error that "Specified cast is not valid" other wise it does not raise any error.
while (dr.Read())
{
Education e = new Education();
e.EducationId = dr.GetInt32(0);
e.Country = dr.GetInt32(1);
e.InstitutionName = dr.GetString(2);
e.Grade = dr.GetString(3);
e.CGPA = dr.GetFloat(4); // ERROR HERE
e.Percentage = dr.GetFloat(5);
e.PassingYear = dr.GetString(6);
}
where as in Education CGPA is also the float property can any one tell me how to read 0 value of float from SQL server
GPA can be handled with Decimal or Numric data type, there are some tasks like complex calculus that require Float data type because the T-SQL functions are in Float but student grade is simple Arithmetic so you can use Decimal instead of Float. The reason Float is very unstable, long time SQL Server users use it for calculations but convert the value to Decimal for storage because you can set precision and scale with Decimal. Hope this helps.|||
Avoid floats, reals, single, doubles if at all possible in the database. They are imprecise numbers and well... They cause all kinds of weird issues. It's not SQL Server issues, it's just issues with those data types in general.
That said, I don't see how it's causing your problem. If you know the column names of the record format, try:
e.CGPA=dr("CGPA")
That assumes the field/column is named CGPA of course.
Or try:
Dim o as object
o=dr("CGPA")
e.CGPA=o
that way you can see what "o" is before you try and convert it to whatever type e.CGPA is.
|||Another option is a simple ANSI ALTER TABLE to change FLOAT to DECIMAL. Run a search for ALTER TABLE in SQL Server BOL (books online). Hope this helps.
No comments:
Post a Comment