How to fill a DataSet Object in C#

Add a Button control named btnLoad and a DataGrid control named dgProducts on the form/page. using System.Data; using System.Data.SqlClient; private void btnLoad_Click(object sender, System.EventArgs e) { // Create a SqlConnection SqlConnection cnn = new SqlConnection( "Data Source=(local); Initial Catalog=Northwind;"+ " Integrated Security=SSPI"); // Create a SqlCommand SqlCommand cmd = cnn.CreateCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = […]
Continue reading…

Enjoyed this post? Share it!

 
 

Using a ADO.NET SqlDataReader Object in C#

Add a Button control (btnGetCustomers) and a ListBox control (lbCustomers) to your form/page. using System.Data; using System.Data.SqlClient;   private void btnGetCustomers_Click(object sender, System.EventArgs e) { // Connect to the database SqlConnection cnn = new SqlConnection(); cnn.ConnectionString = "Data Source=(local);" + "Initial Catalog=Northwind;" + "Integrated Security=SSPI"; // Create a new ad hoc query // to retrieve […]
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Adding Section To Sqldatasource Control – ADO.Net Database

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.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" DataSourceID="SqlDataSource1" DataKeyNames="CustomerID" AutoGenerateColumns="False" AllowSorting="True" AllowPaging="True" AutoGenerateEditButton="true" AutoGenerateDeleteButton="true"> <pagerStyle HorizontalAlign="Center"></pagerStyle> <pagerSettings Position="TopAndBottom" FirstPageText="Go to the first page" LastPageText="Go to the last page" Mode="NextPreviousFirstLast"> </pagerSettings> <columns> <asp:BoundField ReadOnly="True" HeaderText="CustomerID" DataField="CustomerID" SortExpression="CustomerID" Visible="False"></asp:BoundField> <asp:HyperLinkField HeaderText="CompanyName" […]
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Binding Listbox Control To Data Source – ADO.Net Database

<%@ Page Language=VB Debug=true %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.OLEDB" %> <script runat=server> Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs) If Not IsPostBack Then Dim DBConn as OleDbConnection Dim DBCommand As OleDbDataAdapter Dim DSPageData as New DataSet DBConn = New OleDbConnection( _ "PROVIDER=Microsoft.Jet.OLEDB.4.0;" _ & "DATA SOURCE=" _ & Server.MapPath("EmployeeDatabase.mdb;")) DBCommand […]
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Converting Xml To Dataset And Vice Versa (Vb) – ADO.Net Database

<%@ Page Language="vb" Debug="true" %> <%@ Import Namespace="System" %> <%@ Import Namespace="System.Data" %> <html> <head> <title>Converting XML to DataSet and Vice versa.</title> <script language="vb" runat="server"> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Try Dim Ds As New DataSet Ds.ReadXml(Server.MapPath("Data.XML")) Ds.AcceptChanges() Ds.Tables(0).Rows(0).Item("LastModified") = DateTime.Now.ToString() Dim dsChanges As DataSet dsChanges = Ds.GetChanges() dsChanges.WriteXml(Server.MapPath("") & […]
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Executing Stored Procedure Using Sqldatasource Control – ADO.Net Database

<%@ Page Language="C#" %> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Executing a Stored Procedure using SqlDataSource Control</title> </head> <body> <form id="form1" runat="server"> <div> <asp:SqlDataSource ID="categoriesSource" runat="server" ProviderName="System.Data.SqlClient" ConnectionString="<%$ ConnectionStrings:AdventureWorks %>" SelectCommand="GetProductSubCategories" SelectCommandType="StoredProcedure"> </asp:SqlDataSource> Categories: <asp:DropDownList runat="server" DataSourceID="categoriesSource" DataValueField="ProductSubcategoryID" DataTextField="Name" AutoPostBack="true" ID="lstCategories" /> <asp:SqlDataSource ID="productsSource" runat="server" ProviderName="System.Data.SqlClient" ConnectionString="<%$ ConnectionStrings:AdventureWorks %>" SelectCommand="GetProductsByCategoryID" SelectCommandType="StoredProcedure"> <selectParameters> <asp:ControlParameter ControlID="lstCategories" Name="ProductSubcategoryID" PropertyName="SelectedValue" […]
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Linkbutton In Itemtemplate – ADO.Net Database

<%@ Page Language="vb" %> <%@ import Namespace="System.Data" %> <%@ import Namespace="System.Data.SqlClient" %> <script runat="server" language="VB"> Sub Page_Load(sender as Object, e as EventArgs) If Not Page.IsPostBack then BindData() End If End Sub   Sub BindData() Const strConnString as String = "server=localhost;uid=sa;pwd=;database=pubs" Dim objConn as New SqlConnection(strConnString)   Const strSQL as String = "SELECT * FROM titles" […]
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Retrieving Sql Server Data (Vb.Net) – ADO.Net Database

<%@ Page Language=VB Debug=true %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SQLClient" %> <script runat=server> Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs) If Not IsPostBack Then Dim DBConn as SQLConnection Dim DBCommand As SQLDataAdapter Dim DSPageData as New DataSet ‘ DBConn = New SQLConnection("server=localhost;" _ ‘ & "Initial Catalog=TT;" _ ‘ & "User […]
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Use Asp:Datagrid To Add, Update – ADO.Net Database

<%@ Page Language=VB Debug=true %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.OLEDB" %> <script runat=server> Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs) If Not IsPostBack Then BuildDataList End If End Sub Sub DataList_Edit(sender As Object, e As DataListCommandEventArgs) dlDepts.EditItemIndex = CInt(e.Item.ItemIndex) BuildDataList End Sub Sub DataList_Update(sender As Object, e As DataListCommandEventArgs) Dim TheID […]
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Asp:Datagrid: Sort Expression (Vb.Net) – ADO.Net Database

<%@ Page Language=VB Debug=true %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.OLEDB" %> <script runat=server> Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs) If Not IsPostBack Then Dim DBConn as OleDbConnection Dim DBCommand As OleDbDataAdapter Dim DSPageData as New DataSet DBConn = New OleDbConnection( _ "PROVIDER=Microsoft.Jet.OLEDB.4.0;" _ & "DATA SOURCE=" _ & Server.MapPath("EmployeeDatabase.mdb;")) DBCommand […]
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Cache Sql Data Source (C#) – ADO.Net Database

<%@ Page Language="C#" %><script runat="server"> protected void dataSelect(object sender, SqlDataSourceSelectingEventArgs e) { lblMessage.Text = "Retrieving data from database"; } </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server"> <title>Cache SqlDataSource</title> </head> <body> <form id="form1" runat="server"> <div>   <asp:Label id="lblMessage" EnableViewState="false" Runat="server" /> <br /><br />   <asp:GridView id="grdEmployee" DataSourceID="srcEmployee" Runat="server" />   <asp:SqlDataSource id="srcEmployee" EnableCaching="True" CacheDuration="3600" SelectCommand="SELECT […]
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Define Your Own Collection For Objectdatasource – ADO.Net Database

<%@ page language="C#" %><%@ import namespace="System" %> <%@ import namespace="System.Web" %> <%@ import namespace="System.Collections.Generic" %><script runat="server" language="c#">public class Person { private int id; private string firstname; private string lastname; public Person(int id, string firstname, string lastname) { this.id = id; this.firstname = firstname; this.lastname = lastname; } public int Id { get { return this.id; […]
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Get Data In Database Through Sqldataadapter (C#) – ADO.Net Database

<%@ Page Language="C#" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SqlClient" %> <html> <title>DataSet Example</title> <head> <script runat="server"> void Page_Load() { String ConnStr = "Data Source=whsql-v08.prod.mesa1.secureserver.net;Initial Catalog=DB_49907;User ID=w3mentoruser;Password=’password’;"; String SQL = "SELECT ID, FirstName FROM Employee " + "WHERE ID IS NOT NULL"; SqlDataAdapter TitlesAdpt = new SqlDataAdapter(SQL, ConnStr); DataSet Titles = new DataSet(); // […]
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Objectdatasource With Control Parameter – ADO.Net Database

<%@ page language="C#" %><%@ import namespace="System" %> <%@ import namespace="System.Web" %> <%@ import namespace="System.Collections.Generic" %><script runat="server" language="c#">public class Person { private int id; private string firstname; private string lastname; public Person(int id, string firstname, string lastname) { this.id = id; this.firstname = firstname; this.lastname = lastname; } public int Id { get { return this.id; […]
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Sqldatasource For Oracle – ADO.Net Database

<%@ Page Language="C#" %><html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server"> <title>Connect Oracle</title> </head> <body> <form id="form1" runat="server"> <div>   <asp:GridView id="grdOrders" DataSourceID="srcOrders" Runat="server" />   <asp:SqlDataSource id="srcOrders" ProviderName="System.Data.OracleClient" SelectCommand="SELECT * FROM Orders" ConnectionString="Data Source=OracleDB;Integrated Security=yes" Runat="server" />   </div> </form> </body> </html>
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Using Formview Control To Display And Edit Data – ADO.Net Database

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.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:FormView ID="FormView1" Runat="server" DataSourceID="SqlDataSource1" DataKeyNames="CustomerID" AllowPaging="True"> <editItemTemplate> CustomerID: <asp:Label Text='<%# Eval("CustomerID") %>’ Runat="server" ID="CustomerIDLabel1"> </asp:Label><br /> CompanyName: <asp:TextBox Text='<%# Bind("CompanyName") %>’ Runat="server" ID="CompanyNameTextBox"></asp:TextBox><br /> ContactName: <asp:TextBox Text='<%# Bind("ContactName") %>’ Runat="server" ID="ContactNameTextBox"></asp:TextBox><br /> ContactTitle: <asp:TextBox […]
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Bind Data Source To Asp:Dropdownlist – ADO.Net Database

<%@ Page Language="VB" %> <script runat="server"> Function AllPublishers() As System.Data.SqlClient.SqlDataReader Dim connectionString As String = "server='(local)\NetSDK’; trusted_connection=true; Database=’pubs’" Dim sqlConnection As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection(connectionString)   Dim queryString As String = "SELECT [publishers].* FROM [publishers]" Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(queryString, sqlConnection)   sqlConnection.Open Dim dataReader As System.Data.SqlClient.SqlDataReader = sqlCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection)   Return dataReader End […]
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Checking For Update Errors Using Rowupdated Event (C#) – ADO.Net Database

<%@ Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"> protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e) { if (e.Exception != null) { this.lblErrorMessage.Text = e.Exception.Message; } } </script><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" DataSourceID="SqlDataSource1" DataKeyNames="CustomerID" AutoGenerateColumns="False" AllowSorting="True" AllowPaging="True" AutoGenerateEditButton="true" OnRowUpdated="GridView1_RowUpdated"> <pagerStyle HorizontalAlign="Center"></pagerStyle> <pagerSettings […]
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Display Data From Access To Asp:Datagrid – ADO.Net Database

<%@ Page Language=VB Debug=true %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.OLEDB" %> <script runat=server> Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs) Dim DBConn as OleDbConnection Dim DBCommand As OleDbDataAdapter Dim DSPageData as New DataSet DBConn = New OleDbConnection( _ "PROVIDER=Microsoft.Jet.OLEDB.4.0;" _ & "DATA SOURCE=" _ & Server.MapPath("EmployeeDatabase.mdb;")) DBCommand = New OleDbDataAdapter _ […]
Continue reading…

Enjoyed this post? Share it!