英文:
How can save date from textbox to database using Entity Framework?
问题
dtpGraduationYear
是一个包含日期的 TextBox
。
我想要使用 Entity Framework 将 textbox
中的日期保存到数据库中。
private void btnSaveAll_Click(object sender, EventArgs e)
{
//
// 教育选项卡
//
var Education = new database.tblEmployeeQualification
{
EmployeeCode = Convert.ToInt32(txtCode.Text),
UniversitiyCode = string.IsNullOrEmpty(txtEdUniversitiesNo.Text) ? (byte?)null : Convert.ToByte(txtEdUniversitiesNo.Text),
FauculityCode = string.IsNullOrEmpty(txtEdFacultyNo.Text) ? (byte?)null : Convert.ToByte(txtEdFacultyNo.Text),
QualificationsCode = string.IsNullOrEmpty(txtEdQualificationNo.Text) ? (byte?)null : Convert.ToByte(txtEdQualificationNo.Text),
SpecializationCode = string.IsNullOrEmpty(txtEdSpecializationNo.Text) ? (byte?)null : Convert.ToByte(txtEdSpecializationNo.Text),
EductionGrade = string.IsNullOrEmpty(txtEdGradeNo.Text) ? (byte?)null : Convert.ToByte(txtEdGradeNo.Text),
QualificationsTypeCode = string.IsNullOrEmpty(txtQualificationTypeNo.Text) ? (byte?)null : Convert.ToByte(txtQualificationTypeNo.Text),
GraduationYear = dtpGraduationYear.ToString("dd/MM/yyyy"),
Note = txtEdNote.Text,
};
db.tblEmployeeQualifications.AddOrUpdate(Education);
db.SaveChanges();
}
错误在这里
GraduationYear = dtpGraduationYear.ToString("dd/MM/yyyy"),
错误:
> 方法 'tostring' 没有接受 1 个参数的重载
这是我的模型类:
public partial class tblEmployeeQualification
{
public int EmployeeCode { get; set; }
public Nullable<short> UniversitiyCode { get; set; }
public Nullable<short> FauculityCode { get; set; }
public Nullable<short> QualificationsCode { get; set; }
public Nullable<short> SpecializationCode { get; set; }
public Nullable<byte> EductionGrade { get; set; }
public Nullable<byte> QualificationsTypeCode { get; set; }
public Nullable<byte> MainEducationCode { get; set; }
public Nullable<System.DateTime> GraduationYear { get; set; }
public string Note { get; set; }
public virtual tblEmployeeData tblEmployeeData { get; set; }
public virtual tblEnglishLevel tblEnglishLevel { get; set; }
public virtual tblFaculty tblFaculty { get; set; }
public virtual tblMainEduaction tblMainEduaction { get; set; }
public virtual tblQualification tblQualification { get; set; }
public virtual tblQualificationType tblQualificationType { get; set; }
public virtual tblSpecialization tblSpecialization { get; set; }
public virtual tblUniversity tblUniversity { get; set; }
}
英文:
dtpGraduationYear
is a TextBox
that contains a date.
I want to save the date from the textbox
to database using Entity Framework.
private void btnSaveAll_Click(object sender, EventArgs e)
{
//
// tab Education
//
var Education = new database.tblEmployeeQualification
{
EmployeeCode = Convert.ToInt32(txtCode.Text),
UniversitiyCode = string.IsNullOrEmpty(txtEdUniversitiesNo.Text) ? (byte?)null : Convert.ToByte(txtEdUniversitiesNo.Text),
FauculityCode = string.IsNullOrEmpty(txtEdFacultyNo.Text) ? (byte?)null : Convert.ToByte(txtEdFacultyNo.Text),
QualificationsCode = string.IsNullOrEmpty(txtEdQualificationNo.Text) ? (byte?)null : Convert.ToByte(txtEdQualificationNo.Text),
SpecializationCode = string.IsNullOrEmpty(txtEdSpecializationNo.Text) ? (byte?)null : Convert.ToByte(txtEdSpecializationNo.Text),
EductionGrade = string.IsNullOrEmpty(txtEdGradeNo.Text) ? (byte?)null : Convert.ToByte(txtEdGradeNo.Text),
QualificationsTypeCode = string.IsNullOrEmpty(txtQualificationTypeNo.Text) ? (byte?)null : Convert.ToByte(txtQualificationTypeNo.Text),
GraduationYear = dtpGraduationYear.ToString("dd/MM/yyyy"),
Note = txtEdNote.Text,
};
db.tblEmployeeQualifications.AddOrUpdate(Education);
db.SaveChanges();
}
Error is here
GraduationYear = dtpGraduationYear.ToString("dd/MM/yyyy"),
error:
> no overload for method 'tostring' takes 1 arguments
This is my model class:
public partial class tblEmployeeQualification
{
public int EmployeeCode { get; set; }
public Nullable<short> UniversitiyCode { get; set; }
public Nullable<short> FauculityCode { get; set; }
public Nullable<short> QualificationsCode { get; set; }
public Nullable<short> SpecializationCode { get; set; }
public Nullable<byte> EductionGrade { get; set; }
public Nullable<byte> QualificationsTypeCode { get; set; }
public Nullable<byte> MainEducationCode { get; set; }
public Nullable<System.DateTime> GraduationYear { get; set; }
public string Note { get; set; }
public virtual tblEmployeeData tblEmployeeData { get; set; }
public virtual tblEnglishLevel tblEnglishLevel { get; set; }
public virtual tblFaculty tblFaculty { get; set; }
public virtual tblMainEduaction tblMainEduaction { get; set; }
public virtual tblQualification tblQualification { get; set; }
public virtual tblQualificationType tblQualificationType { get; set; }
public virtual tblSpecialization tblSpecialization { get; set; }
public virtual tblUniversity tblUniversity { get; set; }
}
答案1
得分: 0
你可以使用:
GraduationYear = Convert.ToDateTime(dtpGraduationYear.Text.ToString());
英文:
You can use:
GraduationYear = Convert.ToDateTime(dtpGraduationYear.Text.ToString());
答案2
得分: 0
GraduationYear = dtpGraduationYear.Text.ToString("dd/MM/yyyy")
"dtpGraduationYear"你说是一个TextBox。与所有其他的文本框一样,你需要读取"Text"属性。就像错误信息所说的:
方法'tostring'的重载不接受1个参数
也就是说,"TextBox"有一个"ToString()"方法,但不接受参数。
不过,这只是答案的一部分,你的模型期望"GraduationDate"是"Nullable
英文:
The line with the error you said is here:
GraduationYear = dtpGraduationYear.ToString("dd/MM/yyyy")
dtpGraduationYear
you said is a TextBox. As with all your other textboxes, you need to read the Text
property. As the error says:
>no overload for method 'tostring' takes 1 arguments
That is, TextBox
has a ToString()
method, but it takes no arguments.
This is only half the answer though, your model expects GraduationDate
to be Nullable<DateTime>
- you'll need to convert the text to that type, probably using DateTime.Parse
or DateTime.TryParse
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论