Click here to Skip to main content
15,894,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i used to increment the BillId from my table Hbill.I called the increment() to increase my billid like BID1,BID2..... The below code is working but when entering the first record,its showing error since the billid is null.How can i initialize the billId value as Billid='BID0' or any other way to set the default value while starting.

C#
public void increment()
    {
        con.Open();           

        SqlCommand cmd = new SqlCommand("select Max(billid) from hbill ", con);
        SqlDataReader dr = cmd.ExecuteReader();

            while (dr.Read())
            {
                id = dr[0] as string;

                //department = reader[1] as string;

            }

            cur_id = IncrementID(id, 3);                       


    con.Close();
}


static string IncrementID(string startValue, int numNonDigits)
        {
            string nonDigits = startValue.Substring(0, numNonDigits);
            int len = startValue.Length - numNonDigits;
            int number = int.Parse(startValue.Substring(numNonDigits));
            number++;
            if (number >= Math.Pow(10, len)) number = 1; // start again at 1
            return String.Format("{0}{1:D" + len.ToString() + "}", nonDigits, number);
        }
Posted
Comments
Karthik_Mahalingam 23-Nov-13 9:10am    
a

Small Change in your code... Try this...


C#
SqlCommand cmd = new SqlCommand("select Max(billid) from hbill ", con);
        SqlDataReader dr = cmd.ExecuteReader();
          if(dr.HasRows)
          {  
            while (dr.Read())
            {
                id = dr[0] as string;
 
                //department = reader[1] as string;

            }
 
            cur_id = IncrementID(id, 3);                       
          }
          else
          {
             id='BID0'
           cur_id = IncrementID(id, 3);
          }
   
   if(!con.IsClosed)
   {
    con.Close();
   }
 
Share this answer
 
v2
Comments
rebo1 23-Nov-13 11:50am    
hi its showing following error "Object reference not set to an instance of an object" in this line : string nonDigits = startValue.Substring(0, numNonDigits);
Now i changed the above code...check it now
 
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