473,765 Members | 1,956 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

1 New Member
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 1026

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

Similar topics

1
422
by: sonali_aurangabadkar | last post by:
i want to edit whole grid on singel button click
5
1623
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 geographic region. The users can also click a button called "identify". When they do this the mouse pointer changes to a crosshair and when they click on the map, the polygon under the cursor is captured and passed in a query to the database......
0
858
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
1915
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. When editing a row within the Grid View, the columns that are not referenced, are being set to null. Is this by design or am I missing something else somewhere?
1
1504
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 another table. Then I edit the template fields and choose another data source for the template field.
3
1111
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 grid_Row Editing Event to edit the grid.......which command should use to update the data........and send me sample update code in ADO connection
1
4629
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 the data grid view from the database.
0
1574
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 generated. I am not using templates, i am just using bounded fields. I have written the update program. Can anyone tell me the reason why this event is not fired. I have a primary key Sno, which is mentioned in the DatakeyName in the property box. ...
6
2067
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 & simply connect it to the sql data source we use a update command property in sql data source by doing all this we create a such type of data grid in which edit button is display in every row when u click on edit button then the corresponding row is...
0
1405
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 OracleCommand(sql, conn) cmd.CommandType = CommandType.Text;
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10007
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9957
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9835
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8832
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7379
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5276
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.