How to retrieve data from database to frontend, each field in diff txtbox #1 1 Minute Ago | Add to Haimanti's Reputation | Flag Bad Post
hi

Hi i'm new to coding, with basis of theritical knowledge, i tried writing code for data insert n retrieval..
the code i posted last time may be confusing, but its because i'm not comfortable with syntaxes still.

pls guidde with with the same.

Program requirement is:

The data in data grid should display "edit" and "delete" options when mouse is right clicked on it. The user when selects edit on any data, the whole related record should be displayed on new form where user may edit one or more field of the record n update the same in Db.

On selection of "delete' the user should have a msg box flashing if he is sure to delete the record. If "ok" the record may be deleted, if "cancel" the datagrid should be again dispalyed.

Pls guide me as to how do i proceed. I'm stuck on thi ssingle part since long

The code i've trid is as below:

private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //frmInsertCustData frmInsertCustData = new frmInsertCustData();
            //frmInsertCustData.Show();
            //DataSet ds = new DataSet();
            //SqlDataAdapter da = new SqlDataAdapter("Select * from customer order by id", conn1);
            //da.Fill(ds, "customer");
            //row = e.RowIndex;
            //col = e.ColumnIndex;

            

        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //SqlDataReader dr1 = new SqlDataReader();
            //row = e.RowIndex;
            //col = e.ColumnIndex;


           //SqlDataAdapter da1 = new SqlDataAdapter("Delete from customer where @parameter =@value");
           // if (MessageBoxDefaultButton.Button1 = true)
           //     da1.ExecuteNonQuery();
           // else if (MessageBoxDefaultButton.Button2 = true)
           //     dataGridView2.Show();
  
            MessageBox.Show("Do you want to delete the record","Delete",MessageBoxButtons.OKCancel);
            DialogResult dialrslt = new DialogResult();
           frmInsertCustData myfrm = new frmInsertCustData();
            dialrslt = myfrm.ShowDialog();

            if (dialrslt == DialogResult.OK)
            {
             
                
                MessageBox.Show("Deleted Record!!");
            }

            else if (dialrslt == DialogResult.Cancel)
                MessageBox.Show("Action Reverted");

            
        }

Thank you

Dani AI

Generated

This thread is about wiring a DataGrid (or DataGridView) so a right‑click shows Edit/Delete and the selected record is loaded into a form for editing. If this is a web app, follow : use client‑side JavaScript confirms and postbacks or AJAX for server actions. The rest below assumes a WinForms DataGridView.

A reliable flow for WinForms:

  • On right‑click, select the row under the cursor and show the ContextMenuStrip so the user knows which row is targeted.
  • Store or read the row's primary key (keep the ID as a hidden/bound column). As suggested, you must use that PK for any update/delete.
  • For Edit, open a modal edit form and pass the ID. Have that form SELECT the record by ID, populate the textboxes, and on Save run a parameterized UPDATE. Return DialogResult.OK so the caller can refresh or update the binding.
  • For Delete, show a confirmation dialog. If confirmed, run a parameterized DELETE for the ID and then remove the row from the BindingSource or requery the table.

Example patterns (original, minimal):

// right-click selection (DataGridView.CellMouseDown)
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
    if (e.Button != MouseButtons.Right || e.RowIndex < 0) return;
    dataGridView1.ClearSelection();
    dataGridView1.Rows[e.RowIndex].Selected = true;
    dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[0];
    contextMenuStrip1.Show(Cursor.Position);
}
// safe update/delete using parameters
using (var cn = new SqlConnection(connString))
using (var cmd = new SqlCommand("DELETE FROM Customers WHERE Id = @id", cn))
{
    cmd.Parameters.AddWithValue("@id", id);
    cn.Open();
    cmd.ExecuteNonQuery();
}
bindingSource.RemoveCurrent(); // or re-fill DataTable

Practical cautions: always use parameterized commands to avoid SQL injection, wrap connections/commands in using blocks, catch exceptions and show helpful errors, and consider async DB calls or background threads so the UI stays responsive.

Recommended Answers

All 3 Replies

What compiler and operating system? I always used ODBC for database access and have no idea what SqlDataReader is.

[edit]Nevermind -- that thread was in the wrong forum [/edit]

Is this a web or windows app? coz if it a web message box isn't going to work you'll need js for the messages

Here is how you ask the user a question.. you have a lot of un-needed code.

private void simpleButton1_Click(object sender, EventArgs e)
    {
      if (MessageBox.Show("Delete record?", this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
        return;
      //Delete
    }

As far as deleting the data from the database you need to determine the currently selectly row's PRIMARY KEY. This depends on how you are presenting the data. Can you get it off the grids active row or from a bindingSource?

After you have the ID you will issue an SQL command to delete the record then remove it from the datasource's row collection.

Please post more information on how your form is setup.

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.