1) Just use something like SqlDataSource and create CRUD methods, then enable Edit for GridView. Thus anything you do changes to the row's values and click Update button, it will automatically upgrate to db.
2)Manually handle events such as Row_Editing, Row_Updating events to call classes such as SqlCommand or SqlConnection to cope with these problems.
Adnan dani
Member
9 Points
55 Posts
how to bind data from gridview to database
Dec 16, 2012 05:07 AM|LINK
my question is that how can i bind data from gridview to database. How can i use GridView.DataBind method for this purpose.
furry
Member
584 Points
108 Posts
Re: how to bind data from gridview to database
Dec 16, 2012 05:45 AM|LINK
you probably wanna bind your data to GridView
using Code :
http://www.aspsnippets.com/Articles/Bind-GridView-using-SqlDataSource-Programmatically-from-code-in-ASPNet.aspx
http://www.codeproject.com/Articles/14249/How-to-populate-DataGridView-GridView-with-SQL-sta
Using SQL Soruce Objects:
http://www.programcall.com/11/aspnet/binding-gridview-using-sqldatasource-in-aspnet.aspx
Anil Srivast...
Member
442 Points
292 Posts
Re: how to bind data from gridview to database
Dec 16, 2012 05:48 AM|LINK
take namespace
using System.Data;//Container for dataset ds
using System.Data.SqlClient;
using System.DataSet;
1. initialise connection string(datasource,intial catalog, integrated security)
2. open connection
3. use disconnected architecture(SqlDataAdapter)
4. use fill method (adp.Fill(ds,"Virtual Table")
5. GridView1.DataSource=ds.tables[0];
6 use bind method(only in web form)
raju_mab
Member
559 Points
110 Posts
Re: how to bind data from gridview to database
Dec 16, 2012 06:18 AM|LINK
refers link...
https://www.youtube.com/watch?v=w34BDhDPEEQ
http://www.c-sharpcorner.com/uploadfile/john_charles/data-binding-in-Asp-Net-2-0-using-gridview-control/
oned_gk
All-Star
31295 Points
6398 Posts
Re: how to bind data from gridview to database
Dec 16, 2012 07:03 AM|LINK
<asp:FormView ID="FormView1" runat="server" DataKeyNames="ID" DataSourceID="SqlDataSource1" DefaultMode="Insert" oniteminserted="FormView1_ItemInserted"> <InsertItemTemplate> Name: <asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' /> <br /> <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="Insert" /> </InsertItemTemplate> </asp:FormView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TESTConnectionString %>" InsertCommand="INSERT INTO [Persons] ([Name]) VALUES (@Name)"> <InsertParameters> <asp:Parameter Name="Name" Type="String" /> </InsertParameters> </asp:SqlDataSource><asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDataSource2"> <Columns> <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" /> <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" /> <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:TESTConnectionString %>" DeleteCommand="DELETE FROM [Persons] WHERE [ID] = @ID" InsertCommand="INSERT INTO [Persons] ([Name]) VALUES (@Name)" SelectCommand="SELECT [ID], [Name] FROM [Persons] ORDER BY [ID] DESC" UpdateCommand="UPDATE [Persons] SET [Name] = @Name WHERE [ID] = @ID"> <DeleteParameters> <asp:Parameter Name="ID" Type="Int32" /> </DeleteParameters> <UpdateParameters> <asp:Parameter Name="Name" Type="String" /> <asp:Parameter Name="ID" Type="Int32" /> </UpdateParameters> <InsertParameters> <asp:Parameter Name="Name" Type="String" /> </InsertParameters> </asp:SqlDataSource>Calling gridview1.databind to refresh the gridview after data inserted imediately
protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e) { GridView1.DataBind(); }Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: how to bind data from gridview to database
Dec 17, 2012 01:20 AM|LINK
You have mainly two ways:
1) Just use something like SqlDataSource and create CRUD methods, then enable Edit for GridView. Thus anything you do changes to the row's values and click Update button, it will automatically upgrate to db.
2)Manually handle events such as Row_Editing, Row_Updating events to call classes such as SqlCommand or SqlConnection to cope with these problems.
Reguards!
RameshRajend...
Star
7983 Points
2099 Posts
Re: how to bind data from gridview to database
Dec 17, 2012 01:40 AM|LINK
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="BasicGridView" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server"> </asp:GridView> </div> </form> </body> </html> File: Default.aspx.cs using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Web.Configuration; using System.Data.SqlClient; public partial class BasicGridView : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { string connectionString = WebConfigurationManager.ConnectionStrings["Northwind"].ConnectionString; string selectSQL = "SELECT ProductID, ProductName, UnitPrice FROM Products"; SqlConnection con = new SqlConnection(connectionString); SqlCommand cmd = new SqlCommand(selectSQL, con); SqlDataAdapter adapter = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); adapter.Fill(ds, "Products"); GridView1.DataSource = ds; GridView1.DataBind(); } } } File: Web.config <?xml version="1.0"?> <configuration> <connectionStrings> <add name="Northwind" connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=SSPI"/> </connectionStrings> </configuration>sarathi125
Star
13599 Points
2691 Posts
Re: how to bind data from gridview to database
Dec 17, 2012 03:46 AM|LINK
Hi,
What is your requirement, explain little bit clearly, are you want to show the data from database to gridview or ?
Remember to click Mark as Answer on the post that helps to others.
My Blog :MyAspSnippets