简体   繁体   中英

c# add , update, delete not working in DGV and db

my db and DGV it's not updated when i add new record !

表格设计

where is the wrong ? I'm trying a to solve this problem but is still (add, delete, update) i think all of them it has the same problem maybe the dataset , need some help please

// my dataset name: emp2 //my database table name: emp_table

    private void btnAdd_Click(object sender, EventArgs e)
    {
        try
        {
            Add();
            Reset();
            this.BindGrid();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

    private void brnUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            if (string.IsNullOrEmpty(txtId.Text) || string.IsNullOrEmpty(txtName.Text) || string.IsNullOrEmpty(txtDep.Text) || string.IsNullOrEmpty(txtAge.Text) || string.IsNullOrEmpty(txtQualification.Text) || string.IsNullOrEmpty(txtMob.Text) || string.IsNullOrEmpty(txtEmail.Text)) { MessageBox.Show("Please Input data."); return; }
            Update(lblId.Text);
            Reset();
            this.BindGrid();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }

    }

    private void btnDelete_Click(object sender, EventArgs e)
    {
        try
        {
            if (string.IsNullOrEmpty(txtId.Text) || string.IsNullOrEmpty(txtName.Text) || string.IsNullOrEmpty(txtDep.Text) || string.IsNullOrEmpty(txtAge.Text) || string.IsNullOrEmpty(txtQualification.Text) || string.IsNullOrEmpty(txtMob.Text) || string.IsNullOrEmpty(txtEmail.Text)) { MessageBox.Show("Please Input data."); return; }
            Delete(lblId.Text);
            Reset();
            this.BindGrid();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }


    }

    private void dgvOldData_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        if (emp_tableDataGridView.Rows.Count > 0 && e.RowIndex != -1)
        {
            if (emp_tableDataGridView.Rows[e.RowIndex].Cells[0].Selected)
            {
                txtId.Text = emp_tableDataGridView.Rows[e.RowIndex].Cells[1].Value.ToString();
                txtName.Text = emp_tableDataGridView.Rows[e.RowIndex].Cells[2].Value.ToString();
                txtDep.Text = emp_tableDataGridView.Rows[e.RowIndex].Cells[3].Value.ToString();
                txtAge.Text = emp_tableDataGridView.Rows[e.RowIndex].Cells[4].Value.ToString();
                txtQualification.Text = emp_tableDataGridView.Rows[e.RowIndex].Cells[5].Value.ToString();
                txtMob.Text = emp_tableDataGridView.Rows[e.RowIndex].Cells[6].Value.ToString();
                txtEmail.Text = emp_tableDataGridView.Rows[e.RowIndex].Cells[7].Value.ToString();
                lblId.Text = emp_tableDataGridView.Rows[e.RowIndex].Cells[4].Value.ToString();

                btnDelete.Enabled = true;
                brnUpdate.Enabled = true;
                btnAdd.Enabled = false;

            }
        }
    }

    private static void FillDataInDataset(out DataSet oldData, out DataRow dr)
    {
        oldData = new DataSet();
        dr = null;
        sda.Fill(oldData);
    }

    private void BindGrid()
    {
        DataSet _ds = new DataSet();
        sda.Fill(_ds);
        if (_ds.Tables.Count > 0)
        {
            emp_tableDataGridView.DataSource = _ds.Tables[0];
        }

    }

    private void Add()
    {
        DataSet oldData;
        DataRow dr;
        FillDataInDataset(out oldData, out dr);
        //create new row assign value to it.
        dr = oldData.Tables[0].NewRow();
        if (string.IsNullOrEmpty(txtId.Text) || string.IsNullOrEmpty(txtName.Text) || string.IsNullOrEmpty(txtDep.Text) || string.IsNullOrEmpty(txtAge.Text) || string.IsNullOrEmpty(txtQualification.Text) || string.IsNullOrEmpty(txtMob.Text) || string.IsNullOrEmpty(txtEmail.Text)) { MessageBox.Show("Please Input data."); return; }
        dr["emp_id"] = txtId.Text.Trim();
        dr["name"] = txtName.Text.Trim();
        dr["dep"] = txtDep.Text.Trim();
        dr["emp_deg"] = txtAge.Text.Trim();
        dr["emp_pos"] = txtQualification.Text.Trim();
        dr["phone"] = txtMob.Text.Trim();
        dr["email"] = txtEmail.Text.Trim();
        oldData.Tables[0].Rows.Add(dr);
        sda.Update(oldData);

    }

    private void Update(string id)
    {
        DataSet oldData;
        DataRow dr;
        FillDataInDataset(out oldData, out dr);
        //Here get record of specified id.
        DataRow[] tempdata = oldData.Tables[0].AsEnumerable().Where(p => p["index"].ToString() == id).ToArray();
        if (tempdata.Length > 0)
        {
            dr = tempdata[0];
            dr["emp_id"] = txtId.Text.Trim();
            dr["name"] = txtName.Text.Trim();
            dr["dep"] = txtDep.Text.Trim();
            dr["emp_deg"] = txtAge.Text.Trim();
            dr["emp_pos"] = txtQualification.Text.Trim();
            dr["phone"] = txtMob.Text.Trim();
            dr["email"] = txtEmail.Text.Trim();

        }
        sda.Update(oldData);
    }

    private void Delete(string id)
    {
        DataSet oldData;
        DataRow dr;
        FillDataInDataset(out oldData, out dr);
        //Here get record of specified id.
        DataRow[] tempdata = oldData.Tables[0].AsEnumerable().Where(p => p["index"].ToString() == id).ToArray();
        if (tempdata.Length > 0)
        {
            dr = tempdata[0];
            dr["emp_id"] = txtId.Text.Trim();
            dr["name"] = txtName.Text.Trim();
            dr["dep"] = txtDep.Text.Trim();
            dr["emp_deg"] = txtAge.Text.Trim();
            dr["emp_pos"] = txtQualification.Text.Trim();
            dr["phone"] = txtMob.Text.Trim();
            dr["email"] = txtEmail.Text.Trim();
            dr.Delete();
        }
        sda.Update(oldData);

    }

    private void Reset()
    {
        txtId.Text = string.Empty;
        txtName.Text = string.Empty;
        txtDep.Text = string.Empty;
        txtAge.Text = string.Empty;
        txtQualification.Text = string.Empty;
        txtMob.Text = string.Empty;
        txtEmail.Text = string.Empty;
        btnDelete.Enabled = false;
        brnUpdate.Enabled = false;
        btnAdd.Enabled = true;
    }

启用S​​ql事件探查器并在运行应用程序时获取查询。这将有助于解决问题

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