[英]Using dataset in C#
This is something I do in VB.net all the time and i'm trying to replicate it in c#. 这是我一直在VB.net中做的事情,我正在尝试在c#中复制它。 Basically I have a dataset connected to sqlserver and i'm trying to add a new row. 基本上,我有一个连接到sqlserver的数据集,我正在尝试添加新行。 I have the following, but rMainPID seems to be null. 我有以下内容,但rMainPID似乎为空。
private components.db.PIDSDB.MainPIDDataTable dtMainPID;
private components.db.PIDSDBTableAdapters.MainPIDTableAdapter taMainPID = new components.db.PIDSDBTableAdapters.MainPIDTableAdapter();
Then Main: 然后Main:
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);
}
The issue here is that dtMainPID
is null
, presumably because you never assign anything to it. 这里的问题是dtMainPID
为null
,大概是因为您从未为其分配任何内容。 We would expect rMainPID
to be null
, because that is the variable we are trying to populate at the time - but: you need to assign a value to dtMainPID
at some point before trying to use it. 我们希望 rMainPID
为null
,因为这是我们当时试图填充的变量-但是:您需要在某个时候为dtMainPID
分配一个值, 然后再尝试使用它。
Incidentally: 偶然:
the = default(components.db.PIDSDB.MainPIDRow);
= default(components.db.PIDSDB.MainPIDRow);
does nothing useful; 没有什么用处; you might as well declare and init on the same line: 您最好在同一行上进行声明和初始化:
var rMainPID = dtMainPID.NewMainPIDRow();
DataTable
: just... please consider investigating non- DataTable
approaches to data access at some point; DataTable
:只是……请考虑在某些时候研究非DataTable
数据访问方法; think of the kittens 想起小猫
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.