While storing 0.0 in a datatable it gets stored as 0 only
Have a query hope someone can help.
I have a datatable dt1 which has three columns a,b,c (of datatype string, float, float resp). Then I have a datatable dt2 which is a clone of dt1, and the values of dt1 are merged into dt2.
Now the issue is for a particular condition:
when the first row is this:
dt1.Rows.Add("xyz", 0.开发者_运维知识库0, 0.0);
But I can see the value is taken as xyz,0,0 in the datatable. This I am merging to dt2 as
if (dt2 == null)
{
dt2 = dt1.Clone();
}
dt2.Merge(dt1, true);
Now when I merge for the second time with say some vale in dt1 as ("mnp",4.5,8.9) I get an error at dt2.Merge(dt1, true);
Error: .b and .b have conflicting properties: DataType property mismatch.
where b is the column name.
Have defined the table dt1 as:
DataTable dt1 = new DataTable();
dt1.Columns.Add("a", typeof(string));
dt1.Columns.Add("b", typeof(float));
dt1.Columns.Add("c", typeof(float));`
Avi, the following Code runs with no errors in VS 2008 with Target Frameworks 2.0, 3.0 and 3.5:
using System.Data;
namespace DtCheck
{
class Program
{
static void Main()
{
DataTable dt1 = new DataTable();
dt1.Columns.Add("a", typeof(string));
dt1.Columns.Add("b", typeof(float));
dt1.Columns.Add("c", typeof(float));
dt1.Rows.Add("xyz", 0.0, 0.0);
DataTable dt2;
dt2 = dt1.Clone();
dt2.Merge(dt1, true);
dt1.Rows.Add("mnp", 4.5, 8.9);
dt2.Merge(dt1, true);
}
}
}
Can you try my program on your machine? What's the result? If it works, too, something in your program must be different.
Maybe you reassign dt2 to something else before the second merge? Or is it already set at the "if (dt2 == null)", so that the Clone() does not execute?
By the way: the first Clone/Merge can be reduced to dt2 = dt1.Copy()
How about if you postfix them with "f"??
("mnp",4.5f,8.9f)
I think maybe they're being interpreted by the compiler as double and not float.
精彩评论