简体   繁体   中英

LINQ to SQL Failing to Insert to the Database

I am currently learning LINQ and now I am trying to insert using Linq to sql but am having challenges when inserting as It is showing me incorrect string format when I debug but it is no pointing on specific line. Can you please help me where I made mistake.

Save button

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 Table

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)

It will be one of the Parse methods that fail. You have some options to debug this:

  1. Put a breakpoint on the line LEGAL_MEMBER _legalMember = new LEGAL_MEMBER and then try all individual Parse calls in the Immediate Window in VS.

  2. Pass property values one-by-one and see where it fails.

     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; 

Its hard to be sure from this, but the most obvious issue in the code you've given is this line:

DateOfBirth = int.Parse(txtDateOfBirth.Text)

I assume that this is going to attempt to convert a string similar to "09/21/2015" to something like a Julian or OADate date?

If so, you need to convert the string to a datetime, and then onwards to the integer.

To get the datetime, try

var dob = DateTime.Parse(txtDateOfBirth.Text);

To then get an OADate use

DateOfBirth = dob.ToOADate().

If you need a Julian date use a simple helper function like this:

public static double ToJulianDate(this DateTime date)
{
    return date.ToOADate() + 2415018.5;
}

(taken from this answer )

The same would be true of InceptionDate = DateTime.Parse(txtInceptionDate.Text);

If you have the choice though, a better option would be to store these dates as DateTime columns in the database though.

I don't think you need this line: Try to insert after removing this line:

       LegalMembershipID = int.Parse(txtMemberShipNumber.Text) 

as your primary key field is auto increment. Please remove this and then try to insert.

I have realized that my check boxes where the one's causing the problem. After a deep dive i managed to fix it. below is my solution and thanks to everyone who helped me.

            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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM