上下文:Web应用程序(asp.net)使用FormView编辑并将pupil数据插入SQL Server数据库。错误:
System.Data.SqlClient.SqlException:无法将值NULL插入列'txtSchoolID',表'iSAMS.dbo.TblPupilManagementPupils';列不允许空值。 INSERT失败。
我有一个包含FormView
控件的aspx页面,该控件包含(编辑,插入和读取)模板和按钮(用于更新和插入数据)。该插件使用onclick事件处理程序在TblPupilManagementPupils
表中插入新行。
此表包含(在其他列中)具有以下属性的主键:
Identity: True Identity seed: 1 Identity Increment: 1 Data type: int Primary key: true Allow nulls: false
在codebehond文件中,我使用objectcontext来注册实体集合并提交新对象。见下文:
protected void btnInsertCandidate_onClick(object sender, EventArgs e)
{
//string SchoolCode = ((Label)selectedCandidateFormView.FindControl("lblCandidateKey")).Text;
string FirstName = ((TextBox)selectedCandidateFormView.FindControl("txbFirstName")).Text;
string LastName = ((TextBox)selectedCandidateFormView.FindControl("txbLastName")).Text;
string PreName = ((TextBox)selectedCandidateFormView.FindControl("txbPreName")).Text;
string Gender = ((TextBox)selectedCandidateFormView.FindControl("txbGender")).Text;
string DOB = ((TextBox)selectedCandidateFormView.FindControl("txbCandidateDOB")).Text;
string YoE = ((TextBox)selectedCandidateFormView.FindControl("txbCandidateYearOfEntry")).Text;
string NCEY = ((DropDownList)selectedCandidateFormView.FindControl("ddlCandidateNCEntryYear")).SelectedValue.ToString();
string BoardingType = ((DropDownList)selectedCandidateFormView.FindControl("ddlCandidateBoardingType")).SelectedValue.ToString();
string Languages = ((TextBox)selectedCandidateFormView.FindControl("txbCandidateLanguage")).Text;
string Nationalities = ((TextBox)selectedCandidateFormView.FindControl("txbCandidateNationality")).Text;
// insert a new candidate into iSAMS (pupil record) via LINQ. Effectively copying a candidate from Admissions into a new pupil row in iSAMS.
using (AdmissionsVerificationApplication.iSAMSEntities iSAMSContext = new AdmissionsVerificationApplication.iSAMSEntities())
{
TblPupilManagementPupil pupil = new TblPupilManagementPupil()
{
txtForename = FirstName,
txtSurname = LastName,
txtPreName = PreName,
txtGender = Gender,
txtDOB = Convert.ToDateTime(DOB),
intEnrolmentSchoolYear = Convert.ToInt32(YoE),
intEnrolmentNCYear = Convert.ToInt32(NCEY),
txtType = BoardingType,
txtLanguage = Languages,
txtNationality = Nationalities
};
iSAMSContext.TblPupilManagementPupils.AddObject(pupil);
iSAMSContext.SaveChanges();
}
}
我还检查了.edmx文件,以确保ID列具有正确的属性 edmx:StorageModels标记:
<EntityType Name="TblPupilManagementPupils">
<Key>
<PropertyRef Name="TblPupilManagementPupilsID" />
</Key>
<Property Name="TblPupilManagementPupilsID" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
...
为了完整性(我认为!),这是生成的edmx设计器文件中的相关部分:
public partial class TblPupilManagementPupil : EntityObject
{
#region Factory Method
/// <summary>
/// Create a new TblPupilManagementPupil object.
/// </summary>
/// <param name="tblPupilManagementPupilsID">Initial value of the TblPupilManagementPupilsID property.</param>
/// <param name="txtSchoolID">Initial value of the txtSchoolID property.</param>
public static TblPupilManagementPupil CreateTblPupilManagementPupil(global::System.Int32 tblPupilManagementPupilsID, global::System.String txtSchoolID)
{
TblPupilManagementPupil tblPupilManagementPupil = new TblPupilManagementPupil();
tblPupilManagementPupil.TblPupilManagementPupilsID = tblPupilManagementPupilsID;
tblPupilManagementPupil.txtSchoolID = txtSchoolID;
return tblPupilManagementPupil;
}
#endregion
#region Primitive Properties
/// <summary>
/// No Metadata Documentation available.
/// </summary>
[EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
[DataMemberAttribute()]
public global::System.Int32 TblPupilManagementPupilsID
{
get
{
return _TblPupilManagementPupilsID;
}
set
{
if (_TblPupilManagementPupilsID != value)
{
OnTblPupilManagementPupilsIDChanging(value);
ReportPropertyChanging("TblPupilManagementPupilsID");
_TblPupilManagementPupilsID = StructuralObject.SetValidValue(value);
ReportPropertyChanged("TblPupilManagementPupilsID");
OnTblPupilManagementPupilsIDChanged();
}
}
}
private global::System.Int32 _TblPupilManagementPupilsID;
partial void OnTblPupilManagementPupilsIDChanging(global::System.Int32 value);
partial void OnTblPupilManagementPupilsIDChanged();
.....
我在某处读到可能需要设置属性IsDBGenerated以指示映射使用DB进行ID生成....但由于这是生成的文件,我不知道如何添加属性。如果确实是这个问题。
任何帮助都会得到很好的接受。
此致
百里
答案 0 :(得分:0)
原因是因为未设置iSAMS.dbo.TblPupilManagementPupils
表的txtSchoolID
属性。您需要设置学校ID或将列设置为允许NULL
。