I see this:
?dataReader["Id"].GetType().FullName
"System.Guid"
and also this:
on record 2 the datareader will return ""
Those two things are incompatible. I suspect you really have a Guid
column and record 2 is returning NULL
, which in turn shows up as DBNull.Value
in the reader, and calling .ToString()
for the DBNull.Value
result then produces the empty string you observe here.
If that's true, you can do this:
while (dataReader.Read())
{
if (dataReader["Id"] != DBNull.Value)
{
d.id = (Guid)dataReader["Id"];
}
else
{
//get an empty string
}
}
Now the problem is that else
block. The question says this:
How can I add String Empty without the exception
The answer is: you can't. C# is a strongly-typed language, and we have already seen the d.id
property is a System.Guid
. You can't fit a string-shaped peg into a Guid-shaped hole. You have to leave this field empty, or define some default Guid value to mean the value is still empty, and then change code elsewhere to display an empty string rather than the Guid then the value matches that chosen default.