Here is my code:
DistInfo distInfo = new DistInfo
{
if (reader.GetString( reader.GetOrdinal( "GoLiveDate" ) ) == null ) {
AnticipatedLaunchDate = "";
}
else
{
AnticipatedLaunchDate = reader.GetString( reader.GetOrdinal( "GoLiveDate" ) );
}
};
Here is my class:
public class DistInfo
{
public string AnticipatedLaunchDate { get; set; }
}
It is saying AnticipatedLaunchDate does not exist in the current context. Is there a different way to assign it an empty string if it is null?
I built and ran the code and it errored out on line 254 which is this line:
DistInfo distInfo = new DistInfo
{
AnticipatedLaunchDate = reader.GetString( reader.GetOrdinal( "GoLiveDate" ) ) ?? ""
};
With the following error:
Invalid attempt to read when no data is present.
Here is the data that the SQL query returns:
DistID password GoLiveDate FirstBonusRun TechFName TechLName TechEmail TechPhone WebISPFName WebISPLName WebISPEmail WebISPPhone FullFillFName FullFillLName FullFillEmail FullFillPhone FName LName HomePhone Email
113 !12341 NULL NULL John Doe 1234@1234.com 8015555555 NULL NULL NULL NULL NULL NULL NULL NULL Jane Doe 8015555555 1234@1234.com
Your field GoLiveDate contains null values and null is not DBNull.Value returned by GetString,
You can’t use the ?? operator in this case. (See this question here)
Try with this code.
EDIT: