2010-04-15 46 views
1

這裏是我的代碼,一切工作正常唯一的問題是與ReadData()方法,其中我想字符串值是增量即AM0001,AM0002,AM0003等這種情況一直髮生,直到執行程序一次我停止執行程序,第二次當我運行程序相同的值,即AM0001正在獲得回報。由於這個,我從oledb得到一個錯誤,因爲AM0001是一個主鍵字段。如何在運行時增加字符串值而不使用dr.read()並將其存儲在oledb數據庫中?

這是我的代碼:

class Jewellery : Connectionstr 
{ 

    string lmcode; 
    public string LM_code 
    { 
      get { return lmcode;} 
      set { lmcode = ReadData();} 
    } 

    string mname; 
    public string M_Name 
    { 
     get { return mname; } 
     set { mname = value;} 
    } 

    string desc; 
    public string Desc 
    { 
     get { return desc; } 
     set { desc = value; } 
    } 


    public string ReadData() 
    { 
     string jid = string.Empty; 
     string displayString = string.Empty; 
     String query = "select max(LM_code)from Master_Accounts"; 
     Datamanager.RunExecuteReader(Constr,query); 

      jid = LM_code;// this is working on first execution, the second time when i run the program the value null defined in LM_code. 
      if (string.IsNullOrEmpty(jid)) 
      { 
       jid = "AM0000";//This string value has to increment at every time, but it is getting increment only one time. 
      } 
      int len = jid.Length; 
      string split = jid.Substring(2, len - 2); 
      int num = Convert.ToInt32(split); 
      num++; 
      displayString = jid.Substring(0, 2) + num.ToString("0000"); 
      return displayString; 
    } 

    public void add() 
    { 


      String query ="insert into Master_Accounts values ('" + LM_code + "','" + M_Name + "','" + Desc + "')"; 
      Datamanager.RunExecuteNonQuery(Constr , query); 
    } 

任何幫助將不勝感激。

回答

1

不要在方法聲明JID,類級聲明它:

private string jid = string.Empty; 
相關問題