Replacing 01/01/1900 from SQL resultset and exporting to Excel using C#

94 views Asked by At
                foreach (var item in bh)
        {
            worksheet.Cells[rowNumber, 1].Value = item.bill_number;
            worksheet.Cells[rowNumber, 2].Value = item.yearref + @"\" + item.monthref;
            worksheet.Cells[rowNumber, 3].Value = item.stdate.ToShortDateString();
            worksheet.Cells[rowNumber, 4].Value = item.enddate.ToShortDateString();
            worksheet.Cells[rowNumber, 5].Value = item.duration;
            worksheet.Cells[rowNumber, 6].Value = item.last_reading;
            worksheet.Cells[rowNumber, 7].Value = item.new_reading;
            worksheet.Cells[rowNumber, 8].Value = item.usage;
            worksheet.Cells[rowNumber, 9].Value = item.totaldueamt;
            worksheet.Cells[rowNumber, 10].Value = item.total_paid;
            worksheet.Cells[rowNumber, 11].Value = item.paid_date.ToShortDateString();     }

I have the above code that loops through an SQL resultset which is later used to export to Excel, when the item.paid_date is null, in the exported Excel sheet the date is shown as 01/01/1900, How can I replace item.paid_date with '' if date is null?

Here is the store procedure, at times there won't be any record in the reading table where where bg.bill_number = p.bill_number

CREATE PROCEDURE [dbo].[BillHistory] @accNo varchar(50) as begin select bg.Id, bg.account_number, bg.bill_number, bg.year, bg.month, bg.usagetotal, bg.fineamt, bg.fuelcharge, bg.othercharge, bg.totalamt, bg.totaldueamt, bg.billdate, bg.bill_due_date, bg.outstanding_amt, bg.[desc], readingId = re.Id, re.accountno, re.oldccountNo, re.new_reading, re.last_reading, re.meterno, re.yearref , re.monthref , re.referencecount, re.stdate, re.enddate, current_reading_date = re.created_on, pre_reading_date = isnull((select top 1 c.created_on from reading c where c.accountno = @accNo and c.Id < re.Id order by c.Id desc),CAST(-53690 as datetime)), re.duration, re.usage, re.averageusage, re.averageconsumption, isnull((select p.total_paid from payment p where bg.bill_number = p.bill_number ),0) as total_paid, isnull((select p.created_on from payment p where bg.bill_number = p.bill_number ),0) as paid_date from reading re join billing_gen bg on re.Id=bg.reading_id where re.accountno = @accNo and bg.enable=1 order by bg.Id asc; end;
1

There are 1 answers

0
Miroslav Bihari On

have you tried:

worksheet.Cells[rowNumber, 11].Value = item.paid_date == null ? string.Empty : item.paid_date.ToShortDateString(); 

?