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
ANINDYAAsked:
Who is Participating?
 
AshokCommented:
I cannot see images in error1.jpg (three screens together).

            string sFName = textBox_FName.Text.Trim().Substring(0,20);  // assuming length of FirsName is 20 charactes max
            newMember.F_Name = sFName;

// Do same thing for all string fields with limited length.

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

HTH
Ashok

0
 
Roshan DavisCommented:
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
0
 
naspinskiCommented:
It is very simple, your code looks fine, it is your DB that is causing the problem.

One of your variables (probably a VARCHAR) is set to a limit of something and you are inserting something that is longer than the given space.

For example, if you have

Area set as a VARCHAR(20) and you try to put in a string that is 21 characters, you will get this error.

So you either need to truncate your values to the specified DB size before entry, or increase the size of your columns.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
AshokCommented:
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
0
 
ANINDYAAuthor Commented:
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
0
 
naspinskiCommented:
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.
0
 
ANINDYAAuthor Commented:
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
0
 
naspinskiCommented:
I have already told you the answers and I am being ignored, I am done following this problem
0
 
ANINDYAAuthor Commented:
I have given the input very small intentionally so that there comes no question of truncation of data or value
0
 
ANINDYAAuthor Commented:
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
0
 
AshokCommented:
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
0
 
ANINDYAAuthor Commented:
Thanks to you Ashok Sir and naspinski Sir.
Thanks for your timely intervention in this.
Thanking  you,
Anindya Chatterjee
Bangalore
India
0
 
AshokCommented:
I never got B grade before.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.