Posts

Showing posts with the label System.Data

How to convert List to DataTable - C#,ASP.NET

Image
Short Description This example will show how to convert List<T> where T could be any object and convert the List<T> to DataTable. The example will use two class which is System.ComponentModel; and System.Data; Note : The Convertion class extention method must be in static because System.ComponentModel.TypeDescriptor is a static method In this example i will use asp.net . The ASPX page      <h1>         How to convert List To DataTable     </h1>     <asp:GridView ID="GridView1" runat="server">     </asp:GridView> The Code Behind        protected void Page_Load(object sender, EventArgs e)         {             List<string> listOfString = new List<string>();             for (int i = 0; i < 10; i++)             {                 listOfString.Add(i.ToString());             }                        GridView1.DataSource = Convertion.ToDataTable<string>(listOfString);             GridView1.DataBind();         } The

How to alter DataTable to add new Column -C#, asp.net

Image
In this tutorial i will show how to manipulate the DataTable to append with new column. In this example i will retrieve data from database using SqlDataAdapter, you can refer this tutorial to know how to using SqlDataAdapter. After fill DataTable with data, the program will append the column of the DataTable. Lets check on the example below. The Database Table "Users" SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[Users](     [Userid] [varchar](50) NOT NULL,     [UserEmail] [varchar](50) NOT NULL,     [DateTimeCreated] [datetime] NOT NULL,     [CreatedBy] [varchar](50) NOT NULL,     [DateTimeModified] [datetime] NULL,     [ModifiedBy] [varchar](50) NULL ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO INSERT [dbo].[Users] ([Userid], [UserEmail], [DateTimeCreated], [CreatedBy], [DateTimeModified], [ModifiedBy]) VALUES (N'developers', N'developersnote@gmail.com', CAST(0x0000A284014950B0 AS DateTime), N'SYS', NULL, N

Read data from DataTable - C#

This is the example of reading data from datatable : The code explaination : The code will retrieve data from database and store into databable. Andthen , the code will continue by looping each row in a datatable and try to read every column on the row. The Code     using System;     using System.Data;     using System.Data.SqlClient;     class PopDataset     {        static void Main()        {           string connString = "<connection string>";           string sql = "select * from employee";           SqlConnection conn = new SqlConnection(connString);           try           {          DataTable dt = new DataTable();              conn.Open();              SqlDataAdapter da = new SqlDataAdapter(sql, conn);                da.Fill(dt);          conn.Close();                          foreach (DataRow row in dt.Rows)              {         //read each column in row                 foreach (DataColumn col in dt.Columns)         {                    Console.Writ