none
why my update doesn't work?? RRS feed

  • Question

  • please help me,
    I donno why my update doesn't work, the row state is "added  all the time.
    my other problem is that there is someting wrong with my insert, get general error that the "insert"statement is wrong.

    public

     

    void UpdateAdapterMannualy(DataSet ds)

    {

     

    DataTable categoryTable = ds.Tables["Tasks"];

    adapter.SelectCommand.Connection = cn;

    adapter.Fill(categoryTable);

     

     

    foreach (DataRow Row in ds.Tables["Tasks"].Rows)

    {

     

    switch (Row.RowState)

    {

     

    case DataRowState.Added :

    dapter.InsertCommand =

    new OleDbCommand(

     

    "INSERT INTO tasks (Name,Date,Note,DurationSt) " +

     

    "VALUES (@Name,@Date,@Note,@DurationSt)");

    adapter.InsertCommand.Parameters.Clear();

     

    adapter.InsertCommand.Parameters.Add(GetNewParameter("Name", Row["Name"]));

    adapter.InsertCommand.Parameters.Add(GetNewParameter(

    "Date", Row["Start"].ToString()));

    adapter.InsertCommand.Parameters.Add(GetNewParameter(

    "Note", Row["Notes"]));

    adapter.InsertCommand.Parameters.Add(GetNewParameter(

    "DurationSt", Row["Duration"].ToString()));

     

     

    this.Open();

    adapter.InsertCommand.Connection = cn;

    adapter.InsertCommand.ExecuteNonQuery();

     

    this.Close();

     

     

    break;
    case DataRowState.Deleted :

     

    break;

     

    case DataRowState.Modified :

     

    break;

    }

    }

    }

    Tuesday, October 6, 2009 8:36 AM

Answers

  • Most likely you receive an error for the INSERT SQL statement because you have reserved named as column names (for example, Name, Date). Try to put all the names into square brackets and it should resolve an error. Your SQL statement should look like 

    new OleDbCommand(

     

     

    "INSERT INTO [tasks] ([Name],[Date],[Note],[DurationSt]) " +

     

     

    "VALUES (@Name,@Date,@Note,@DurationSt)"

    );


    Val Mazur (MVP) http://www.xporttools.net
    • Marked as answer by Zhipeng Lee Monday, October 12, 2009 3:23 AM
    Tuesday, October 6, 2009 10:21 AM
    Moderator

All replies

  • Most likely you receive an error for the INSERT SQL statement because you have reserved named as column names (for example, Name, Date). Try to put all the names into square brackets and it should resolve an error. Your SQL statement should look like 

    new OleDbCommand(

     

     

    "INSERT INTO [tasks] ([Name],[Date],[Note],[DurationSt]) " +

     

     

    "VALUES (@Name,@Date,@Note,@DurationSt)"

    );


    Val Mazur (MVP) http://www.xporttools.net
    • Marked as answer by Zhipeng Lee Monday, October 12, 2009 3:23 AM
    Tuesday, October 6, 2009 10:21 AM
    Moderator
  • You didn't indicate what type of database you are working with but if it's Access then Val is correct.

    List of Microsoft Jet 4.0 reserved words


    Paul ~~~~ Microsoft MVP (Visual Basic)
    Tuesday, October 6, 2009 2:36 PM