It is currently 09 Sep 2010, 13:46

All times are UTC + 2 hours





 Page 1 of 1 [ 6 posts ] 
Author Message
 Post subject: ERROR in INSERT INTO Statement C#
PostPosted: 29 Jul 2010, 13:51 
Registered User

Joined: 13 Feb 2009, 12:59
Posts: 156
Hi Guys

I am busy learn from SAMS teach yourlsef c# in 24 hours and been struggling for the pass few hours on this Database lesson. My Code is as follows.

class Level Vairables
   OleDbConnection m_cnADONetconnection = new OleDbConnection();
        OleDbDataAdapter m_daDataAdapter = new OleDbDataAdapter();
        DataTable m_dtContacts = new DataTable();
        int m_rowPosition = 0;


Form Load Event
   m_cnADONetconnection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\JosamRepairs.accdb;Persist Security Info=False";
            m_cnADONetconnection.Open();
            m_daDataAdapter = new OleDbDataAdapter ("SELECT * FROM Repairs ",m_cnADONetconnection);
            OleDbCommandBuilder m_cbCommandBuilder = new OleDbCommandBuilder(m_daDataAdapter);
            m_daDataAdapter.Fill(m_dtContacts);


btnSave_click
   DataRow drNewRow = m_dtContacts.NewRow();
            drNewRow["Client"] = cboClient.Text;
            drNewRow["Date"] = dateTimePicker1.Text;
            drNewRow["Serial"] = txtSerial.Text;
            drNewRow["Fault"] = txtWorkDone.Text;
            m_dtContacts.Rows.Add(drNewRow);
            m_daDataAdapter.Update(m_dtContacts);
            m_rowPosition = m_dtContacts.Rows.Count - 1;


I have gone over everything and still cant seem to get this Right. when i click on save this gets High Lighted
Quote:
m_daDataAdapter.Update(m_dtContacts);
With error message, (Syntax error in INSERT INTO statement)

Can anyone see a fault here as i cant.


Offline
 Profile E-mail  
 
 Post subject: Re: ERROR in INSERT INTO Statement C#
PostPosted: 29 Jul 2010, 14:13 
Forum Moderator
Forum Moderator
User avatar

Joined: 19 Sep 2003, 02:00
Posts: 14183
Location: Bloodbank (AKA: Archery Range)
Alliance: Vampires
Check what is in the values you are passing as parameters:

drNewRow["Client"] = cboClient.Text;
drNewRow["Date"] = dateTimePicker1.Text;
drNewRow["Serial"] = txtSerial.Text;
drNewRow["Fault"] = txtWorkDone.Text;


one of them might contain an ' or something which should be escaped before you pass it in.



_________________
Image
The Hitchhiker's Guide to the Galaxy wrote:
Anyone who is capable of getting themselves made President should on no account be allowed to do the job.
Online
 Profile E-mail  
 
 Post subject: Re: ERROR in INSERT INTO Statement C#
PostPosted: 29 Jul 2010, 15:06 
Registered User

Joined: 13 Feb 2009, 12:59
Posts: 156
I double checked and re-checked, everything seems perfect. checked my connection string and good. Even re-checked my access table, using access 2007.


Offline
 Profile E-mail  
 
 Post subject: Re: ERROR in INSERT INTO Statement C#
PostPosted: 29 Jul 2010, 15:58 
Final Warning

Joined: 04 Sep 2004, 02:00
Posts: 8178
Location: /home/room/chair
Alliance: Pirates
Are your data types corresponding?


Offline
 Profile  
 
 Post subject: Re: ERROR in INSERT INTO Statement C#
PostPosted: 30 Jul 2010, 07:25 
Registered User

Joined: 13 Feb 2009, 12:59
Posts: 156
Yes they all are. I have even tried commenting out "Date,Serial,Fault" and still the same error, Then i commeted out "Client Date Fault" and still the same error. Here is the code for my fclsNewclient class.

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;
using System.IO;
using System.Data.OleDb;

namespace josam_Repairs
{
    public partial class clsfNewRepair : Form
    {
        OleDbConnection m_cnADONetconnection = new OleDbConnection();
        OleDbDataAdapter m_daDataAdapter = new OleDbDataAdapter();
        DataTable m_dtContacts = new DataTable();
        int m_rowPosition = 0;


        public clsfNewRepair()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            //Closes the form(clsfNewRepair)
            this.Dispose();
            this.Close();
        }

        private void clsfNewRepair_Load(object sender, EventArgs e)
        {
            try
            {
                clsDirectories.RetrieveDirectories(cboClient);

                m_cnADONetconnection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\JosamRepairs.accdb;Persist Security Info=False";
                m_cnADONetconnection.Open();
                m_daDataAdapter = new OleDbDataAdapter("SELECT * FROM Repairs", m_cnADONetconnection);
                OleDbCommandBuilder m_cbCommandBuilder = new OleDbCommandBuilder(m_daDataAdapter);
                m_daDataAdapter.Fill(m_dtContacts);
            }
            catch (Exception test)
            {
                MessageBox.Show(test.Message);
            }

         
        }

        private void clsfNewRepair_FormClosed(object sender, FormClosedEventArgs e)
        {
            m_cnADONetconnection.Dispose();
            m_cnADONetconnection.Close();

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
           

                DataRow drNewRow = m_dtContacts.NewRow();
                drNewRow["Client"] = cboClient.Text;
               drNewRow["Date"] = dateTimePicker1.Text;
                drNewRow["Serial"] = txtSerial.Text;
                drNewRow["Fault"] = txtWorkDone.Text;
                m_dtContacts.Rows.Add(drNewRow);
                m_daDataAdapter.Update(m_dtContacts);
                m_rowPosition = m_dtContacts.Rows.Count - 1;
            }
            catch (System.Data.OleDb.OleDbException test)
            {
                MessageBox.Show(test.Message);
            }
           
           
        }
    }
}


And then the code for my clsDirectory class which i use to scann up the client folders and insert them into the cboClient.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace josam_Repairs
{
    public class clsDirectories
    {

        public clsDirectories()
        {
        }

        public static void RetrieveDirectories(System.Windows.Forms.ComboBox cboBox)
        {
            //Vairable to store the JosamClientProfilePath
            String ClientProfilePath = @"\\SERVER\\Share Josam\\Josam Client Profile";

            DirectoryInfo di = new DirectoryInfo(ClientProfilePath);

            DirectoryInfo[] diArr = di.GetDirectories();

            foreach (DirectoryInfo dri in diArr)
                cboBox.Items.Add(dri.Name);
        }
    }

}



               







Offline
 Profile E-mail  
 
 Post subject: Re: ERROR in INSERT INTO Statement C#
PostPosted: 30 Jul 2010, 09:44 
Registered User

Joined: 13 Feb 2009, 12:59
Posts: 156
The problem was with the DateTime in Access, i assume that it is a reserved word but changed it to DateCalibrated and works perfectly. thanks for the help:)


Offline
 Profile E-mail  
 
Display posts from previous:  Sort by  
 Page 1 of 1 [ 6 posts ] 

All times are UTC + 2 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to: