Link to home
Start Free TrialLog in
Avatar of Mahdi Sedaqat
Mahdi SedaqatFlag for Qatar

asked on

C# Print Gridview Error:Object reference not set to an instance of an object

i am trying to print a form which has textboxes and data gridveiw  using Printdocument control, the code below will print the text boxes and first cell of grid view , for the rest of the cells i am getting following  error :
An unhandled exception of type 'System.NullReferenceException' occurred in System.Windows.Forms.dll
Object reference not set to an instance of an object.

 
 private void btnPrint_Click(object sender, EventArgs e)
        {
            printPreviewDialog1.Document = printDocument1;
            printPreviewDialog1.ShowDialog();
        }
e.Graphics.DrawString("Date: "+DateTime.Now.ToShortDateString(),new Font("Arial",12,FontStyle.Bold),Brushes.Black,new Point(20,95));
            e.Graphics.DrawString("Report Number" + txtReportNumber.Text, new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(20, 150));
            e.Graphics.DrawString(@"------------------------------------------------------------------------------------------------------------------------------------------------------",
            new Font("Arial",12,FontStyle.Bold),Brushes.Black,new Point(20,170));
            e.Graphics.DrawString("SN", new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(65, 185));
            e.Graphics.DrawString("Reference", new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(180, 185));
            e.Graphics.DrawString("Amount", new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(480, 185));
            e.Graphics.DrawString("FOP", new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(650, 185));
            e.Graphics.DrawString(@"------------------------------------------------------------------------------------------------------------------------------------------------------",
            new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(20, 195));
            int ypos = 220;
            for(int i=0;i<ReportGridView.Rows.Count;i++)
            {
                e.Graphics.DrawString(ReportGridView.Rows[i].Cells[1].Value.ToString(),new Font("Arial",12,FontStyle.Regular),Brushes.Black,new Point(65,ypos));
                e.Graphics.DrawString(ReportGridView.Rows[i].Cells[2].Value.ToString(),new Font("Arial",12,FontStyle.Regular),Brushes.Black,new Point(180,ypos));
                e.Graphics.DrawString(ReportGridView.Rows[i].Cells[3].Value.ToString(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(480, ypos));
                e.Graphics.DrawString(ReportGridView.Rows[i].Cells[4].Value.ToString(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(650, ypos));
                ypos += 40;
            }

Open in new window

Avatar of sarabande
sarabande
Flag of Luxembourg image

ReportGridView.Rows[ i ].Cells[4]

Open in new window


i would assume that the Cells array has a zero-based index same as the Rows array. if so, ReportGridView.Rows[ i ].Cells[4] might be out of boundaries and references a NULL by accident.

so you may try to use

Cells[0], ..., Cells[3] but not Cells[4]

Sara
Avatar of Mahdi Sedaqat

ASKER

thanks for reply but i have 5 Cells and i want to print from 1 to 4 only,
Avatar of romanm
romanm

Do all the cells you are trying to print have a value? this does look like an empty cell has a null value.
Try to isolate the exception by breaking the code so you can debug it.

instead of
e.Graphics.DrawString(ReportGridView.Rows[i].Cells[1].Value.ToString(),new Font("Arial",12,FontStyle.Regular),Brushes.Black,new Point(65,ypos));

Open in new window

do
var row = ReportGridView.Rows[i];
if (null != row) {
  var cell = row.Cells[1];
  if (null != cell) {
    if (null != cell.Value) {
      e.Graphics.DrawString(cell.Value.ToString(),new Font("Arial",12,FontStyle.Regular),Brushes.Black,new Point(65,ypos));
    }
    else {
      throw new Exception("no cell value for cell #1 on row " + i);
    }
  }
  else {
    throw new Exception("no cell #1 on row " + i);
  }
}
else {
  throw new Exception("no row on i="+i);
}

Open in new window

Repeat this for the other cells you are printing, and now your exception tells you where it failed and why.
all the cells hava values , and I tried the code as well but still, I am getting same error
In that case extend the test code to catch if row.Cells is null, and lets pull this code piece into a function to clear out the loop.

add a new function, GetCellValue
private string GetCellValue(int i, int cell_n)
{
    var row = ReportGridView.Rows[i];
    if (null != row)
    {
        if (null != row.Cells)
        {
            var cell = row.Cells[cell_n];
            if (null != cell)
            {
                if (null != cell.Value)
                {
                    return cell.Value.ToString();
                }
                else
                {
                    throw new Exception("no cell value for cell #"+ cell_n + " on row " + i);
                }
            }
            else
            {
                throw new Exception("no cell #1 on row " + i);
            }
        }
        else
        {
            throw new Exception("no Cells in row " + i);
        }
    }
    else
    {
        throw new Exception("no row on i=" + i);
    }
}

Open in new window

And replace the way you access the value with
e.Graphics.DrawString(GetCellValue(i,1),new Font("Arial",12,FontStyle.Regular),Brushes.Black,new Point(65,ypos));

Open in new window


With all that it should get you a better handle on where the code fails.

Also I personally would replace the whole throw exception nonsense out the window and write the function to return an empty string if no value can be accessed.
Like this:
private string GetCellValue(int i, int cell_n)
{
    var row = ReportGridView.Rows[i];
    if (null != row)
    {
        if (null != row.Cells)
        {
            var cell = row.Cells[cell_n];
            if (null != cell)
            {
                if (null != cell.Value)
                {
                    return cell.Value.ToString();
                }
            }
        }
    }
    return string.Empty;
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Mahdi Sedaqat
Mahdi Sedaqat
Flag of Qatar image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial