473,662 Members | 2,382 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# Web-App: Update Database from GridView using DataSets

26 New Member
Hi
I m looking for some sample example (Code) for my following scenario

I have a Grid View which populates data from database say from Product Tables
using dataset and dataadapter

This Grid has three columns of productid ,ProductName ,Unit Price (THis is editable text box field). There is checkbox column. (Paging is enabled there are 2 pages )

There are three buttons say update and delete
Now suppose I select 5 checkboxes and
1) click update then watever changes I have done should be reflected in database
2)click delete then those five rows should be deleted in database.
and my page should be refreshed.

(ALL THESE SHOULD BE DONE WITHOUT USING SQLDATASOURCE OR ANY OTHER INBUILT .NET stuff)

Any help will be greatly appreciated
Dec 16 '07 #1
10 4102
kenobewan
4,871 Recognized Expert Specialist
What have you done so far? Please read the faqs you are not following the site rules. Thanks.
Dec 16 '07 #2
gagonm
26 New Member
I didn't get u.

I guess you are saying me to put code watever I have done till now??



What have you done so far? Please read the faqs you are not following the site rules. Thanks.
Dec 17 '07 #3
Shashi Sadasivan
1,435 Recognized Expert Top Contributor
Not necessary that it should be the code.

But are you asking us to make that for you? or are you asking for help in that?

We can help you if you are facing issues with your current work or if something isint working, Please do mention what you have started with that so that we can guide you in the right direction.

PS: Do not enter text in Caps lock, this is against the site regulations!
Dec 17 '07 #4
gagonm
26 New Member
Hi

Below is the part of code .
Here in BtnSubmit_Click Event I m able to find which all check box was clicked and wat value was changed but after this I need help how to update database with these values using a gud design.
Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="C#"  Trace="false" Debug="true" AutoEventWireup="true" CodeFile="GridTemplate.aspx.cs" Inherits="GridTemplate" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml" >
  6. <head runat="server">
  7.     <title>Untitled Page</title>
  8. </head>
  9. <body>
  10.     <form id="form1" runat="server">
  11.     <div>
  12.         <asp:GridView ID="GridBind" runat="server"  AutoGenerateColumns="False"  style="left: 261px; position: absolute; top: 9px" Height="294px">
  13.           <Columns>
  14.           <asp:TemplateField HeaderText="Select">
  15.             <ItemTemplate>
  16.               <asp:CheckBox ID="chek"   runat="server"  />
  17.             </ItemTemplate>
  18.  
  19.           </asp:TemplateField> 
  20.            <asp:TemplateField HeaderText="Product ID" SortExpression="ProductID">
  21.              <ItemTemplate >
  22.                <asp:label ID="ProductId" Width="40"  Text='<%#Eval("ProductID")%>' runat="server"/>                
  23.             </ItemTemplate>   
  24.             </asp:TemplateField>
  25.               <asp:TemplateField HeaderText="Unit Price" SortExpression="UnitPrice">
  26.                 <ItemTemplate>
  27.                    <asp:label ID="UnitPrice" Width="40"  Text='<%#bind("UnitPrice")%>' runat="server"/>                
  28.                 </ItemTemplate>           
  29.                 <EditItemTemplate>
  30.                    <asp:TextBox ID="UnitPrice" Width="40"  Text='<%#bind("UnitPrice")%>' runat="server"/>                
  31.                 </EditItemTemplate>
  32.              </asp:TemplateField>
  33.            <asp:TemplateField HeaderText="Product Name" SortExpression="ProductName">
  34.              <ItemTemplate>
  35.                <asp:TextBox ID="ProductName" Width="40"  Text='<%#bind("ProductName")%>' runat="server"/>                
  36.              </ItemTemplate> 
  37.             </asp:TemplateField>
  38.           </Columns>                  
  39.         </asp:GridView>
  40.         <br />
  41.         <br />
  42.         <asp:Button ID="btnsubmit" runat="server" OnClick="btnsubmit_Click" Style="left: 338px;
  43.             position: absolute; top: 341px" Text="submit" />
  44.  
  45.     </div>
  46.     </form>
  47. </body>
  48. </html>
  49.  
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Web;
  7. using System.Web.Security;
  8. using System.Web.UI;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.WebControls.WebParts;
  11. using System.Web.UI.HtmlControls;
  12. using System.Data.SqlClient;
  13.  
  14.  
  15. public partial class GridTemplate : System.Web.UI.Page
  16. {
  17.     private DataView dv;
  18.     protected void Page_Load(object sender, EventArgs e)
  19.     {
  20.         if (!Page.IsPostBack)
  21.             DataBindingGrid();
  22.     }
  23.     private void DataBindingGrid()
  24.     {
  25.  
  26.         string Conn1 = @"Data Source=XYZ\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True";
  27.         SqlConnection conn = new SqlConnection(Conn1);    
  28.         SqlCommand cmd = new SqlCommand();
  29.         cmd.CommandType = CommandType.Text;
  30.         cmd.CommandText = "Select top 20 ProductID,ProductName,UnitPrice from Products";
  31.         cmd.Connection = conn;
  32.         SqlDataAdapter sqda = new SqlDataAdapter(cmd);
  33.         DataSet ds = new DataSet();
  34.         sqda.Fill(ds, "Product");    
  35.         GridBind.DataSource = ds.Tables["Product"];
  36.         GridBind.DataBind();
  37.     }
  38.  
  39.     protected void btnsubmit_Click(object sender, EventArgs e)
  40.     {
  41.         List<int> rowid = new List<int>();
  42.         List<string> unitprice = new List<string>();
  43.         foreach (GridViewRow row in GridBind.Rows)
  44.         {
  45.             CheckBox chk1 = (CheckBox)row.Cells[0].FindControl("chek") as CheckBox;
  46.             if (chk1.Checked)
  47.             {
  48.                 rowid.Add(row.RowIndex);
  49.                 TextBox txtb = (TextBox)row.Cells[3].FindControl("ProductName") as TextBox;
  50.  
  51.                 unitprice.Add(txtb.Text);
  52.             }            
  53.         }
  54.     }
  55. }
a
Dec 17 '07 #5
Frinavale
9,735 Recognized Expert Moderator Expert
You know how you grabbed data from the database to populate your GridView?
It's basically the same thing for when you want to update your database.
Just connect to it and update the row that's been edited with the information the user provided....

For more help with using a database, please see How to use a database in your program.

-Frinny
Dec 17 '07 #6
gagonm
26 New Member
Here Once I click submit then how can I can use dataset or datable and there different properties to find out which rows were updated and then use them to update my database.

I only know how to these updates using ExecuteNonQuery on command objects but never used dataset or dataadapter.
I m very new with disconnected architecture of ADO .NET especially updation part.



You know how you grabbed data from the database to populate your GridView?
It's basically the same thing for when you want to update your database.
Just connect to it and update the row that's been edited with the information the user provided....

For more help with using a database, please see How to use a database in your program.

-Frinny
Dec 18 '07 #7
Frinavale
9,735 Recognized Expert Moderator Expert
Here Once I click submit then how can I can use dataset or datable and there different properties to find out which rows were updated and then use them to update my database.

I only know how to these updates using ExecuteNonQuery on command objects but never used dataset or dataadapter.
I m very new with disconnected architecture of ADO .NET especially updation part.
Do you have a column in your GridView for an "edit button"?
When the user clicks the edit button they're able to make changes to the data within the GridView. Once they've finished they click "save" or "update" (or whatever the button says) and this raises a RowUpdated event. You should implement the method that handles this event.

See the GridView class for more details, examples, and how-tos.

See the DataSet Class for more information on how to use a DataSet. And take a look at Using DataSets in ADO.NET.

Cheers!

-Frinny
Dec 18 '07 #8
gagonm
26 New Member
hi
In my case I have to update some (ex 3 to 4) rows data at once on click of submit(which is not part of GridView control on form ) button on form.
I m not using Edit and update button for individual button .Here updation will be done for all selected at once.


Thanks
Gagan
Dec 19 '07 #9
Frinavale
9,735 Recognized Expert Moderator Expert
hi
In my case I have to update some (ex 3 to 4) rows data at once on click of submit(which is not part of GridView control on form ) button on form.
I m not using Edit and update button for individual button .Here updation will be done for all selected at once.


Thanks
Gagan
Did you take a look at the link I gave you on using DataSets in ADO.NET?
On that site you would have found a link that lead you to Updating Data Sources with DataAdapters. There is an example there on how to use your DataSet to update the database.

-Frinny
Dec 19 '07 #10

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

Similar topics

0
2529
by: Phillip J. Eby | last post by:
PEP: 333 Title: Python Web Server Gateway Interface v1.0 Version: $Revision: 1.1 $ Last-Modified: $Date: 2004/08/27 17:30:09 $ Author: Phillip J. Eby <pje at telecommunity.com> Discussions-To: Python Web-SIG <web-sig at python.org> Status: Draft Type: Informational Content-Type: text/x-rst Created: 07-Dec-2003
9
2997
by: Marina Anufreichik | last post by:
Hi, After deploymnet web application on web server I can access page on local machine and login fine but when I'm trying to access web site from remote machine I can see login page, but when I'm trying to login with correct credentials it give me error: Server Error in '/PDVMgr' Application. ----------------------------------------------------------------------------
10
1864
by: Jeremy Ames | last post by:
I have created a web application that uses a custom control and a web service on my local system. I have gotten all of the bugs worked out on my local system and now it is time to move it to the sever. I have tried copying all of the files and folders within my application folder. I have even tried building a web setup and installing it on the server. The problem that I am having is that my web service is still looking for the web service...
0
2153
by: Diego F. | last post by:
I've been days with that. I'm trying to work with web services sending and returning objects, and the web service must store some objects. - My first try (the most obvious in my opinion) was to use a class contained in a extern dll so the web service and the application can use it and send or return via web services: public customObject myWebMethod(customObject obj); . I saw that web services can't deal with this cause the WS creates...
16
3152
by: C. Woody Butler | last post by:
I have a strange problem with a website and a web service. They're both on the same server, running Windows server 2003. The website is set up as the default website for the server. I've got the same code running in several different environments, it just so happens this is the production server we're having problems with. Not ideal, typically. What happens is this.
0
1177
by: Baruaa | last post by:
hi i m asheesh , can any on ehelp me about this error. Line 57: --> Line 58: <httpHandlers> Line 59: <add verb="*" path="*.vb" type="System.Web.HttpNotFoundHandler,System.Web" />
6
2358
by: Ruslan | last post by:
Hello, I have to project: one ASN.NET project and another - Web Service in the same solution. I want to use the same web.config and global.asax files. Does it possible?
0
2089
by: Erick Lopez | last post by:
When I send my web page to browser in ouput windows recibe this message and the web page the error BC32400 Please Help me Auto-attach to process ' aspnet_wp.exe' on machine 'TABLET' succeeded. 'DefaultDomain': Loaded 'c:\windows\microsoft.net\framework\v1.1.4322\mscorlib.dll', No symbols loaded. 'DefaultDomain': Loaded 'c:\windows\assembly\gac\system.web\1.0.5000.0__b03f5f7f11d50a3a\system.web.dll', No symbols loaded. 'DefaultDomain':...
7
6526
by: Jonas | last post by:
Hi. I'm trying to develop a web custom control that uses a programmatically created treeview. My problem is that I get an exception when I try to render the control. With properties do I have to assign to get this working. Here is some sample code: namespace WebControlLibrary1 {
2
7391
by: =?Utf-8?B?YW5vbg==?= | last post by:
I am not sure if this is the right forum. Environment : Windows server 2008, IIS 7.0 I get the 'Could not load the file or assembly 'blowery.web.httpCompress' or one of its dependencies. The system cannot fond the file specified error. When I comment out the line in the web config file, I get the error on the line following that. I have all the dll files in the bin folder.
0
8856
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8762
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
8545
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
7365
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
6185
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
5653
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();...
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1992
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.