Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9244383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:00:59+00:00 2026-06-18T09:00:59+00:00

I have a gridview and Textboxes in Form1. When Form1 loads it will load

  • 0

I have a gridview and Textboxes in Form1. When Form1 loads it will load the data into Gridview from database. I have a selectionChanged event of Gridview from where the data goes into textboxes and Now my problem is I want to edit them in Textboxes and save them to database, but When I click on save button it is creating a new record in the database and gridview. How to fix this ?

Below is my code for SaveButton:

private void btnSave_Click_1(object sender, EventArgs e)
{
    DataRow dr = dt.NewRow();
    da = new SqlDataAdapter("select * from Measurement", con);
    SqlCommandBuilder cb = new SqlCommandBuilder(da);
    dr["CellNumber"] = txtCellNo.Text.Trim();
    dr["FirstName"] = txtFirstName.Text;
    dr["LastName"] = txtLastName.Text;
    dr["Shirt"] = txtShirt.Text;
    dr["Pant"] = txtPant.Text;
    dr["DueDate"] = txtDueDate.Text;
    dr["Date"] = txtDate.Text;
    if (dr["CellNumber"] == "")
    {
        MessageBox.Show("Please enter Cell Number");
    }
    else if (dr["CellNumber"] != "")
    {
        dr = dt.Select(" ID = " + txtID.Text)[0]; //updated here
    }

    try
    {
        da.Update(ds, "Measurement");
    }
    catch (DBConcurrencyException ex)
    {
        MessageBox.Show(ex.Message);
    }
}

Code for Gridview:

private void dgv_SelectionChanged_1(object sender, EventArgs e)
{
    if (dgv.SelectedRows.Count > 0)
    {

        foreach (DataGridViewRow row in dgv.SelectedRows)
        {
            //Send the first cell value into textbox'
            txtLastName.Text = row.Cells["LastName"].Value.ToString();
            txtFirstName.Text = row.Cells["FirstName"].Value.ToString();
            txtCellNo.Text = row.Cells["CellNumber"].Value.ToString();
            txtDate.Text = row.Cells["Date"].Value.ToString();
            txtDueDate.Text = row.Cells["DueDate"].Value.ToString();
            txtPant.Text = row.Cells["Pant"].Value.ToString();
            txtShirt.Text = row.Cells["Shirt"].Value.ToString();
            txtID.Text = row.Cells["ID"].Value.ToString();
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:01:00+00:00Added an answer on June 18, 2026 at 9:01 am

    da.Update(ds, "Measurement"); is inserting a new record because the RowState property of the row you are attempting to update is set to Added since you are assigning the cell values to a new row. You need to update the values of an existing row. Check DbDataAdapter.Update Documentation :

    When an application calls the Update method, the DbDataAdapter
    examines the RowState property, and executes the required INSERT,
    UPDATE, or DELETE statements iteratively for each row, based on the
    order of the indexes configured in the DataSet

    Try this :

    private void btnSave_Click_1(object sender, EventArgs e)
    {
        da = new SqlDataAdapter();
        da.SelectCommand = new SqlCommand("select * from Measurement where ID = @ID",con);
        da.SelectCommand.Parameters.AddWithValue("@ID",int.Parse(txtID.Text));
        SqlCommandBuilder cb = new SqlCommandBuilder(da);
        da.Fill(ds, "Measurement");        
    
        if (String.IsNullOrEmpty(txtCellNo.Text.Trim()))
        {
            MessageBox.Show("Please enter Cell Number");
        }
        else
        {
            try
            {
               dr = ds.Tables["Measurement"].Rows[0];
    
               dr["CellNumber"] = txtCellNo.Text.Trim();
               dr["FirstName"] = txtFirstName.Text;
               dr["LastName"] = txtLastName.Text;
               dr["Shirt"] = txtShirt.Text;
               dr["Pant"] = txtPant.Text;
               dr["DueDate"] = txtDueDate.Text;
               dr["Date"] = txtDate.Text;
    
               cb.GetUpdateCommand();
               da.Update(ds, "Measurement");
            }
            catch (Exception ex)
            {
               MessageBox.Show(ex.Message);
            }
        }
    
    
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have data that gets displayed in gridview. If I'm pulling it from one
Have a gridview control which will show X amount of rows. I want to
i am trying to load images from the northwind database (categories table, images that
I have a Gridview, with textboxes in templatefields, where i dynamically add columns to
I have gridview that is loaded from another aspx page after an ajax call,
I have this GridView that I'm trying to bind data to and I need
I have a gridview in an ASP.NET site. There's a TemplateField right now that
I have a GridView with textboxes in its cells. I need to call a
I have a GridView in Which I have four TextBoxes in the Template Field.
I have a gridview in which i have two textboxes. The senarios is such

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.