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 Bind Data To Asp Datagrid In C# – ADO.Net Database

<%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.OleDb" %> <script language="C#" runat="server"> void Page_Load(object sender, EventArgs e) { OleDbConnection objConnection; OleDbDataAdapter objCommand; string strConnect; string strCommand; DataSet DataSet1 = new DataSet();   strConnect = @"Provider=Microsoft.Jet.OLEDB.4.0;"; //If you don’t have the EmployeeDatabase.mdb database, then you will have to //change the following three lines to a different database […]
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Compare Connection: Metadata (C#) – ADO.Net Database

<%@ Page Language="C#" %> <%@ import Namespace="System.Data" %> <%@ import Namespace="System.Data.SqlClient" %> <%@ import Namespace="System.Data.OleDb" %> <%@ import Namespace="System.Data.Odbc" %> <script runat="server"> void Page_Load(object sender, EventArgs e) { SqlConnection mySqlConnection = new SqlConnection("Data Source=whsql-v08.prod.mesa1.secureserver.net;Initial Catalog=DB_49907;User ID=w3mentoruser;Password=’password’;"); mySqlConnection.Open();   lblSqlConnectionString.Text = mySqlConnection.ConnectionString; lblSqlConnectionTimeout.Text = Convert.ToString(mySqlConnection.ConnectionTimeout); lblSqlDatabase.Text = mySqlConnection.Database; lblSqlDataSource.Text = mySqlConnection.DataSource; lblSqlPacketSize.Text = Convert.ToString(mySqlConnection.PacketSize); lblSqlServerVersion.Text = […]
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Display Of Data In Table (Grid) Using Sql Objects (Vb.Net) – ADO.Net Database

<%@Import Namespace="System.Data" %> <%@Import Namespace="System.Data.SqlClient" %><html> <body> <h2>Display of Data in a Table (Grid) Using SQL Objects</h2>Northwind Employees: <asp:datagrid id="dgrEmployees" runat="server" /><script language="vb" runat="server"> Sub Page_Load() Dim strSQL as string = "SELECT FirstName,LastName FROM Employees;" Dim strConnection as String = "server=EWANB;database=Northwind; User Id=sa;password=;"   Dim objDataSet As New DataSet() Dim objConnection As New SqlConnection(strConnection)   […]
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Insert Data To Database Using Sql (C#) – ADO.Net Database

<%@ Page Debug="true" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.OleDb" %><script language="C#" runat="server"> void Page_Load (Object sender, EventArgs e) { OleDbConnection objConnection = null; OleDbCommand objCmd = null; String strConnection, strSQL; strConnection = "Provider=Microsoft.Jet.OleDb.4.0;"; // strConnection += @"Data Source=C:\Northwind.mdb"; strConnection += @"Data Source="+MapPath("EmployeeDatabase.mdb");   // Create and open the connection object objConnection = new […]
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Read Data From Sqlconnection (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 Typical Sqldatasource Control Generated By Visual Studio – ADO.Net Database

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"></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:SqlDataSource ID="SqlDataSource1" Runat="server" SelectCommand="SELECT * FROM [Customers]" ConnectionString="<%$ ConnectionStrings:AppConnectionString1 %>"> </asp:SqlDataSource> </div> </form> </body> </html>File: Web.config<configuration> <appSettings/> <connectionStrings> <add name="AppConnectionString1" connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings> <system.web> <compilation debug="true" strict="false" explicit="true"> <codeSubDirectories> <add […]
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Working With Transactions With Access Database – 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 DBDelete As New OleDbCommand Dim DBTrans As OleDbTransaction DBConn = New OleDbConnection( _ "PROVIDER=Microsoft.Jet.OLEDB.4.0;" _ & "DATA SOURCE=" _ & Server.MapPath("EmployeeDatabase.mdb;")) DBConn.Open() DBTrans = DBConn.BeginTransaction() DBDelete.Connection […]
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Adding Indicators To Display Sorting Direction – ADO.Net Database

<%@ Page Language="C#" %> <script runat="server"> void deptView_RowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.Header) { string imageUrl = (deptView.SortDirection==SortDirection.Ascending ?"Asc.gif" :"Desc.gif"); for(int i=0; i<deptView.Columns.Count; i++) { string columnExpression = deptView.Columns[i].SortExpression; if (columnExpression != "" && columnExpression == deptView.SortExpression) { Image img = new Image(); img.ImageUrl =imageUrl; e.Row.Cells[i].Controls.Add(img); } } } } </script> <html […]
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Bind Sqldatareader To Gridview – ADO.Net Database

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default_aspx" %><!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.csusing System; using System.Data; using System.Configuration; 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.Data.SqlClient;public partial class […]
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Creating Custom Parameter Objects – ADO.Net Database

File: UsernameParameter.csusing System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls;namespace MyControls { public class UsernameParameter : Parameter { protected override object Evaluate(HttpContext context, Control control) { if (context != null) return context.User.Identity.Name; else return null; } } } File: Default.aspx<%@ Page Language="C#" %> <%@ Register TagPrefix="custom" Namespace="MyControls" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html […]
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Filtering Data – ADO.Net Database

<%@ Page Language="C#" %> <!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 id="Head1" runat="server"> <title>Show Filtered Products</title> </head> <body> <form id="form1" runat="server"> <asp:DropDownList id="ddlProductCategory" DataSourceID="srcProductCategories" DataTextField="Name" DataValueField="Id" Runat="server" /> <asp:Button id="btnSelect" Text="Select" Runat="server" /> <asp:GridView id="grdProducts" DataSourceID="srcProducts" AutoGenerateColumns="false" Runat="server"> <columns> <asp:BoundField DataField="Title" HeaderText="Product Title" /> <asp:BoundField DataField="Director" HeaderText="Product Director" /> </columns> </asp:GridView> […]
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Loop Through Data In Sql Server By Dataset – 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) Dim DBConn as SQLConnection Dim DBCommand As SQLDataAdapter Dim DSPageData as New DataSet Dim I as Long ‘ DBConn = New SQLConnection("server=localhost;" _ ‘ & "Initial Catalog=TT;" _ ‘ & "User […]
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Set Datasource To String Array Of File Names – ADO.Net Database

<%@ Page %> <%@ import Namespace="System.IO" %> <script runat="server"> Sub Page_Load(sender as Object, e as EventArgs) ‘Set the DataSource to a String array of file names dgFiles.DataSource = Directory.GetFiles("C:\") dgFiles.DataBind() End Sub</script> <asp:datalist id="dgFiles" runat="server" RepeatColumns="2" CellSpacing="5"> <itemTemplate> <%# Container.DataItem %> </itemTemplate> </asp:datalist>
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Use Sqlcachedependencyadmin To Gettablesenabledfornotifications – ADO.Net Database

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="Default_aspx" %><!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" DataSourceID="SqlDataSource1" AutoGenerateColumns="False" DataKeyNames="au_id"> <columns> <asp:BoundField ReadOnly="True" HeaderText="au_id" DataField="au_id" SortExpression="au_id"> </asp:BoundField> <asp:BoundField HeaderText="au_lname" DataField="au_lname" SortExpression="au_lname"> </asp:BoundField> <asp:BoundField HeaderText="au_fname" DataField="au_fname" SortExpression="au_fname"> </asp:BoundField> <asp:BoundField HeaderText="phone" DataField="phone" SortExpression="phone"></asp:BoundField> <asp:BoundField HeaderText="address" DataField="address" […]
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Bind Asp:Datagrid Delete Action To Asp:Linkbutton – 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_Delete(sender As Object, e As DataListCommandEventArgs) Dim TheID as String TheID = CType(e.Item.FindControl("lblID"), Label).Text Dim DBConn as OleDbConnection Dim DBDelete As […]
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Call Store Procedure Through Sqlconnection – ADO.Net Database

<%@ Page Language="VB" %> <%@ import Namespace="System.Data" %> <%@ import Namespace="System.Data.SqlClient" %> <script runat="server"> Sub Page_Load(Sender As Object, E As EventArgs) Dim ConnectionString As String = _ "server=(local)\netsdk;database=cam;trusted_connection=true"   Dim CommandText As String = "AllDiscsByDate"   Dim myConnection As New SqlConnection(ConnectionString) Dim myCommand As New SqlCommand(CommandText, myConnection) Dim workParam As New SqlParameter()   myCommand.CommandType = […]
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Deletion Using Sqldatasource Control – ADO.Net Database

<%@ Page Language="C#" %> <script runat="server"> void deptSource_Deleted(object sender,SqlDataSourceStatusEventArgs e) { if (e.Exception == null) { if (e.AffectedRows == 1) { lblResult.Text = "Record deleted successfully."; } else { lblResult.Text = "An error occurred during the delete operation."; } } else { lblResult.Text = "An error occurred while attempting to delete the row." + e.Exception.Message; […]
Continue reading…

Enjoyed this post? Share it!