Click here to Skip to main content
14,691,039 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Additional information: Syntax error in INSERT INTO statement..


This Error Show when i try save record Only in First Table Data Save successfully But in Second Table Data not Save and Show Error
so please help me resolve this Problam
my code is here:
i have two table in my database first CUSTOMER and second MONTHLYINSTALLMENT

What I have tried:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Configuration;


namespace Fincorp
{
    public partial class NewCustomerEntry : Form
    {
        public NewCustomerEntry()
        {
            InitializeComponent();
        }

        private void NewCustomerEntry_Load(object sender, EventArgs e)
        {
           
        }

        private void textBoxMobileNo_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBoxNetLoanAmt_TextChanged(object sender, EventArgs e)
        {

        }

        private void btnexitCustomerEntry_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnsaveCustomerEntry_Click(object sender, EventArgs e)
        {
            if (IsValidated())
            {
                try
                {
                    SaveRecord(); tabControl1.SelectTab(tabPage2); SaveRecord1();
                    
                }
                catch (ApplicationException ex)
                {
                    MessageBox.Show("Error:" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void SaveRecord()
        {
            String connstring = ConfigurationManager.ConnectionStrings["vijay"].ConnectionString;
            string cmdstring = "insert into customer (LoanNumber, LoanDate, CustomerName, FatherName, Address, MobileNo, SecMobileNo, City, District, State) VALUES (@LoanNumber, @LoanDate, @CustomerName, @FatherName, @Address, @MobileNo, @SecMobileNo, @City, @District, @State)";
          
            using (OleDbConnection con = new OleDbConnection(connstring))
            {
                using (OleDbCommand cmd = new OleDbCommand(cmdstring, con))
                {
                    con.Open();

                    cmd.Parameters.AddWithValue("@LoanNumber", textBoxLoanNumber.Text);
                    cmd.Parameters.AddWithValue("@LoanDate", textBoxLoanDate.Text);
                    cmd.Parameters.AddWithValue("@CustomerName", txtCustomerName.Text);
                    cmd.Parameters.AddWithValue("@FatherName", txtFatherName.Text);
                    cmd.Parameters.AddWithValue("@Address", txtAddress.Text);
                    cmd.Parameters.AddWithValue("@MobileNo", txtMobileNo.Text);
                    cmd.Parameters.AddWithValue("@SecMobileNo", txtSecMobileno.Text);
                    cmd.Parameters.AddWithValue("@City", txtCity.Text);
                    cmd.Parameters.AddWithValue("@District", txtDistrict.Text);
                    cmd.Parameters.AddWithValue("@State", txtState.Text);
                    
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Recourd is saved", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    con.Close();
                }   
            }
        }

        private void SaveRecord1()
        {
            tabControl1.SelectTab(tabPage2);
            {
                String connstring = ConfigurationManager.ConnectionStrings["vijay"].ConnectionString;
                string cmdstring = "insert into monthlyinstalment (LoanNumber, Price, DownPayment, FileCharge, Month, Intrest) VALUE (@LoanNumber1, @Price, @DownPayment, @FileCharge, @Month, @Intrest)";

                using (OleDbConnection con = new OleDbConnection(connstring))
                {
                    using (OleDbCommand cmd = new OleDbCommand(cmdstring, con))
                    {
                        con.Open();

                        cmd.Parameters.AddWithValue("@LoanNumber1", textBoxLoanNumber.Text);
                        cmd.Parameters.AddWithValue("@Price", txtPrice.Text);
                        cmd.Parameters.AddWithValue("@DownPayment", txtDownPayment.Text);
                        cmd.Parameters.AddWithValue("@FileCharge", txtFileCharge.Text);
                        cmd.Parameters.AddWithValue("@Month", txtMonth.Text);
                        cmd.Parameters.AddWithValue("@Intrest", txtInterest.Text);

                        cmd.ExecuteNonQuery();
                        con.Close();
                    }
                }
            }
        }

        private bool IsValidated()
        {
           if (txtCustomerName.Text.Trim() == string.Empty)
           {
               MessageBox.Show("Customer Name is Requied", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
               txtCustomerName.Focus();
               return false;
           }

           if (txtFatherName.Text.Trim() == string.Empty)
           {
               MessageBox.Show("Father's Name is Requied", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
               txtFatherName.Focus();
               return false;
           }

           if (txtAddress.Text.Trim() == string.Empty)
           {
               MessageBox.Show("Address is Requied", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
               txtAddress.Focus();
               return false;
           }

           if (txtMobileNo.Text.Trim() == string.Empty)
           {
               MessageBox.Show("Mobile Number is Requied", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
               txtMobileNo.Focus();
               return false;
           }


           if (txtPrice.Text.Trim() == string.Empty)
           {
               MessageBox.Show("Loan Amount is Requied", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
               tabControl1.SelectTab(tabPage2);  txtPrice.Focus();
               return false;
           }

           if (txtDownPayment.Text.Trim() == string.Empty)
           {
               MessageBox.Show("Down Payment is Requied", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
               tabControl1.SelectTab(tabPage2);  txtDownPayment.Focus();
               return false;
           }

           if (txtFileCharge.Text.Trim() == string.Empty)
           {
               MessageBox.Show("File Charge is Requied", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
               tabControl1.SelectTab(tabPage2);  txtFileCharge.Focus();
               return false;
           }
           if (txtMonth.Text.Trim() == string.Empty)
           {
               MessageBox.Show("Month is Requied", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
               tabControl1.SelectTab(tabPage2);  txtMonth.Focus();
               return false;
           }

           if (txtInterest.Text.Trim() == string.Empty)
           {
               MessageBox.Show("Interest Rate is Requied", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
               tabControl1.SelectTab(tabPage2); txtInterest.Focus();
               return false;
           }
            
            return true;
        }
    }
}
Posted
Updated 28-Feb-20 23:09pm
v3
Comments
Richard MacCutchan 29-Feb-20 4:56am
   
Most likely something in one of your textboxes is not valid. Use your debugger to check.

1 solution

you seem to be missing S from the VALUES keyword.

Instead of
string cmdstring = "insert into monthlyinstalment (LoanNumber, Price, DownPayment, FileCharge, Month, Intrest) VALUE (@LoanNumber1, @Price, @DownPayment, @FileCharge, @Month, @Intrest)";

use
string cmdstring = "insert into monthlyinstalment (LoanNumber, Price, DownPayment, FileCharge, Month, Intrest) VALUES (@LoanNumber1, @Price, @DownPayment, @FileCharge, @Month, @Intrest)";
   
Comments
Amar chand123 29-Feb-20 4:39am
   
Wendelius, i change "Value" into "values" But Problem Remaining Same
Wendelius 29-Feb-20 4:43am
   
What exception you now get ?

Based on your post should the name of the table be MONTHLYINSTALLMENT instead of MONTHLYINSTALMENT, one L missing perhaps?
Wendelius 29-Feb-20 4:49am
   
Please post the full error message you get.
Amar chand123 29-Feb-20 4:52am
   
Fixed MONTHLYINSTALMENT, but Problem Remaining Same
https://snipboard.io/6h09Vl.jpg
Design View
Wendelius 29-Feb-20 4:54am
   
Could you post the error simply as text here. I'm not able to access the site you're referring.
Amar chand123 29-Feb-20 4:56am
   
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll

Additional information: Syntax error in INSERT INTO statement.
Wendelius 29-Feb-20 4:58am
   
Could you also post the current statement causing this problem (after all the changes)
Amar chand123 29-Feb-20 4:59am
   
output from debug

'Fincorp.vshost.exe' (CLR v4.0.30319: Fincorp.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Fincorp.vshost.exe' (CLR v4.0.30319: Fincorp.vshost.exe): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities\12.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Fincorp.vshost.exe' (CLR v4.0.30319: Fincorp.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms\v4.0_4.0.0.0__b77a5c561934e089\System.Windows.Forms.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Fincorp.vshost.exe' (CLR v4.0.30319: Fincorp.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Fincorp.vshost.exe' (CLR v4.0.30319: Fincorp.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Drawing\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Fincorp.vshost.exe' (CLR v4.0.30319: Fincorp.vshost.exe): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities.Sync\12.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.Sync.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Fincorp.vshost.exe' (CLR v4.0.30319: Fincorp.vshost.exe): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.Debugger.Runtime\12.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.Debugger.Runtime.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Fincorp.vshost.exe' (CLR v4.0.30319: Fincorp.vshost.exe): Loaded 'C:\Users\VIJAY\Documents\Visual Studio 2013\Projects\New folder\Fincorp\bin\Debug\Fincorp.vshost.exe'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Fincorp.vshost.exe' (CLR v4.0.30319: Fincorp.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Fincorp.vshost.exe' (CLR v4.0.30319: Fincorp.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Fincorp.vshost.exe' (CLR v4.0.30319: Fincorp.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml.Linq\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.Linq.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Fincorp.vshost.exe' (CLR v4.0.30319: Fincorp.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Data.DataSetExtensions\v4.0_4.0.0.0__b77a5c561934e089\System.Data.DataSetExtensions.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Fincorp.vshost.exe' (CLR v4.0.30319: Fincorp.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\Microsoft.CSharp\v4.0_4.0.0.0__b03f5f7f11d50a3a\Microsoft.CSharp.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Fincorp.vshost.exe' (CLR v4.0.30319: Fincorp.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\System.Data\v4.0_4.0.0.0__b77a5c561934e089\System.Data.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enable
Amar chand123 29-Feb-20 5:04am
   
Sir if you sand me your Email than i send my Project File
Same Error

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll

Additional information: Syntax error in INSERT INTO statement.
Wendelius 29-Feb-20 5:34am
   
If you could post the current, failing SQL statement here, that'd be best.
Wendelius 29-Feb-20 7:04am
   
One thing crossed my mind. Depending on the database system you use the word month could be a reserved word. If that is the case try surrounding it with brackets [month] (or whatever mechanism used in your database).
Amar chand123 29-Feb-20 8:59am
   
Thank you @Wendelius
it Work :) [Really Month is Reserved Word]
I change "Month" into "Mont" and Error is gone, Finally Problem Solved,
I try Search all Internet for this Problem Solution, now i am relaxed
Now All Data Saved Without any Error

Thank Your
Wendelius 29-Feb-20 11:28am
   
You're welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900