con=new SqlConnection(conn);
con.Open();
using(SqlTransaction tx=con.BeginTransaction())
{
SqlCommand newcom=new SqlCommand("select * from BabyInfo;select * from familyInfo;select * from birthInfo;select * from birthCheckup;select * from leaveHospital",con);
newcom.Transaction=tx;
DataSet ds=new DataSet();
SqlDataAdapter drp;
drp=new SqlDataAdapter(newcom);
rp.SelectCommand=newcom;
SqlCommandBuilder slcom=new SqlCommandBuilder(drp);
drp.Fill(ds);
DataTable tb_babyinfo,tb_familyinfo,tb_birthinfo,tb_birthcheckup,tb_leavehospital;
tb_babyinfo=ds.Tables[0];
tb_familyinfo=ds.Tables[1];
tb_birthinfo=ds.Tables[2];
tb_birthcheckup=ds.Tables[3];
tb_leavehospital=ds.Tables[4];
try
{ //婴儿信息
DataRow new1=tb_babyinfo.NewRow();
new1["name"]=TextBox19.Text.Trim();
new1["sex"]=Convert.ToUInt16(RadioButtonList2.SelectedValue);
string birth=TextBox22.Text.Trim()+"-"+TextBox23.Text.Trim()+"-"+TextBox24.Text.Trim()+" "+TextBox25.Text.Trim()+":"+TextBox26.Text.Trim();
new1["birth"]=DateTime.Parse(birth);
new1["birthNum"]=Convert.ToInt16(TextBox27.Text.Trim());
new1["pregnantWeek"]=Convert.ToUInt16(TextBox28.Text.Trim());
//new1["userName"]=
tb_babyinfo.Rows.Add(new1);
//家庭信息
DataRow new2=tb_familyinfo.NewRow();
new2["motherName"]=TextBox1.Text.Trim();
new2["motherBirth"]=DateTime.Parse(TextBox2.Text.Trim());
new2["motherNation"]=DropDownList1.SelectedValue;
new2["motherDegree"]=DropDownList2.SelectedValue;
new2["motherProfession"]=DropDownList3.SelectedValue;
new2["motherCrop"]=TextBox6.Text.Trim();
new2["fatherName"]=TextBox7.Text.Trim();
new2["fatherBirth"]=DateTime.Parse(TextBox8.Text.Trim());
new2["fatherNation"]=DropDownList4.SelectedValue;
new2["fatherDegree"]=DropDownList5.SelectedValue;
new2["fatherProfession"]=DropDownList6.SelectedValue;
new2["fatherCrop"]=TextBox12.Text.Trim();
new2["address"]=TextBox13.Text.Trim();
new2["phone"]=TextBox14.Text.Trim();
new2["habbit"]=TextBox15.Text.Trim();
new2["member"]=TextBox16.Text.Trim();
new2["allergyHistory"]=TextBox17.Text.Trim();
new2["familyHistory"]=TextBox18.Text.Trim();
tb_familyinfo.Rows.Add(new2);
//出生情况
DataRow new3=tb_birthinfo.NewRow();
new3["inpatientNum"]=Convert.ToInt32(TextBox21.Text.Trim());
new3["delivery"]=DropDownList7.SelectedValue;
new3["growth"]=TextBox3.Text.Trim();
new3["breath"]=TextBox4.Text.Trim();
new3["weight"]=Convert.ToSingle(TextBox9.Text.Trim());
new3["high"]=Convert.ToSingle(TextBox5.Text.Trim());
new3["birthGrade"]=TextBox10.Text.Trim();
new3["motherSyndrome"]=TextBox11.Text.Trim();
new3["antenatalBabyCondition"]=TextBox29.Text.Trim();
new3["especialTreat"]=TextBox30.Text.Trim();
tb_birthinfo.Rows.Add(new3);
//体格检查
DataRow new4=tb_birthcheckup.NewRow();
new4["reactivity"]=DropDownList8.SelectedValue;
new4["cry"]=DropDownList9.SelectedValue;
new4["face"]=TextBox31.Text.Trim();
new4["skin"]=DropDownList10.SelectedValue;
new4["limb"]=TextBox32.Text.Trim();
new4["others"]=TextBox33.Text.Trim();
new4["diagnosis"]=TextBox34.Text.Trim();
tb_birthcheckup.Rows.Add(new4);
//出院记录
DataRow new5=tb_leavehospital.NewRow();
string leavedate=TextBox35.Text.Trim()+"-"+TextBox36.Text.Trim()+"-"+TextBox37.Text.Trim();
new5["leaveDate"]=DateTime.Parse(leavedate);
new5["weight"]=Convert.ToSingle(TextBox38.Text.Trim());
new5["umbilicalCord"]=TextBox39.Text.Trim();
new5["feedCondition"]=TextBox40.Text.Trim();
new5["commonlyCondition"]=TextBox41.Text.Trim();
new5["conclusion"]=TextBox42.Text.Trim();
new5["doctorAdvice"]=TextBox42.Text.Trim();
//new5["doctor"]=
tb_leavehospital.Rows.Add(new5);
if(ds.HasChanges(DataRowState.Added))
{
DataSet xds;
xds=ds.GetChanges(DataRowState.Added);
if(xds.HasErrors)
{
tx.Rollback();
return;
}
drp.Update(xds.Tables[0]); 能正确执行,,
drp.Update(xds.Tables[1]); 出现错误
drp.Update(xds.Tables[2]);
drp.Update(xds.Tables[3]);
drp.Update(xds.Tables[4]);
}
else
{
tx.Rollback();
return;
}
tx.Commit();
con.Close();
}
{
(tx!=null)
tx.Rollback();
}
}
现在的问题是,,程序执行后,,table[0]能增加,,到更新table[1]时,,报错:
DataTable“Table1”中缺少 SourceColumn“name”的 DataColumn“name”。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.InvalidOperationException: DataTable“Table2”中缺少 SourceColumn“name”的 DataColumn“name”。
源错误:
行 557: drp.Update(xds.Tables[0]);
行 558: drp.Update(xds.Tables[1]);
行 559: drp.Update(xds.Tables[2]);
行 560: drp.Update(xds.Tables[3]);
行 561: drp.Update(xds.Tables[4]);
2 个解决方案
#1
DataTable“Table1”中缺少 SourceColumn“name”的 DataColumn“name”。
--》看一下你的SQL语句中是不是少了个: Name字段
--》看一下你的SQL语句中是不是少了个: Name字段
#2
你的new2这个DataRow里面缺少了对name字段的赋值,而你fill出来的Table[1]中有name字段,换言之你的DataRow里面的字段你肯定漏掉了对某个字段赋值或者你字段名打错了,再检查一下
#1
DataTable“Table1”中缺少 SourceColumn“name”的 DataColumn“name”。
--》看一下你的SQL语句中是不是少了个: Name字段
--》看一下你的SQL语句中是不是少了个: Name字段
#2
你的new2这个DataRow里面缺少了对name字段的赋值,而你fill出来的Table[1]中有name字段,换言之你的DataRow里面的字段你肯定漏掉了对某个字段赋值或者你字段名打错了,再检查一下