Link to home
Start Free TrialLog in
Avatar of ANINDYA
ANINDYAFlag for India

asked on

LINQ error (insert data in table)

Experts,
I have an error.
Please see the attached image and the code .
there you will come to know that the problem which I am facing while inserting the data in the table.
Thanking you ,
Anindya
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CDManagementSoftware
{
    public partial class FormSalesEntry : Form
    {
        public FormSalesEntry()
        {
            InitializeComponent();
            
        }


        private void FormSalesEntry_Load(object sender, EventArgs e)
        {

            
        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }

        private void button_Save_Click(object sender, EventArgs e)
        {
            CDCenterManagementDatabaseDataContext dc = new CDCenterManagementDatabaseDataContext();
            Table_Membership newMember = new Table_Membership();
            newMember.Address = textBox_Address.Text;
            newMember.F_Name = textBox_FName.Text;
            newMember.L_Name = textBox_LName.Text;
            newMember.Area = textBox_Area.Text;
            newMember.PostalCode = Convert.ToInt32(textBox_Postalcode.Text.ToString());
            //newMember.PostalCode = textBox_Postalcode.Text.ToString();
            newMember.Phone = textBox_Phone.Text.ToString();
            newMember.email = textBox_Email.Text;
            newMember.Amount_Paid = Convert.ToDecimal(textBox_AmountPaid.Text.ToString());
            //newMember.Amount_Paid = textBox_AmountPaid.Text.ToString(); ;
            newMember.M_option = comboBox_choice.SelectedItem.ToString();
            newMember.Date_Of_Payment = DateTime.Now;


            dc.Table_Memberships.InsertOnSubmit(newMember);
            dc.SubmitChanges();
            MessageBox.Show(" {0} " +newMember.Member_Id.ToString());




        }

        private void tabPage2_Click(object sender, EventArgs e)
        {

        }

        
    }
}

Open in new window

error.JPG
error1.JPG
Avatar of Roshan Davis
Roshan Davis
Flag of United States of America image

looks like some problem realted to this has been fixed in .NET 3.5 SP1, can you check this link?
http://connect.microsoft.com/VisualStudio/feedback/details/353232/linq-to-sql-exceptions
SOLUTION
Avatar of naspinski
naspinski
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Ashok
Ashok
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Another option is

open FormSalesEntry.cs [Design]

click on a TextBox
Press F4

change MaxLength property to 20 characters for example.

Do samething for all other limited charactes string fields.
Now you do not need to code anything as in my previous post.

HTH
Ashok
Avatar of ANINDYA

ASKER

Expert Ashok111
I have done the code change as you said but you see the error.
The screen shot is having the  1. input 2. error  3.code changed as per your instruction

The code is also there in the attached code section as per your instruction .
Please Sir help me.
I am not understanding what is my mistake.
I am trying your 2 nd method also.
Thanking you
Anindya
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CDManagementSoftware
{
    public partial class FormSalesEntry : Form
    {
        public FormSalesEntry()
        {
            InitializeComponent();
            
        }


        private void FormSalesEntry_Load(object sender, EventArgs e)
        {

            
        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }

        private void button_Save_Click(object sender, EventArgs e)
        {
            CDCenterManagementDatabaseDataContext dc = new CDCenterManagementDatabaseDataContext();
            Table_Membership newMember = new Table_Membership();
            string sAddress = textBox_Address.Text.Trim().Substring(0, 20);
            newMember.Address = sAddress;
            //newMember.Address = textBox_Address.Text;

            string sFName = textBox_FName.Text.Trim().Substring(0, 20);
            newMember.F_Name = sFName;

            //newMember.F_Name = textBox_FName.Text;

            string sLName = textBox_LName.Text.Trim().Substring(0, 20);
            newMember.L_Name = sLName;
            //newMember.L_Name = textBox_LName.Text;

            string sArea = textBox_Area.Text.Trim().Substring(0,20);
            newMember.Area = sArea;
            //newMember.Area = textBox_Area.Text;


            newMember.PostalCode = Convert.ToInt32(textBox_Postalcode.Text.ToString());
            //newMember.PostalCode = textBox_Postalcode.Text.ToString();
            newMember.Phone = textBox_Phone.Text.ToString();

            string sEmail = textBox_Email.Text.Trim().Substring(0, 20);
            newMember.email = sEmail;
            //newMember.email = textBox_Email.Text;
            newMember.Amount_Paid = Convert.ToInt32(textBox_AmountPaid.Text.ToString());
            //newMember.Amount_Paid = textBox_AmountPaid.Text.ToString(); ;


            newMember.M_option = comboBox_choice.SelectedItem.ToString();


            newMember.Date_Of_Payment = DateTime.Now;


            dc.Table_Memberships.InsertOnSubmit(newMember);
            dc.SubmitChanges();
            MessageBox.Show(" {0} " +newMember.Member_Id.ToString());




        }

        private void tabPage2_Click(object sender, EventArgs e)
        {

        }

        
    }
}

Open in new window

error.JPG
You are trying to substring something that may or may not be 20 characters, if you try to substring(0,20) a string that is only 10 characters long, it will error.
Avatar of ANINDYA

ASKER

Ashok Sir
I have tried your 2nd method also .
Please see the screen shot.
3 screen shot are there one by one.
Please see the code here I have used is the previous code of at the beginning of my question .
Thanking you
Anindya
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CDManagementSoftware
{
    public partial class FormSalesEntry : Form
    {
        public FormSalesEntry()
        {
            InitializeComponent();
            
        }


        private void FormSalesEntry_Load(object sender, EventArgs e)
        {

            
        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }

        private void button_Save_Click(object sender, EventArgs e)
        {
            CDCenterManagementDatabaseDataContext dc = new CDCenterManagementDatabaseDataContext();
            Table_Membership newMember = new Table_Membership();


            //string sAddress = textBox_Address.Text.Trim().Substring(0, 20);
            //newMember.Address = sAddress;
            newMember.Address = textBox_Address.Text;

            //string sFName = textBox_FName.Text.Trim().Substring(0, 20);
            //newMember.F_Name = sFName;

            newMember.F_Name = textBox_FName.Text;

            //string sLName = textBox_LName.Text.Trim().Substring(0, 20);
            //newMember.L_Name = sLName;
            newMember.L_Name = textBox_LName.Text;

            //string sArea = textBox_Area.Text.Trim().Substring(0,20);
            //newMember.Area = sArea;
            newMember.Area = textBox_Area.Text;


            newMember.PostalCode = Convert.ToInt32(textBox_Postalcode.Text.ToString());
            //newMember.PostalCode = textBox_Postalcode.Text.ToString();
            newMember.Phone = textBox_Phone.Text.ToString();

            //string sEmail = textBox_Email.Text.Trim().Substring(0, 20);
            //newMember.email = sEmail;
            newMember.email = textBox_Email.Text;
            newMember.Amount_Paid = Convert.ToInt32(textBox_AmountPaid.Text.ToString());
            //newMember.Amount_Paid = textBox_AmountPaid.Text.ToString(); ;


            newMember.M_option = comboBox_choice.SelectedItem.ToString();


            newMember.Date_Of_Payment = DateTime.Now;


            dc.Table_Memberships.InsertOnSubmit(newMember);
            dc.SubmitChanges();
            MessageBox.Show(" {0} " +newMember.Member_Id.ToString());




        }

        private void tabPage2_Click(object sender, EventArgs e)
        {

        }

        
    }
}

Open in new window

error.JPG
I have already told you the answers and I am being ignored, I am done following this problem
Avatar of ANINDYA

ASKER

I have given the input very small intentionally so that there comes no question of truncation of data or value
Avatar of ANINDYA

ASKER

Expert naspinski
I am not ignoring any suggestion.
But as you can see the  problem is not getting solved.
Although I have tried all the options.
I hope you can see.
Please do not get angry or upset Expert naspinski
Thanking you
anindya
You said you changed MaxLength to 20 for all fields.

I want you to set it to same length as size of the field in the Database.
I think Phone's size is 10 characters.

If any other field's size is less than 20, please set MaxLength accordingly.

Ashok
Avatar of ANINDYA

ASKER

Thanks to you Ashok Sir and naspinski Sir.
Thanks for your timely intervention in this.
Thanking  you,
Anindya Chatterjee
Bangalore
India
I never got B grade before.