locked
not able to delete record in the database. RRS feed

  • Question

  • hi ther.
    maybe my question sound so easy for you guys.
    im new to c# programming.

    i have trouble in deleting record in the database and at the same time it automatically update the record to the database. I managed to delete record in the gridview but when i run back the application,the record still there because it doesn't connect to the database.

    the code that i use that managed to delete the record in the gridview is
    ------> dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);

    so can anyone help me in achieving the correct way and proper solution? I need a clear description..

    this is my code:


    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Text;

    using System.Windows.Forms;

    using System.Data.OleDb;

    namespace WindowsApplication1

    {

    public partial class Form1 : Form

    {

    public Form1()

    {

    InitializeComponent();

    }

    System.Data.OleDb.OleDbConnection con;

    DataSet ds1 = new DataSet();

    DataSet ds2 = new DataSet(); //for the sequencer tab

    System.Data.OleDb.OleDbDataAdapter da; //for the command tab

    System.Data.OleDb.OleDbDataAdapter da2; //for the sequencer tab

    string[,] set2 = new string[20, 5];

    string[] colnames = new string[5];

    int i = 0;



    private void Form1_Load(object sender, EventArgs e)

    {

    con = new System.Data.OleDb.OleDbConnection();

    con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=C:/projectDb.mdb";

    string sql = "SELECT * From Table1";

    da = new System.Data.OleDb.OleDbDataAdapter(sql, con);

    con.Open();

    da.Fill(ds1, "Table1");

    NavigateRecords();

    dataGridView1.DataSource = ds1.Tables["Table1"].DefaultView;

    con.Close();

    sql = "SELECT * From Table2";

    da2 = new System.Data.OleDb.OleDbDataAdapter(sql, con);

    con.Open();

    da2.Fill(ds2, "Table2");

    dataGridView2.DataSource = ds2.Tables["Table2"].DefaultView;

    con.Close();

    con.Dispose();

    colnames[0] = "class";

    colnames[1] = "command";

    colnames[2] = "parameter";

    colnames[3] = "return";

    colnames[4] = "delay";

    //dataGridView2.DataSource = new Mommo.Data.ArrayDataView(set2, colnames);

    }

    private void btnAdd_Click(object sender, EventArgs e)

    {

    con = new System.Data.OleDb.OleDbConnection();

    con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=C:/projectDb.mdb";

    string sql = "INSERT INTO table1(class,command ) VALUES( '" + comboBox1.Text + "','" + textBox1.Text + "')";

    OleDbCommand myCmd = new OleDbCommand(sql, con);

    try

    {

    con.Open();

    myCmd.ExecuteNonQuery();

    textBox1.Clear(); //clear after the value inserted into the database

    MessageBox.Show("Command Inserted", "Success Message");

    string sql2 = "SELECT * FROM Table1 ";

    da = new System.Data.OleDb.OleDbDataAdapter(sql2, con);

    DataSet dset = new DataSet();

    //da.TableMappings.Add("Table1", "command");

    da.Fill(dset, "Table1");

    //DataViewManager dviewmanager = new DataViewManager();

    //dviewmanager = dset.DefaultViewManager;

    //dataGridView1.DataSource = table1BindingSource;

    //da.Fill(ds1);

    dataGridView1.DataSource = dset.Tables["Table1"].DefaultView;

    }

    catch (Exception ex)

    {

    MessageBox.Show(ex.Message + "Double check with your entry","Error");

    }

    finally

    {

    con.Close();

    }

    }

    private void quitToolStripMenuItem_Click(object sender, EventArgs e)

    {

    if (MessageBox.Show("Are you sure you want to quit?", "Exit", MessageBoxButtons.YesNo) == DialogResult.Yes)

    {

    Application.Exit();

    }

    }

    private void btnInsert_Click(object sender, EventArgs e)

    {

    set2[i, 0] = comboBox2.Text;

    set2[i, 1] = comboBox3.Text;

    set2[i, 2] = comboBox4.Text;

    set2[i, 3] = textBox2.Text;

    set2[i, 4] = textBox3.Text;

    i++;

    dataGridView2.Refresh();

    }

    private void NavigateRecords()

    {

    DataRow dRow;

    for (int i = 0; i <5; i++)

    {

    dRow = ds1.Tables["Table1"].Rows[i];

    comboBox2.Items.Add(dRow.ItemArray.GetValue(0).ToString());

    comboBox3.Items.Add(dRow.ItemArray.GetValue(1).ToString());

    }

    }

    private void btnDelete_Click(object sender, EventArgs e)

    {

    con = new System.Data.OleDb.OleDbConnection();

    con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=C:/projectDb.mdb";

    string sql = "DELETE FROM Table1 WHERE command = '" + dataGridView1.SelectedRows + "'";

    OleDbCommand myCmd = new OleDbCommand(sql, con);

    try

    {

    DataSet dset = new DataSet();

    da = new System.Data.OleDb.OleDbDataAdapter(sql, con);

    con.Open();

    dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);

    da.Fill(dset,"Table");

    da.Update(dset);

    dataGridView1.DataSource = dset.Tables["Table1"];

    myCmd.ExecuteNonQuery();



    }

    catch (Exception ex)

    {

    MessageBox.Show(ex.Message, "Error");

    }

    finally

    {

    con.Close();

    }

    }



    private void btnDelete2_Click(object sender, EventArgs e)

    {

    dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);

    }

    private void button1_Click(object sender, EventArgs e)

    {

    // foreach (DataRow dr in DataTable.Rows)

    // {

    // This loop is running total of data table row

    // And you get the column value this way dr["cloumnName"]

    // then insert into database.

    //}

    }



    }

    }


    thanks in advance. =(

    • Edited by sam.astro Wednesday, November 12, 2008 9:36 AM
    • Moved by Michael Sun [MSFT]Microsoft employee Friday, November 14, 2008 7:06 AM Off Topic for Visual C# Language (Moved from Visual C# Language to Off-Topic Posts (Do Not Post Here))
    Wednesday, November 12, 2008 4:20 AM

Answers