要在c#.net中将billId值初始化为Billid ='BID0' [英] To initialize the billId value as Billid='BID0' in c#.net

查看:174
本文介绍了要在c#.net中将billId值初始化为Billid ='BID0'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经从我的表中增加BillId Hbill.I调用increment()来增加我的账单,如BID1,BID2 .....下面的代码工作但是当输入第一条记录时,它显示错误,因为billid是null。我怎样才能将billId值初始化为Billid ='BID0'或者在启动时设置默认值的任何其他方式。



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.

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);
        }

推荐答案

代码中的小变化...试试这个...





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


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();
   }


现在我更改了上面的代码...现在检查
Now i changed the above code...check it now


这篇关于要在c#.net中将billId值初始化为Billid ='BID0'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆