473,394 Members | 1,879 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,394 software developers and data experts.

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

26
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 4084
kenobewan
4,871 Expert 4TB
What have you done so far? Please read the faqs you are not following the site rules. Thanks.
Dec 16 '07 #2
gagonm
26
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 Expert 1GB
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
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 Expert Mod 8TB
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
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 Expert Mod 8TB
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
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 Expert Mod 8TB
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
gagonm
26
Much Thanks for ur help
Dec 25 '07 #11

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

Similar topics

0
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:...
9
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...
10
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...
0
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...
16
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...
0
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
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
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'...
7
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...
2
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
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...

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.