Posts

Showing posts with the label Data Table

How to import CSV files into DataTable in C#

Below is code the which you can use for converting a csv file into Data Table in C#. This method also handles comma values inside a double quoted value. 1. Provide the file path of your csv file: string sCsvFilePath = @"C:\Downloads\Sample.csv"; 2. Call Custom Method: DataTable  dtTable = ConvertCSVtoDataTable(sFilePath); 3. Copy and Paste below: public   static   DataTable  ConvertCSVtoDataTable( string  sCsvFilePath) {      DataTable  dtTable =  new   DataTable ();      Regex  CSVParser =  new   Regex ( ",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))" );      using  ( StreamReader  sr =  new   StreamReader (sCsvFilePath))     {          string [] headers = sr.ReadLine().Split( ',' );          foreach ...

Code for Data Export from GridView to Excel in Asp.Net- c#

In this blog, I am going to show how we could export our data from a gridview to excel sheet in Asp.net- c# (Export to excel data) First of all, just take a gridview on your design page and fill data as below :: protected void Page_Load(object sender, EventArgs e)     {         if (!IsPostBack)         {            BindGrid();         }     }     protected void BindGrid()     {         gvDetails.DataSource = this.getData();         gvDetails.DataBind();     }     private DataTable getData()     {         DataTable dt = new DataTable();         dt.Columns.Add("UserId", ...

Fill/ Bind/ Load DropDown List with the data from the Sql Server table.

In this blog i am going to explain how to dynamically Bind/Load/Fill DropDownList with the data from the Sql Server table. First of all n the design page(.aspx) place a DropDownList control as:          <asp:DropDownList ID="ddlistQualification" runat="server" RepeatColumns="2">         </asp:DropDownList> Now Create a connectionstring in the web.config file under configuration tag as: <configuration> <connectionStrings>                                 <add name="Conn" connectionString="Data Source=.;Initial Catalog=My_DB;Integrated Security=True"/> </connectionStrings> </configuration> Now, Create a Database "My_DB" in sql server and also create a table "Qualifications" having 2 fields :          "Qualification_Id int identity(1,1) primary key not null, Qualification varchar(50)" ...

Bind data to checkbox list from database in C# (Fiil Checkbox list from table in asp.net c#)

In this blog i am going to explain how to dynamically Bind/Load/Fill CheckBoxList with the data from the Sql Server table. First of all n the design page(.aspx) place a CheckBoxList control as:          <asp:CheckBoxList ID="ckQualification" runat="server" RepeatColumns="2">         </asp:CheckBoxList> Now Create a connectionstring in the web.config file under configuration tag as: <configuration> <connectionStrings>                                 <add name="Conn" connectionString="Data Source=.;Initial Catalog=My_DB;Integrated Security=True"/> </connectionStrings> </configuration> Now, Create a Database "My_DB" in sql server and also create a table "Qualifications" having 2 fields :           "Qualification_Id int identity(1,1) primary key not null, Qualification...

Check Empty/ Null value in the Data Table and replace null values with custom value

Image
Below i have used a gridview and putted some data inside gridview containing few null values inside data columns and replacing all null values with string "Data Not Avaialable" :: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; public partial class ValueAgainstNull : System.Web.UI.Page {     protected void Page_Load(object sender, EventArgs e)     {         if (!IsPostBack)         {             BindData();         }     }     protected void BindData()     {         DataTable dt = new DataTable();         dt.Columns.Add("UserId", typeof(Int32));         dt.Columns.Add("Name", typeof(string));         dt.Columns.Add("Designation", typeof(s...
go to top image