Reputation: 147
Hy, I'm getting this error on line _obj.Add(new data()... here is my code
SqlCommand com = new SqlCommand("sp_get_a", con)
{
CommandType = CommandType.StoredProcedure
};
_obj = new List<n>();
con.Open();
SqlDataReader rdr = com.ExecuteReader();
if (rdr.HasRows)
{
while (rdr.Read())
{
_obj.Add(new data() { Id = rdr.GetInt32(0), na = rdr.GetString(1), Al = rdr.GetString(2), Sc = rdr.GetFloat(3), So = rdr.GetInt32(4), Ta = rdr.GetInt32(5), Ai = rdr.GetInt32(6), Sh = rdr.GetInt32(7) });
}
}
else
{
throw new Exception("rdr don't have any rows");
}
con.Close();
my stored procedure
CREATE PROCEDURE sp_get_a
AS SELECT Id, Na, Al, Sc, So, Ta, Ai, Sh FROM x I don't have any double value and closest to double is Sc(float). so I tried Sc = Convert.ToSingle(rdr.GetFloat(3)). Where am I doing wrong ? Edit- My model
public class n
{
public int Id { get; set; }
public string Na { get; set; }
public string Al { get; set; }
public float Sc { get; set; }
public int So { get; set; }
public int Ta { get; set; }
public int Ai { get; set; }
public int Sh { get; set; }
}
Upvotes: 5
Views: 9690
Reputation: 1
You need to change the datatype of Sc
from float
to double
SQL Server real
is equivalent to C# Single
and SQL server float
is equivalent to C# Double
.
And you should consider @jdweng point also
Upvotes: 14