By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,771 Members | 1,685 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,771 IT Pros & Developers. It's quick & easy.

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

P: 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
Share this Question
Share on Google+
10 Replies


kenobewan
Expert 2.5K+
P: 4,871
What have you done so far? Please read the faqs you are not following the site rules. Thanks.
Dec 16 '07 #2

P: 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
Expert 100+
P: 1,435
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

P: 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
Expert Mod 5K+
P: 9,731
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

P: 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
Expert Mod 5K+
P: 9,731
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

P: 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
Expert Mod 5K+
P: 9,731
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

P: 26
Much Thanks for ur help
Dec 25 '07 #11

Post your reply

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