0

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
James Wilson
  • 5,074
  • 16
  • 63
  • 122

1 Answers1

1

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:

DistInfo distInfo = new DistInfo() {AnticipatedLaunchDate = "Some default value instead of null"}; 
int colOrdinal = reader.GetOrdinal( "GoLiveDate" ); 
if(!reader.IsDBNull(colOrdinal ) ) 
{ 
    distInfo.AnticipatedLaunchDate = reader.GetString(colOrdinal);
} 
return distInfo; 
Community
  • 1
  • 1
Steve
  • 213,761
  • 22
  • 232
  • 286
  • I see that you have added the contents of your record. As said GoLiveDate is null and GetString() returns DBNull.Value not null. – Steve Apr 20 '12 at 22:25
  • Even this isn't working. Altho I am able to get farther. When stepping through it it makes it to the !reader.IsDBNull line, but skips over the block completely. They original person setup the column as a VarChar(50) not sure if that makes a difference. It doesn't seem to want to recognize NULL as DbNULL. – James Wilson Apr 20 '12 at 22:33
  • That's right. If your field in the database contains NULL then IsDBNull return true and you should not enter in the block where you are using GetString. You can't use GetString(). You should set a default value for your AnticipatedLaunchDate. I will try to update my answer – Steve Apr 20 '12 at 22:36
  • I figured it out! Just forgot to include an else. Now it seems to be working, now to repeat this another 11 times. going to be a big method. – James Wilson Apr 20 '12 at 22:41