CG

CG

  • NA
  • 22
  • 0

Help Updating a database required

Mar 9 2009 1:48 PM

Hi there I have a Acess database. On my windows form I have a dataGridView, the dataGridView is not bound to the database, I search for a serial number from a txtBox and fill the dataGridView with the found information, all this is working ok but I cannot update the database to refelect the changes made in dataGridView.. Please could comeone help as I am new to c#.....

using System;
using System.Data;
using System.Diagnostics;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Globalization;

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

         int mCount = 0;

        System.Data.OleDb.OleDbConnection con;
       
        System.Data.OleDb.OleDbDataAdapter da;
        System.Data.OleDb.OleDbCommandBuilder cb;
        DataSet ds1;
        DataRow dr1;

        private void Form1_Load(object sender, EventArgs e)
        {
            con = new System.Data.OleDb.OleDbConnection();
            ds1 = new DataSet();
            con.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\";


            string sql = "SELECT * From CraigsFault";
            da = new System.Data.OleDb.OleDbDataAdapter(sql, con);

            con.Open();
            da.Fill(ds1, "CraigsFault");
            con.Close();
            con.Dispose();

        }

        private void btnFind_Click(object sender, EventArgs e)
        {
           
            con.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\";

            string sql = "SELECT * From CraigsFault";
            da = new System.Data.OleDb.OleDbDataAdapter(sql, con);

            con.Open();
            da.Fill(ds1, "CraigsFault");
            con.Close();
            con.Dispose();

            string searchFor = txtFind.Text;
            int results = 0;
            DataRow[] returnedRows;
            returnedRows = ds1.Tables["CraigsFault"].Select("PRODUCT_1='" + searchFor + "'");
            results = returnedRows.Length;

            if (results > 0)
                do
                {
                    dr1 = returnedRows[mCount];
                    dataGridView1.Rows.Add(dr1[5], dr1[6], dr1[4], dr1[1], dr1[13]);
                    mCount++;
                }
                while (results > mCount);
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            da.UpdateCommand = cb.GetUpdateCommand();
            da.InsertCommand = cb.GetInsertCommand();
            da.DeleteCommand = cb.GetDeleteCommand();
           
            da.Update(ds1, "CraigsFault");
        }
    }
}


Answers (3)