none
DataSet is not updating the table RRS feed

  • Question

  • IN the following code sample, I want to update the table but it's not. it is Updating my datagrid. I want the description to be 'Care' on the table. This is just a sample project to learn how to use the datasets.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data.SqlClient;
    using System.Data;

    public partial class _Default : System.Web.UI.Page
    {

        SqlCommand cmd;
        SqlDataAdapter da;
        DataSetPK ds;
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection cn = new SqlConnection("server=ORDERS;uid=OG;pwd=OG;database=PRO2");
            cmd = new SqlCommand("select * from [OG].[TAP_C_ROOM_TABLES]", cn);
            da = new SqlDataAdapter(cmd);
           // dsProducts tds = new dsProducts();
            ds = new DataSetPK();

            da.Fill(ds, ds.TAP_C_ROOM_TABLES.TableName);

            dgok.DataSource = ds;
            dgok.DataBind();

        }
        protected void btnInsert_Click(object sender, EventArgs e)
        {

       
            DataRow row = null;
           // DataSetPK ds = new DataSetPK();
            row = ds.TAP_C_ROOM_TABLES.NewTAP_C_ROOM_TABLESRow();
            row[0] = "T1";
            row[1] = "a";

            row[2] = "test";
            ds.TAP_C_ROOM_TABLES.BeginInit();
            ds.TAP_C_ROOM_TABLES.Rows.Add(row);
            ds.TAP_C_ROOM_TABLES.EndInit();
            ds.TAP_C_ROOM_TABLES.AcceptChanges();
            ds.AcceptChanges();

            da.Update(ds.TAP_C_ROOM_TABLES);

            da.Update(ds, ds.TAP_C_ROOM_TABLES.TableName);


            dgok.DataSource = ds;
            dgok.DataBind();

        }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            ds.TAP_C_ROOM_TABLES.Rows[0][2]="Care";
            ds.TAP_C_ROOM_TABLES.AcceptChanges();
            ds.AcceptChanges();
            da.Update(ds, ds.TAP_C_ROOM_TABLES.TableName);

            dgok.DataSource = ds;
            dgok.DataBind();

        }
    }

    Wednesday, August 24, 2011 1:31 PM

Answers

  • I'm sorry, I didn't notice that there were also DataTable.AcceptChanges() ... I only focused on the ds. Did you comment out ALL the .AcceptChanges() code (both DataSet and DataTable)?

    Another thing I noticed is that you're using Begin/EndInit() ... you want to use the Begin/EndEdit() instead ...and, I don't even think you need a .BeginEdit(), but you should definitely use the .EndEdit().


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Marked as answer by Larcolais Gong Thursday, September 1, 2011 2:46 AM
    Wednesday, August 24, 2011 3:59 PM

All replies

  • Get rid of the ds.AcceptChanges() ... what that does is to make the DataSet appear as if there are NO changes! Obviously, not what you want!  ;0) Comment out that line and it should work.
    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Wednesday, August 24, 2011 3:44 PM
  • Bonnie, I commented it out but it still doesn't update the table. there is a foreign key on the table but i am just trying to change the description as a sample.

    Wednesday, August 24, 2011 3:52 PM
  • I'm sorry, I didn't notice that there were also DataTable.AcceptChanges() ... I only focused on the ds. Did you comment out ALL the .AcceptChanges() code (both DataSet and DataTable)?

    Another thing I noticed is that you're using Begin/EndInit() ... you want to use the Begin/EndEdit() instead ...and, I don't even think you need a .BeginEdit(), but you should definitely use the .EndEdit().


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Marked as answer by Larcolais Gong Thursday, September 1, 2011 2:46 AM
    Wednesday, August 24, 2011 3:59 PM