none
Problems with updating data to access through gridview in C# RRS feed

  • Question

  • Hello everyone,

    I got some difficulties with getting my code to work.

    1) I get the data from a mdb file in my datagridview.

    2) now I want to change records or add records and update it to my mdb file.

    this is the code I wrote:

        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            OleDbConnection con = new OleDbConnection();
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            OleDbDataAdapter da = new OleDbDataAdapter();

            private void Form1_Load(object sender, EventArgs e)
            {
                con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\Latop-Sven\\Desktop\\dbExamen.mdb";
                con.Open();


                ds.Tables.Add(dt);


                da = new OleDbDataAdapter("Select * from tblStudent",con);
                da.Fill(dt);
                dataGridView1.DataSource = dt.DefaultView;
                con.Close();
            }

            private void btnsave_Click(object sender, EventArgs e)
            {
                con.Open();
                da.Update(ds, "tblStudent");
                con.Close();
            }
        }

    the error that I get at the moment is: Update unable to find TableMapping['tblStudent'] or DataTable 'tblStudent'.

    Can someone help me with this?

    thanks

    Wednesday, August 15, 2012 2:17 PM

Answers

  • I would suspect that when you call the Fill method you will need to specify the name of the DataTable. It doesn't look like you do that in your code.

    Paul ~~~~ Microsoft MVP (Visual Basic)

    Wednesday, August 15, 2012 6:10 PM
  • Paul is right. As you do not mention table name at the time initialization, it will consider it as “Table1”. It is good practices to always give a meaning full name to a Data table.

    You can do in many ways i.e

    DataTable dt = new DataTable("tblStudent");

    OR

    dt.TableName="tblStudent";

    OR

    da.Fill(dt,"tblStudent");


    Lingaraj Mishra

    Thursday, August 16, 2012 6:53 AM

All replies

  • I would suspect that when you call the Fill method you will need to specify the name of the DataTable. It doesn't look like you do that in your code.

    Paul ~~~~ Microsoft MVP (Visual Basic)

    Wednesday, August 15, 2012 6:10 PM
  • Paul is right. As you do not mention table name at the time initialization, it will consider it as “Table1”. It is good practices to always give a meaning full name to a Data table.

    You can do in many ways i.e

    DataTable dt = new DataTable("tblStudent");

    OR

    dt.TableName="tblStudent";

    OR

    da.Fill(dt,"tblStudent");


    Lingaraj Mishra

    Thursday, August 16, 2012 6:53 AM