Click here to Skip to main content
15,901,205 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
had written this code for next button.but the error shows in ds line "if (BILLNo == Convert.ToInt32(dt.Rows[0]["BillNo"].ToString())"


C#
private void pcNext_Click(object sender, EventArgs e)
{
   if (BILLNo == Convert.ToInt32(dt.Rows[0]["BillNo"].ToString()))
   {
       MessageBox.Show("Already at Last Page..!");
   }
   else
   {
       BILLNo++;
       ShowData();
   }
}

error- object reference not set to an instance of an object.

please help me...
Posted
Updated 23-Sep-12 23:58pm
v2

hi Shivani,
you are trying to read data from data table when its null... please check your data table
regards
sarva
 
Share this answer
 
First check if the column is present or not and if in-case its present the column value has to be checked with DBNull.Value and not Null.

C#
private void pcNext_Click(object sender, EventArgs e)
        {
            if (dt != null && dt.Rows.Count > 0 && dt.Rows.Contains("BillNo") && dt.Rows[0]["BillNo"] != DBNull.Value)
            {
            if (BILLNo == Convert.ToInt32(dt.Rows[0]["BillNo"].ToString()))
            {
                MessageBox.Show("Already at Last Page..!");
            }
            else
            {
                BILLNo++;
                ShowData();
            }
            }
        }
 
Share this answer
 
Before use a field from reader please check that field should not be null
C#
if (!Convert.IsDBNull(oReader["FEEDBACK_DATE"]))
{
     //your code
}
 
Share this answer
 
try this one...
C#
private void pcNext_Click(object sender, EventArgs e)
{
    if(dt.Rows[0]["BillNo"] != null){
        if (BILLNo == Convert.ToInt32(dt.Rows[0]["BillNo"].ToString()))
        {
            MessageBox.Show("Already at Last Page..!");
        }
        else
        {
            BILLNo++;
            ShowData();
        }
    }
}
 
Share this answer
 
Comments
Shivani Dash 24-Sep-12 6:10am    
thanks 4 help bt nt working.
Richard MacCutchan 24-Sep-12 6:17am    
You need to use your debugger to find out which object reference has not been set. This is basic C#, and you should understand it before writing compound expressions as above.
Debug and step through your source code.
Find the object that is null.
 
Share this answer
 
hello friend i thing don't convert .tostring...then it execute
 
Share this answer
 
just needed to keep the "BILLNo" in separate variable(bill) and then call that again there.. like,

private void pcNext_Click(object sender, EventArgs e)
{
if (BILLNo ==bill)
{
MessageBox.Show("Already at Last Page..!");
}
else
{
BILLNo++;
ShowData();
}
}
 
Share this answer
 
just needed to keep the "BILLNo" in separate variable(bill) and then call that again there.. like,

private void pcNext_Click(object sender, EventArgs e)
{
if (BILLNo ==bill)
{
MessageBox.Show("Already at Last Page..!");
}
else
{
BILLNo++;
ShowData();
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900