Hi All,

Im trying to Insert a new row into a database and refresh the dataset.

This is what I have so far:

void ButtonInsert(object s, EventArgs e)
        {
            string connectionString = null;
            OleDbConnection connection;
            OleDbCommand cmd = new OleDbCommand();
            string sql = null;
            connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;data source=Book.mdb";
            connection = new OleDbConnection(connectionString);
            sql = "INSERT INTO Books (BookKey, Title, Pages, AuthorKey, PublisherID) Values ('12','A1BC','999','12','14')";
            connection.Open();
            cmd.CommandText = sql;
            cmd.Connection = connection;
            cmd.Parameters.Add("@Title", OleDbType.VarChar, 50, "Title");
            cmd.Parameters.Add("@BookKey", OleDbType.VarChar, 50, "BookKey");
            
            cmd.ExecuteNonQuery();
            connection.Close();
            refreshlist(); // restarts the form
        }

When i click the Insert Button it inputs the data into the database but its doesnt seem to appear on the dataset/datatable.

I had an exceptChanges to the dataset but that didnt seem to do much good. just wondering what i am missing.

Eventually I want it so that it inserts from inputed data

Thank you in Advance

Regards

Recommended Answers

All 2 Replies

void ButtonInsert(object s, EventArgs e)
        {
            string connectionString = null;
            OleDbConnection connection;
            OleDbCommand cmd = new OleDbCommand();
            string sql = null;
            connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;data source=Book.mdb";
            connection = new OleDbConnection(connectionString);
            sql = "INSERT INTO Books (BookKey, Title, Pages, AuthorKey, PublisherID) Values (@BookKey, @Title, @Pages, @AuthorKey, @PublisherID)";

            cmd.CommandText = sql;
            cmd.Connection = connection;
            cmd.Parameters.AddWithValue("@BookKey,"10");
            cmd.Parameters.AddWithValue("@Title,"Title of book");
            cmd.Parameters.AddWithValue("@Pages,"110");
            cmd.Parameters.AddWithValue("@AuthorKey,"1");
            cmd.Parameters.AddWithValue("@PublishedID,"1");


            connection.Open();            
            cmd.ExecuteNonQuery();
            connection.Close();
            refreshlist(); // restarts the form
        }
commented: Worked perfectly +1

Thanks for the reply. I Have a slight problem which i did relize would occur. I have 2 tables in my database. one has a primary authorkey and the other has the foreign authorkey for the relationship. it occured that if i typed in an author key which existed i would get an error.

Any ideas on how i can overcome this.

thanks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.