简体   繁体   中英

Updating a record in ms-access database using c# unknown error

I wanted to update a record in my ms-access database from a form using c# but nothing happens when I clicked my update button. There are no errors and no exceptions, it's just.. it doesn't do anything. I am puzzled and nearly going mad trying to figure out what is wrong. Please help me, I am new to C#.

My form has four textboxes and buttons (update, add, delete, and clear) and a listview. So far, I got the "add" and "clear" okay, the "delete" is not yet worked, and I am currently having trouble in the "update".

The listview acts as a table, invisibly holds the ID (width 0) of an employee while displaying the firstname and lastname of an employee from my database.

Each time a record is selected from the listview, the computer will get the invisible ID number of the selected row, and will display the corresponding data associated with the ID to the textboxes (firstname, middlename, lastname, address, position)

namespace WindowsFormsApp1
{


public partial class Form1 : Form
{

    string employeeID; //global variable 
    public void LvRefresh()
    {

        //this method is used to load records from the database
        //to the listview, it is also used to REFRESH the records of the listview.

        listView1.Items.Clear();
        listView1.View = View.Details;
        OleDbConnection con = new OleDbConnection();
        con.ConnectionString = @"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\\Users\\genesis\\Documents\\Database1.accdb";
        con.Open();

        OleDbCommand cmdEmp = new OleDbCommand("Select ID,FN,LN from Employees", con);
        OleDbDataReader rdrEmp = cmdEmp.ExecuteReader();

        if (rdrEmp.HasRows)
        {

            while (rdrEmp.Read())
            {

                ListViewItem listitem = new ListViewItem(rdrEmp["ID"].ToString());
                listitem.SubItems.Add(rdrEmp["FN"].ToString());
                listitem.SubItems.Add(rdrEmp["LN"].ToString());
                listView1.Items.Add(listitem);

            }

        }
        con.Close();
        rdrEmp.Close();
        cmdEmp.Dispose();
    }



    public Form1()
    {

        InitializeComponent();
        LvRefresh(); //load the ID, FN, MN from the database to the listview
    }

    private void button2_Click(object sender, EventArgs e)
    {

        //clear the textboxes
        textADRS.Clear();
        textFN.Clear();
        textMN.Clear();
        textLN.Clear();
        textPOS.Clear();
    }

    private void buttonSUB_Click(object sender, EventArgs e)
    {

        //add records

        string Adrs = textADRS.Text;
        string Fname = textFN.Text;
        string Mname = textMN.Text;
        string Lname = textLN.Text;
        string Pos = textPOS.Text;

        try
        {

            OleDbConnection con = new OleDbConnection();
            con.ConnectionString = @"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\\Users\\genesis\\Documents\\Database1.accdb";
            OleDbCommand cmd = new OleDbCommand("Insert into Employees (FN,MN,LN,[Address],[Position]) Values (@FirstName,@MidName,@LastName,@Address,@Position)", con);
            cmd.Parameters.Add(new OleDbParameter("@FirstName", Fname));
            cmd.Parameters.Add(new OleDbParameter("@MidName", Mname));
            cmd.Parameters.Add(new OleDbParameter("@LastName", Lname));
            cmd.Parameters.Add(new OleDbParameter("@Address", Adrs));
            cmd.Parameters.Add(new OleDbParameter("@Position", Pos));
            con.Open();
            cmd.ExecuteNonQuery();
            MessageBox.Show("Record Submitted", "Nice!");
            con.Close();
            cmd.Dispose();
            LvRefresh(); //refresh listview

        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    private void listView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        //codes that are responsible for the reflecting of records to the textboxes

        try
        {

            ListViewItem item = listView1.SelectedItems[0];
            employeeID = item.Text; //update the value of global variable employeeID
            textBox1.Text = employeeID;

            OleDbConnection con = new OleDbConnection();
            con.ConnectionString = @"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\\Users\\genesis\\Documents\\Database1.accdb";
            con.Open();

            OleDbCommand cmdEmp = new OleDbCommand("Select * from Employees where ID = @empID", con);
            cmdEmp.Parameters.Add("@empID", employeeID);
            OleDbDataReader rdrEmp = cmdEmp.ExecuteReader();

            if (rdrEmp.HasRows)
            {

                while (rdrEmp.Read()) 
                {

                    textFN.Text = rdrEmp["FN"].ToString();
                    textMN.Text = rdrEmp["MN"].ToString();
                    textLN.Text = rdrEmp["LN"].ToString();
                    textADRS.Text = rdrEmp["Address"].ToString();
                    textPOS.Text = rdrEmp["Position"].ToString();

                }

            }
            con.Close();
            rdrEmp.Close();
            cmdEmp.Dispose();
        }

        catch (Exception)
        {

        }

    }

THE PROBLEM

    private void buttonUpdate_Click(object sender, EventArgs e)
    {
        string Adrs = textADRS.Text;
        string Fname = textFN.Text;
        string Mname = textMN.Text;
        string Lname = textLN.Text;
        string Pos = textPOS.Text;

        try
        {

            OleDbConnection con = new OleDbConnection();
            con.ConnectionString = @"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\\Users\\genesis\\Documents\\Database1.accdb";
            con.Open();

            OleDbCommand cmdEmp = new OleDbCommand("Update Employees set FN = @FirstName, MN = @MidName, LN = @LastName, [Address] = @Address, [Position] = @Pos where ID = @empID", con);
            cmdEmp.Parameters.Add(new OleDbParameter("@empID", employeeID));
            cmdEmp.Parameters.Add(new OleDbParameter("@FirstName", Fname));
            cmdEmp.Parameters.Add(new OleDbParameter("@MidName", Mname));
            cmdEmp.Parameters.Add(new OleDbParameter("@LastName", Lname));
            cmdEmp.Parameters.Add(new OleDbParameter("@Address", Adrs));
            cmdEmp.Parameters.Add(new OleDbParameter("@Position", Pos));
            cmdEmp.ExecuteNonQuery();

            MessageBox.Show("Record Updated!", "Nice!");
            con.Close();
            cmdEmp.Dispose();
            LvRefresh(); //refresh listview
        }

        catch (Exception error)
        {
            MessageBox.Show(error.Message);
        }

    }
}
}

NOTE: THE "RECORD UPDATED" MESSAGEBOX IN THE UPDATE FUNCTIONALITY IS DISPLAYED DESPITE NOT ACTUALLY UPDATING ANYTHING.

Add your parameters in the order they appear in your sql statment. Access needs them created in the order they appear.

 OleDbCommand cmdEmp = new OleDbCommand("Update Employees set FN = @FirstName, MN = @MidName, LN = @LastName, [Address] = @Address, [Position] = @Pos where ID = @empID", con);
        cmdEmp.Parameters.Add(new OleDbParameter("@FirstName", Fname));
        cmdEmp.Parameters.Add(new OleDbParameter("@MidName", Mname));
        cmdEmp.Parameters.Add(new OleDbParameter("@LastName", Lname));
        cmdEmp.Parameters.Add(new OleDbParameter("@Address", Adrs));
        cmdEmp.Parameters.Add(new OleDbParameter("@Position", Pos));
        cmdEmp.Parameters.Add(new OleDbParameter("@empID", employeeID));
       cmdEmp.ExecuteNonQuery();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM