How to read SQL Table data into a C# DataTable

Here, give this a shot (this is just a pseudocode) using System; using System.Data; using System.Data.SqlClient; public class PullDataTest { // your data table private DataTable dataTable = new DataTable(); // your method to pull data from database to datatable public void PullData() { string connString = @”your connection string here”; string query = “select … Read more

How to fill a datatable with List [duplicate]

Just in case you have a nullable property in your class object: private static DataTable ConvertToDatatable<T>(List<T> data) { PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(T)); DataTable table = new DataTable(); for (int i = 0; i < props.Count; i++) { PropertyDescriptor prop = props[i]; if (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>)) table.Columns.Add(prop.Name, prop.PropertyType.GetGenericArguments()[0]); else table.Columns.Add(prop.Name, prop.PropertyType); } object[] values … Read more

DataTable: How to get item value with row name and column name? (VB)

Dim rows() AS DataRow = DataTable.Select(“ColumnName1 = ‘value3′”) If rows.Count > 0 Then searchedValue = rows(0).Item(“ColumnName2”) End If With FirstOrDefault: Dim row AS DataRow = DataTable.Select(“ColumnName1 = ‘value3′”).FirstOrDefault() If Not row Is Nothing Then searchedValue = row.Item(“ColumnName2”) End If In C#: var row = DataTable.Select(“ColumnName1 = ‘value3′”).FirstOrDefault(); if (row != null) searchedValue = row[“ColumnName2”];

DataView.RowFilter Vs DataTable.Select() vs DataTable.Rows.Find()

You are looking for the “best approach on finding rows in a datatable”, so I first have to ask: “best” for what? I think, any technique has scenarios where it might fit better then the others. First, let’s look at DataView.RowFilter: A DataView has some advantages in Data Binding. Its very view-oriented so it has … Read more

Export DataTable to Excel File

use this code… dt = city.GetAllCity();//your datatable string attachment = “attachment; filename=city.xls”; Response.ClearContent(); Response.AddHeader(“content-disposition”, attachment); Response.ContentType = “application/vnd.ms-excel”; string tab = “”; foreach (DataColumn dc in dt.Columns) { Response.Write(tab + dc.ColumnName); tab = “\t”; } Response.Write(“\n”); int i; foreach (DataRow dr in dt.Rows) { tab = “”; for (i = 0; i < dt.Columns.Count; i++) … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)