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 From Oledbconnection To Asp:Dropdownlist – 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=" _ ‘ […]
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Catch Database Operation Exceptions For Data Binding – ADO.Net Database

<%@ import Namespace="System.Data" %> <%@ import Namespace="System.Data.Oledb" %><script language="vb" runat="server"> Sub Page_Load() Dim strConnection as String = "Provider=Microsoft.Jet.OLEDB.4.0;" strConnection += "Data Source=C:\Northwind.mdb" data_src.text = strConnection Dim strSQL as string = "SELECT FirstName, MiddleName, LastName FROM Employees;" Dim objDataSet as new DataSet() Dim objConnection as new OledbConnection(strConnection) Dim objAdapter as new OledbDataAdapter(strSQL, objConnection)Try objAdapter.Fill(objDataSet, "Employees")   […]
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Detecting Concurrency Errors After Updating Data (Vb) – ADO.Net Database

<%@ Page Language="VB" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"> Protected Sub SqlDataSource1_Updated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) If (e.AffectedRows > 0) Then Message.Text = "The record has been updated" Else Message.Text = "Possible concurrency violation" End If End Sub </script><html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" […]
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Handling Concurrency Issues: Compareallvalues Or Overwritechanges – ADO.Net Database

<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><script runat="server"> protected void srcProducts_Updated(object sender, SqlDataSourceStatusEventArgs e) { if (e.AffectedRows == 0) lblMessage.Text = "Could not update record"; } </script><html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server"> <title>Concurrency</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="lblMessage" EnableViewState="false" runat="server" /> <asp:DetailsView id="dtlProducts" DataKeyNames="Id" AutoGenerateEditButton="true" AllowPaging="true" DataSourceID="srcProducts" Runat="server" […]
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Paging Through 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>Page Grid</title> </head> <body> <form id="form1" runat="server"> <asp:GridView id="grdProducts" DataSourceID="srcProducts" AllowPaging="true" PageSize="3" Runat="server" /> <asp:SqlDataSource id="srcProducts" ConnectionString="<%$ ConnectionStrings:Products %>" SelectCommand="SELECT Id,Title,Director FROM Products" Runat="server" /> </form> </body> </html>
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Synchronize Database Operation – ADO.Net Database

<%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.OleDb" %><script language="VB" runat="server"> Sub Page_Load(Sender As Object, E As EventArgs) Dim strConnection As String Dim strSQL As String Dim objDataSet As New DataSet() Dim objConnection As OleDbConnection Dim objAdapter As OleDbDataAdapter’ strConnection = "Provider=Microsoft.Jet.OLEDB.4.0; " & _ ‘ "Data Source=C:\Northwind.mdb" strConnection = "Provider=sqloledb;Data Source=whsql-v08.prod.mesa1.secureserver.net;Initial Catalog=DB_49907;User ID=w3mentoruser;Password=password;"   strSQL […]
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Using Stored Procedures – ADO.Net Database

<%@ Import namespace="System.Data" %> <%@ Import namespace="System.Data.SqlClient" %><html> <head><title>Using Stored Procedures</title></head> <body> <form runat="server" method="post"> <asp:DataGrid id="dgOutput" runat="server" /> </form> </body> </html><script language="VB" runat="server"> Sub Page_Load(ByVal Source As Object, ByVal E As EventArgs) ‘ Connection setup Dim strConnection As String = ConfigurationSettings.AppSettings("NorthWind") Dim objConnection As New SqlConnection(strConnection) Dim objCommand As New SqlCommand( _ "[Ten Most […]
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Add Datacolumn To Datatable – ADO.Net Database

<%@ Page Language="C#" %> <%@ import Namespace="System.Data" %> <%@ import Namespace="System.Data.SqlClient" %> <script runat="server"> void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) BindData(); }     IEnumerable ItemsInCart() { DataTable dt = new DataTable(); DataRow dr;   dt.Columns.Add(new DataColumn("ProductID", typeof(int))); dt.Columns.Add(new DataColumn("Name", typeof(string))); dt.Columns.Add(new DataColumn("Price", typeof(float)));   for (int i = 0; i < 10; […]
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Bind Data To Hyper Link Column – 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 Connection To Sql Express (Vb.Net) – ADO.Net Database

<%@ Page Language="C#" %><html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server"> <title>Show Local Connection</title> </head> <body> <form id="form1" runat="server"> <div>   <asp:GridView id="grdEmployee" DataSourceID="srcEmployee" Runat="server" />   <asp:SqlDataSource id="srcEmployee" SelectCommand="SELECT * FROM Employee" ConnectionString="Data Source=.\SQLEXPRESS; AttachDbFilename=|DataDirectory|MyDatabase.mdf; Integrated Security=True;User Instance=True" Runat="server" />     </div> </form> </body> </html>
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Editing Record With Detailsview Control. – 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 Update</title> </head> <body> <form id="form1" runat="server"> <div> <asp:DetailsView id="dtlProducts" DataKeyNames="Id" AutoGenerateEditButton="true" AllowPaging="true" DataSourceID="srcProducts" Runat="server" /> <asp:SqlDataSource id="srcProducts" ConnectionString="<%$ ConnectionStrings:Products %>" SelectCommand="SELECT Id,Title,Director,InStocks FROM Products" UpdateCommand="UPDATE Products SET Title=@Title,Director=@Director,InStocks=@InStocks WHERE Id=@Id" Runat="server" /> </div> </form> </body> </html>File: Web.config<configuration> […]
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Inserting Data With Detailsview Control – 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 Insert</title> </head> <body> <form id="form1" runat="server"> <div> <asp:DetailsView id="dtlProducts" AllowPaging="true" DataSourceID="srcProducts" AutoGenerateInsertButton="true" Runat="server" /> <asp:SqlDataSource id="srcProducts" ConnectionString="<%$ ConnectionStrings:Products %>" SelectCommand="SELECT Title,Director,InStocks FROM Products" InsertCommand="INSERT Products (Title,Director,InStocks) VALUES (@Title,@Director, Runat="server" /> </div> </form> </body> </html>File: Web.config<configuration> <connectionStrings> <add […]
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Reading From Excel Workbook (Vb.Net) – ADO.Net Database

<%@ Import namespace="System.Data" %> <%@ Import namespace="System.Data.OleDb" %><html> <head> <title>Reading from an Excel Workbook</title> </head> <body> <h3>Reading from an Excel Workbook</h3> <asp:DataGrid id="dgInventory" runat="server" /> </body> </html><script language="VB" runat="server"> Sub Page_Load(Source As Object, E As EventArgs) Dim strConnection As String = "Provider=Microsoft.Jet.OleDb.4.0;" & _ "data source=C:\yourExcellFileName.xls;" & _ "Extended Properties=Excel 8.0;" Dim objConnection As New […]
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Updating Access Data – 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 Writing Binary Content In Asp.Net – ADO.Net Database

<%@ Page Language="vb" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SqlClient" %> <%@ Import Namespace="System.IO" %> <html> <head> <title>Writing binary content in ASP.NET</title> <script runat="server"> Sub Page_Load() Dim ConnStr As String ‘change the following string to your settings ConnStr = "Data Source=(local)\NetSDK;" ConnStr &= "database=pubs;integrated security=true" Dim mySqlConn As New SqlConnection(ConnStr) mySqlConn.Open Dim SQL As […]
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Adding Datasourcemode Property 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"><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 %>" DataSourceMode="DataSet"> </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" […]
Continue reading…

Enjoyed this post? Share it!

 
 

CSharp Build Dataset – ADO.Net Database

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="TestDataSet" %><!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 id="Head1" runat="server"> <title>Using a DataSet</title> </head> <body> <form id="form1" runat="server"> <div id="container"> <asp:GridView id="grdProducts" runat="server" /> <hr /> <asp:GridView id="grdCustomer" runat="server" /> </div> </form> </body> </html>File: Default.aspx.csusing System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; […]
Continue reading…

Enjoyed this post? Share it!