473,396 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Error while updating, editing the Grid view and saving the changes to Database

I have a SaveButton through which I am trying to add a new row, edit the row and save the changes. I am able to edit the existing row in the GridView. The problem is when I try to add a new row the existing row is getting modified. If I use `dt.Rows.Add(dr)` then new row is getting added after editing the existing row and saving.

Below is my code:

Expand|Select|Wrap|Line Numbers
  1.     private void btnSave_Click_1(object sender, EventArgs e)
  2.         {
  3.             int MyId;
  4.             bool i = int.TryParse(txtID.Text, out MyId);
  5.             da = new SqlDataAdapter();
  6.             da.SelectCommand = new SqlCommand("select * from Measurement where ID = @ID",con);
  7.             da.SelectCommand.Parameters.AddWithValue("@ID",MyId);
  8.             SqlCommandBuilder cb = new SqlCommandBuilder(da);
  9.             da.Fill(ds, "Measurement");
  10.  
  11.             if (String.IsNullOrEmpty(txtCellNo.Text.Trim()))
  12.             {
  13.                 MessageBox.Show("Please enter Cell Number");
  14.             }
  15.             else
  16.             {
  17.                 try
  18.                 {  
  19.                     if (ds.Tables["Measurement"].Rows.Count < 1 )
  20.                     {
  21.                         dt.Rows.Add();               
  22.                     }
  23.                     else
  24.                     {
  25.                         dr = ds.Tables["Measurement"].Rows[0];
  26.                         dr["CellNumber"] = txtCellNo.Text.Trim();
  27.                         dr["FirstName"] = txtFirstName.Text;
  28.                         dr["LastName"] = txtLastName.Text;
  29.                         dr["Shirt"] = txtShirt.Text;
  30.                         dr["Pant"] = txtPant.Text;
  31.                         dr["DueDate"] = txtDueDate.Text;
  32.                         dr["Date"] = txtDate.Text;
  33.  
  34.                         cb.GetUpdateCommand();
  35.                         da.Update(ds, "Measurement");
  36.                     }
  37.                 }
  38.                 catch (Exception ex)
  39.                 {
  40.                     MessageBox.Show(ex.Message);
  41.                 }
  42.             }
  43.         }
Note: I got the values from Gridview to Text Boxes using Gridview Selection changed event.
Feb 6 '13 #1
0 1013

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: sonali_aurangabadkar | last post by:
i want to edit whole grid on singel button click
5
by: Adam Sandler | last post by:
Hello, Before I begin, here's some background to the problem. I'm working on a web app which deals with maps. Via a browser, the users can zoom in, zoom out, and pan around a map of their...
0
by: nagarjunaReddy | last post by:
hi all please send me the code to update the data in the database through the grid view whithout using any controls like sql datasource thanks nagarjunareddy
3
by: Andrew Robinson | last post by:
I have a Grid View and associated Object Data Source control. The ODS interface with a custom data source that returns a List<MyEntity>. I am only referencing some of the columns on the GridView....
1
by: usaccess | last post by:
Hi I have a sql data source that is pointing to a table which is accessed through a grid view. My goal is to have one of the columns in this table be populated as a drop down list from...
3
by: thiagarajanrsbe | last post by:
Dear All, I have one problem in gridview.Im using C# code and ADO connection. In the grid view ,data is not updated in the update link....Please hep me to solve this issue......... Im use...
1
by: vibhashin | last post by:
I hve extracted a coloumn in combo box from database. I want that when i select a particular value from the combo box it should display the related details of the selected value in the combo box in...
0
by: sajitk | last post by:
Guys, I have a grid view where i use the option of Editing a record. When i click on the edit button, Update and Cancel button appears. But when i click on the Update link, the event is not...
6
by: AnagJohari | last post by:
actually i want to edit the row of an grid view by code behind not using any sql data source. u know that that show edit button property in a grid view when we set this property to true &...
0
by: michaelTun | last post by:
how can I retrieve data from oracle database to Visual Studio -2005 asp.net grid view. connection.Open() string sql = "select * from departments where department_id < 60" cmd = new...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.