我目前正在学习LINQ,现在我正在尝试使用Linq插入sql但插入时遇到了挑战,因为它在我调试时向我显示不正确的字符串格式,但它没有指向特定的行。你能在我犯错的地方帮助我。
保存按钮
List<LEGAL_MEMBER> _LegalMemberList = _dc.LEGAL_MEMBERs.Where(a => a.IDNumber == txtIDNumber.Text.ToString()).ToList();
if (rbtnAreYouEmployed.Items[0].Selected == true)
{
ViewState["AreyouEmployed"] = true;
}
else
{
ViewState["AreyouEmployed"] = false;
}
if (rbtnIsSACitizen.Items[0].Selected == true)
{
ViewState["IsSACitizen"] = true;
}
else
{
ViewState["IsSACitizen"] = false;
}
if (_LegalMemberList != null)
{
if (_LegalMemberList.Count() == 0)
{
LEGAL_MEMBER _legalMember = new LEGAL_MEMBER
{
IDNumber = txtIDNumber.Text,
InceptionDate = Convert.ToDateTime(txtInceptionDate.Text),
LegalPreferedName = txtPreferedName.Text,
Initials = txtInitials.Text,
TitleID = int.Parse(cboTitle.SelectedValue),
FullNames = txtFullNames.Text,
Surname = txtSurname.Text,
Age = int.Parse(txtAge.Text),
DateOfBirth = Convert.ToDateTime(txtDateOfBirth.Text),
PassportNumber = txtPassport.Text,
AreyouEmployed = bool.Parse(ViewState["AreyouEmployed"].ToString()),
Employer = txtEmployer.Text,
ContactNumber = txtContactNumber.Text,
OtherContanctNumber = txtOtherContanctNumber.Text,
EmailAddress = txtEmailAddress.Text,
IsSACitizen = bool.Parse(ViewState["IsSACitizen"].ToString()),
TelephoneWork = txtTelephoneWork.Text,
TelephoneHome = txtTelephoneHome.Text,
};
_dc.LEGAL_MEMBERs.InsertOnSubmit(_legalMember);
_dc.SubmitChanges();
SQL表
CREATE TABLE [dbo].[LEGAL_MEMBER](
[LegalMembershipID] [int] IDENTITY(1,1) NOT NULL,
[InceptionDate] [datetime] NULL,
[LegalPreferedName] [nvarchar](50) NULL,
[Initials] [nvarchar](50) NULL,
[TitleID] [int] NULL,
[FullNames] [nvarchar](50) NULL,
[Surname] [nvarchar](50) NULL,
[Age] [int] NULL,
[DateOfBirth] [datetime] NULL,
[IDNumber] [nvarchar](50) NULL,
[PassportNumber] [nvarchar](50) NULL,
[AreyouEmployed] [bit] NULL,
[Employer] [nvarchar](50) NULL,
[ContactNumber] [nvarchar](50) NULL,
[OtherContanctNumber] [nvarchar](50) NULL,
[EmailAddress] [nvarchar](50) NULL,
[IsSACitizen] [bit] NULL,
[TelephoneWork] [nvarchar](50) NULL,
[TelephoneHome] [nvarchar](50) NULL)
答案 0 :(得分:0)
这将是失败的Parse
方法之一。您有一些选项可以调试它:
在LEGAL_MEMBER _legalMember = new LEGAL_MEMBER
行上设置一个断点,然后在VS中的Parse
中尝试所有单独的Immediate Window
来电。
逐个传递属性值并查看失败的位置。
LEGAL_MEMBER _legalMember = new LEGAL_MEMBER();
_legalMember.LegalMembershipID = int.Parse(txtMemberShipNumber.Text);
_legalMember.InceptionDate = DateTime.Parse(txtInceptionDate.Text);
_legalMember.LegalPreferedName = txtPreferedName.Text;
_legalMember.Initials = txtInitials.Text;
_legalMember.TitleID = int.Parse(cboTitle.SelectedValue);
_legalMember.FullNames = txtFullNames.Text;
_legalMember.Surname = txtSurname.Text;
_legalMember.Age = int.Parse(txtAge.Text);
_legalMember.DateOfBirth = int.Parse(txtDateOfBirth.Text);
_legalMember.IDNumber = txtIDNumber.Text;
_legalMember.PassportNumber = txtPassport.Text;
_legalMember.AreyouEmployed = bool.Parse(ViewState["AreyouEmployed"].ToString());
_legalMember.Employer = txtEmployer.Text;
_legalMember.ContactNumber = txtContactNumber.Text;
_legalMember.OtherContanctNumber = txtOtherContanctNumber.Text;
_legalMember.EmailAddress = txtEmailAddress.Text;
_legalMember.IsSACitizen = bool.Parse(ViewState["IsSACitizen"].ToString());
_legalMember.TelephoneWork = txtTelephoneWork.Text;
_legalMember.TelephoneHome = txtTelephoneHome.Text;
答案 1 :(得分:0)
很难确定这一点,但您所提供的代码中最明显的问题是这一行:
DateOfBirth = int.Parse(txtDateOfBirth.Text)
我认为这将尝试转换类似于&#34; 09/21/2015&#34;像Julian或OADate这样的日期?
如果是这样,您需要将字符串转换为日期时间,然后再转换为整数。
要获取日期时间,请尝试
var dob = DateTime.Parse(txtDateOfBirth.Text);
然后使用OADate
DateOfBirth = dob.ToOADate().
如果您需要Julian日期,请使用如下的简单帮助函数:
public static double ToJulianDate(this DateTime date)
{
return date.ToOADate() + 2415018.5;
}
(摘自this answer)
InceptionDate = DateTime.Parse(txtInceptionDate.Text);
如果你有选择,那么更好的选择是将这些日期存储为数据库中的DateTime列。
答案 2 :(得分:0)
我认为你不需要这一行:在删除这一行后尝试插入:
LegalMembershipID = int.Parse(txtMemberShipNumber.Text)
因为您的主键字段是自动增量。请删除它,然后尝试插入。
答案 3 :(得分:0)
我已经意识到我的复选框导致了问题。深度潜水后,我设法解决了这个问题。以下是我的解决方案,感谢所有帮助过我的人。
List<LEGAL_MEMBER> _LegalMemberList = _dc.LEGAL_MEMBERs.Where(a => a.IDNumber == txtIDNumber.Text.ToString()).ToList();
if (rbtnAreYouEmployed.Items[0].Selected == true)
{
ViewState["AreyouEmployed"] = true;
}
else
{
ViewState["AreyouEmployed"] = false;
}
if (rbtnIsSACitizen.Items[0].Selected == true)
{
ViewState["IsSACitizen"] = true;
}
else
{
ViewState["IsSACitizen"] = false;
}
if (_LegalMemberList != null)
{
if (_LegalMemberList.Count() == 0)
{
LEGAL_MEMBER _legalMember = new LEGAL_MEMBER
{
IDNumber = txtIDNumber.Text,
InceptionDate = Convert.ToDateTime(txtInceptionDate.Text),
LegalPreferedName = txtPreferedName.Text,
Initials = txtInitials.Text,
TitleID = int.Parse(cboTitle.SelectedValue),
FullNames = txtFullNames.Text,
Surname = txtSurname.Text,
Age = int.Parse(txtAge.Text),
DateOfBirth = Convert.ToDateTime(txtDateOfBirth.Text),
PassportNumber = txtPassport.Text,
AreyouEmployed = bool.Parse(ViewState["AreyouEmployed"].ToString()),
Employer = txtEmployer.Text,
ContactNumber = txtContactNumber.Text,
OtherContanctNumber = txtOtherContanctNumber.Text,
EmailAddress = txtEmailAddress.Text,
IsSACitizen = bool.Parse(ViewState["IsSACitizen"].ToString()),
TelephoneWork = txtTelephoneWork.Text,
TelephoneHome = txtTelephoneHome.Text,
};
_dc.LEGAL_MEMBERs.InsertOnSubmit(_legalMember);
_dc.SubmitChanges();