0

这是我一直在 VB.net 中做的事情,我正试图在 c# 中复制它。基本上我有一个连接到 sqlserver 的数据集,我正在尝试添加一个新行。我有以下内容,但 rMainPID 似乎为空。

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

        }

在此处输入图像描述

4

1 回答 1

1

这里的问题dtMainPIDnull,大概是因为你从来没有给它分配任何东西。我们希望 rMainPID是,因为这是我们当时试图填充的变量 - 但是:在尝试使用它之前null,您需要dtMainPID在某个时刻为其分配一个值。

顺便:

  • 没有= default(components.db.PIDSDB.MainPIDRow);任何用处;您不妨在同一行声明和初始化:

    var rMainPID = dtMainPID.NewMainPIDRow();
    
  • DataTable:只是......请考虑DataTable在某个时候调查非数据访问方法;想想小猫

于 2013-10-31T12:02:14.460 回答