CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2009
    Posts
    19

    [RESOLVED] problem with updating a datatable

    using 3.5 .net
    here is my connection related code (i know it works at least partially, as it allows me to read data from the table, and to recognize the columns before the dataadapter.update command).

    public partial class Edit_Subjects : Form
    {
    OleDbConnection m_cnADONetConnection = new OleDbConnection();
    OleDbDataAdapter m_daSubjectDataAdapter;
    OleDbCommandBuilder m_cbSubjectCommandBuilder;
    DataTable m_Subjects = new DataTable();

    private void Edit_Subjects_Load(object sender, EventArgs e)
    {
    m_cnADONetConnection.ConnectionString =
    @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\temp\testing.mdb";
    m_cnADONetConnection.Open();
    m_daSubjectDataAdapter = new OleDbDataAdapter("Select * From Subject", m_cnADONetConnection);
    m_daSubjectDataAdapter.Fill(m_Subjects);

    When i try to add a row, using
    DataRow drNewrow = m_Subjects.NewRow();
    m_Subjects.Rows.Add(drNewrow);
    m_daSubjectDataAdapter.Update(m_Subjects);
    to do so, i receve an error:
    "Syntax error in INSERT INTO statement" (unhandled OledbException)

    I also receive errors when i try to edit or delete rows.
    Can someone please find the error?
    Thank you very much.

  2. #2
    Join Date
    Aug 2009
    Posts
    19

    Re: problem with updating a datatable

    I was looking at another post, in a different language that generated the same error, and there is was because there were spaces in the column names.
    is it possible that that is the problem here?
    currently my code for adding the new row looks like:
    Code:
                    DataRow drNewrow   = m_Subjects.NewRow();
                    drNewrow["game1"]  = txtgame1.Text;
                    drNewrow["game2"]  = cbBgame2.SelectedItem.ToString();
                    drNewrow["Preference Level"]    = cbBpreference_Level.SelectedItem.ToString();
                    m_Subjects.Rows.Add(drNewrow);
                    m_daSubjectDataAdapter.Update(m_Subjects);
    Thanks

  3. #3
    Join Date
    Nov 2006
    Posts
    357

    Re: problem with updating a datatable

    I dont think "Preference Level" is a valid column name... or if it is then it isnt recommended to put spaces in... if you do have *dodgy* table names you can always enclose them in [Table Name] which should allow it... i.e MyADOObject["[Preference Level]"];

  4. #4
    Join Date
    Aug 2009
    Posts
    19

    Re: problem with updating a datatable

    That is done when I try and create a new datarow?
    so it would look like:
    drNewRow[somenamehere["[Preference Level]"] = something.text
    ?
    where do i declare the table column name?

  5. #5
    Join Date
    Aug 2009
    Posts
    19

    Re: problem with updating a datatable

    This worked for me, and is what i believe was meant.
    where generally the column names is "somenamehere" if it is two or more words, its "[some name here]"

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured