Click here to Skip to main content
15,890,390 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my code
C#
protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
           DataTable dtMax = gen.GetDataTable("SPSelectDCNMForMaxRecord");
           if (dtMax.Rows.Count > 0)
           {
               string strMaxNo = (Convert.ToInt32(dtMax.Rows[0]["maxRecord"].ToString()) + 1).ToString();
               lblDCNO.Text = strMaxNo;
           }
           else
           {
               lblDCNO.Text = "00000000001";
           }
           txtDate.Text =DateTime.Now.ToString();
           fillCustomerName();

           BindGrid();
       }
Posted
Updated 2-Jul-13 5:32am
v3
Comments
Mahesh Bailwal 2-Jul-13 11:23am    
Please elaborate more?
Sampath Kumar Sathiya 2-Jul-13 11:26am    
Database or your datatable?
hardikshah011 2-Jul-13 11:26am    
datatable
hardikshah011 2-Jul-13 11:27am    
datatable get null value still the condition true..else part not work...
Mahesh Bailwal 2-Jul-13 11:47am    
Did you debug it?

Check for a null:
C#
DataTable dtMax = gen.GetDataTable("SPSelectDCNMForMaxRecord");
if (dtMax != null && dtMax.Rows.Count > 0)
The order is important: if you don't check for null first, you will get an exception as you have been.
 
Share this answer
 
Hi
please check null value before checking the row count.
if(dtMax != null)

Thanks
Anil
 
Share this answer
 
You may also want to check the row value isn't null

// checks the DataTable isn't null, the maxRecord valus isn't null and DataTable has rows

if (dtMax != null && dtMax.Rows.Count > 0 && dtMax.Rows[0]["maxRecord"] != null)
 
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