2013-10-31 22 views
0

這是我一直在VB.net中做的事情,我試圖在C#中複製它。基本上我有一個數據集連接到sqlserver,我試圖添加一個新的行。我有以下,但rMainPID似乎爲空。在C#中使用數據集#

private components.db.PIDSDB.MainPIDDataTable dtMainPID; 
    private components.db.PIDSDBTableAdapters.MainPIDTableAdapter taMainPID = new components.db.PIDSDBTableAdapters.MainPIDTableAdapter(); 

然後主營:

protected void doSave(object sender, EventArgs e) 
     { 
      Int32 ClientID = Convert.ToInt32(txtCompanyClientID.Text); 
      String ChannelLead = Request.Form["ChannelLead"]; 
      // String ChannelSalesPerson = Request.Form["ChannelSalesPerson"]; 
      String ChannelSalesPerson = "Test"; 
      String DropDeadDate = Request.Form["DropDeadDate"]; 
      String MCSalesLead = Request.Form["MCSalesLead"]; 
      Int32 ProjectTypeID = Convert.ToInt32(Request.Form["ProjectTypeID"]); 
      Int32 ProjectStatusID = Convert.ToInt32(Request.Form["ProjectStatusID"]); 
      String ClientBriefing = Request.Form["ClientBriefing"]; 
      String ProjectRequirements = txtProjectRequirements.Text; 

      components.db.PIDSDB.MainPIDRow rMainPID = default(components.db.PIDSDB.MainPIDRow); 
      rMainPID = dtMainPID.NewMainPIDRow(); 
      rMainPID.ClientID = (ClientID); 
      rMainPID.ChannelLead = (ChannelLead); 
      rMainPID.ChannelSalesPerson = (ChannelSalesPerson); 
      rMainPID.DropDeadDate = (DropDeadDate); 
      rMainPID.MCSalesLead = (MCSalesLead); 
      rMainPID.ProjectTypeID = (ProjectTypeID); 
      rMainPID.ProjectStatusID = (ProjectStatusID); 
      rMainPID.ClientBriefing = (ClientBriefing); 
      rMainPID.ProjectRequirements = (ProjectRequirements); 

      dtMainPID.AddMainPIDRow(rMainPID); 
      taMainPID.Update(dtMainPID); 

     } 

enter image description here

+0

dtMainPID爲空,因此你不能用它來返回MainPIDRow –

+0

的新實例,請檢查對象'dtMainPID'什麼或不 –

+0

嗯,是的,我得到的,但我不知道如何解決它 - 它不應該是空的,所以檢查一下它是沒有或不會使錯誤沒有,但它不會做更新。 – TMB87

回答

1

的這裏的問題是,dtMainPIDnull,大概是因爲你從來沒有指定任何東西給它。我們將預計rMainPIDnull,因爲那是我們正在努力的時候來填充變量 - 但是:你需要在某些時候嘗試使用它之前將值分配給dtMainPID

順便說一句:

  • = default(components.db.PIDSDB.MainPIDRow);沒有任何用處;你不妨聲明和初始化在同一行:

    var rMainPID = dtMainPID.NewMainPIDRow(); 
    
  • DataTable:只是......請考慮調查非DataTable方法在某些時候的數據訪問; 想到小貓