a.Crud Operations in CSV Files and Text Files Using C# ASP.NET
b.How to delete the rows in csv file
c.How do I delete certain column from .csv file
d.Connect to CSV Data Files using Microsoft Data Access Components
e.Accessing Text Files using ODBC Data Provider
f.How to deleting specific row inside CSVFile>New>Web Site then select “C#”(left side in visual studio) “Empty Web Site”

name it as you want.
now add a aspx page in your website.
now add a folder in your website name it Upload or anything as you want.
right click on it and your add csv file in it.
i have a csv file which contains information about person.
csv file
i have added a class file in my website.
now i have design my aspx page for performing CRUD Operations.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <table class="auto-style1"> <tr> <td>Employeeid</td> <td class="auto-style3"> <asp:TextBox ID="txtemployeeid" runat="server"></asp:TextBox> </td> <td class="auto-style3"> <asp:Button ID="Btngetupdate" runat="server" Text="GetPersonForUpdate" /> </td> <td class="auto-style3"> <asp:Button ID="btnupdate" runat="server" Text="UpdatePerson" /> </td> </tr> <tr> <td>Fristname</td> <td class="auto-style3"> <asp:TextBox ID="txtfirstname" runat="server"></asp:TextBox> </td> <td class="auto-style3"> </td> <td class="auto-style3"> </td> </tr> <tr> <td>lastname</td> <td class="auto-style3"> <asp:TextBox ID="txtlastname" runat="server"></asp:TextBox> </td> <td class="auto-style3"> </td> <td class="auto-style3"> </td> </tr> <tr> <td>Emailid</td> <td class="auto-style3"> <asp:TextBox ID="txtemailid" runat="server"></asp:TextBox> </td> <td class="auto-style3"> </td> <td class="auto-style3"> </td> </tr> <tr> <td>Address</td> <td class="auto-style3"> <asp:TextBox ID="txtaddress" runat="server"></asp:TextBox> </td> <td class="auto-style3"> </td> <td class="auto-style3"> </td> </tr> <tr> <td> </td> <td class="auto-style3"> <asp:Button ID="btnadd" runat="server" Text="Addperson" style="height: 26px" /> </td> <td class="auto-style3"> </td> <td class="auto-style3"> </td> </tr> <tr> <td> </td> <td class="auto-style3"> </td> <td class="auto-style3"> </td> <td class="auto-style3"> </td> </tr> <tr> <td class="auto-style2"> </td> <td class="auto-style4"> <asp:Button ID="btnGet" runat="server" Text="GetPerson" /> </td> <td class="auto-style4"> </td> <td class="auto-style4"> <asp:Button ID="btndelete" runat="server" Text="DeletePerson" /> </td> </tr> </table> <asp:GridView ID="GridView1" runat="server"></asp:GridView> </form> </body> </html>
Now i have written logic for Crud operation please read all code patiently.In this Post you can also learn these points.
Description:In this post you will also learn these point.
1.Create CSV and save on local/server automatically,save csv file in folder in
2.How to Add Rows to a DataTable ,How to add a new row to c# DataTable ,Add new rows to a DataTable pro grammatically C#
3.How To Compare and delete datatable row using C#,c# – Deleting specific rows from DataTable ,
c# – Remove Row from DataTable Depending on Condition
4.Find and Update cell in DataTable,How to update records inside datatable only in c#,Updating Existing Records in a Dataset,c# How to Edit a row in the datatable
Insert ,Update and Delete in DataTable using c#
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Microsoft.VisualBasic.FileIO; using System.Data.Odbc; using System.Text; using System.IO; public partial class Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } private void GetPerson() { string spath = System.Web.HttpContext.Current.Server.MapPath("~/Upload"); //odbc connection to the csv file string strConnString = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + spath.Trim() + ";Extensions=asc,csv,tab,txt;Persist Security Info=False"; OdbcConnection objCSV = new OdbcConnection(strConnString); OdbcCommand Cmd = new OdbcCommand("select * from Person.csv ", objCSV); objCSV.Open(); OdbcDataAdapter adp = new OdbcDataAdapter(Cmd); DataSet ds = new DataSet(); adp.Fill(ds); objCSV.Close(); GridView1.DataSource = ds; GridView1.DataBind(); } private Person GetPerson(string employeeid) { string spath = System.Web.HttpContext.Current.Server.MapPath("~/Upload"); //odbc connection to the csv file string strConnString = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + spath.Trim() + ";Extensions=asc,csv,tab,txt;Persist Security Info=False"; OdbcConnection objCSV = new OdbcConnection(strConnString); OdbcCommand Cmd = new OdbcCommand("select * from Person.csv ", objCSV); objCSV.Open(); OdbcDataReader rdr = Cmd.ExecuteReader(); Person _person = new Person(); while (rdr.Read()) { if (rdr["Employeeid"].ToString() == employeeid) { _person.Employeeid =Convert.ToInt32(rdr["Employeeid"]); _person.Fristname = rdr["Fristname"].ToString(); _person.lastname = rdr["lastname"].ToString(); _person.Emailid = rdr["Emailid"].ToString(); _person.Address = rdr["Address"].ToString(); } } objCSV.Close(); return _person; } //adding new row to the datatable private bool AddPerson(Person obj) { try { string spath = System.Web.HttpContext.Current.Server.MapPath("~/Upload"); //odbc connection to the csv file string strConnString = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + spath.Trim() + ";Extensions=asc,csv,tab,txt;Persist Security Info=False"; OdbcConnection objCSV = new OdbcConnection(strConnString); OdbcCommand Cmd = new OdbcCommand("select * from Person.csv", objCSV); OdbcDataAdapter adp = new OdbcDataAdapter(Cmd); DataSet ds = new DataSet(); adp.Fill(ds, "csv"); DataTable dt = ds.Tables["csv"]; //adding new row to the datatable DataRow newRow = dt.NewRow(); newRow["Employeeid"] = obj.Employeeid; newRow["Fristname"] = obj.Fristname; newRow["lastname"] = obj.lastname; newRow["Emailid"] = obj.Emailid; newRow["Address"] = obj.Address; dt.Rows.Add(newRow); //this function for rewrite csv file Replacefile(ds.Tables["csv"]); return true; } catch { return false; } } //updating datatable with condition private bool UpdatePerson(string employeeid, Person obj) { try { string spath = System.Web.HttpContext.Current.Server.MapPath("~/Upload"); //odbc connection to the csv file string strConnString = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + spath.Trim() + ";Extensions=asc,csv,tab,txt;Persist Security Info=False"; OdbcConnection objCSV = new OdbcConnection(strConnString); OdbcCommand Cmd = new OdbcCommand("select * from Person.csv", objCSV); OdbcDataAdapter adp = new OdbcDataAdapter(Cmd); DataSet ds = new DataSet(); adp.Fill(ds, "csv"); DataTable dt = ds.Tables["csv"]; foreach (DataRow rdr in dt.Rows) { if (rdr["Employeeid"].ToString() == employeeid) { rdr["Employeeid"] = obj.Employeeid; rdr["Fristname"] = obj.Fristname; rdr["lastname"] = obj.lastname; rdr["Emailid"] = obj.Emailid; rdr["Address"] = obj.Address; } } Replacefile(ds.Tables["csv"]); return true; } catch(Exception ex) { return false; } } //remove or delete row from datatable private int Delete(string employeeid) { try { string spath = System.Web.HttpContext.Current.Server.MapPath("~/Upload"); //odbc connection to the csv file string strConnString = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + spath.Trim() + ";Extensions=asc,csv,tab,txt;Persist Security Info=False"; OdbcConnection objCSV = new OdbcConnection(strConnString); objCSV.Open(); OdbcCommand oCmd = new OdbcCommand("select * from Person.csv ", objCSV); OdbcDataAdapter adp = new OdbcDataAdapter(oCmd); DataSet ds = new DataSet(); adp.Fill(ds, "csv"); objCSV.Close(); int nor = 0; // number of rows deleted DataTable dt = ds.Tables["csv"]; DataRow[] toBeDeleted; toBeDeleted = dt.Select("employeeid=" + employeeid + ""); if (toBeDeleted.Length > 0) { foreach (DataRow dr in toBeDeleted) { dt.Rows.Remove(dr);//removing row nor++; } } Replacefile(ds.Tables["csv"]); return nor; } catch(Exception ex) { return 0; } } //creating csv file and save it in a folder. private void Replacefile(DataTable datatable) { // this method for creating csv file char seperator = ','; string spath = System.Web.HttpContext.Current.Server.MapPath("~/Upload"); string path = spath + "/Person.csv"; StringBuilder sb = new StringBuilder(); for (int i = 0; i < datatable.Columns.Count; i++) { sb.Append(datatable.Columns[i]); if (i < datatable.Columns.Count - 1) sb.Append(seperator); } sb.AppendLine(); foreach (DataRow dr in datatable.Rows) { for (int i = 0; i < datatable.Columns.Count; i++) { sb.Append(dr[i].ToString()); if (i < datatable.Columns.Count - 1) sb.Append(seperator); } sb.AppendLine(); } //write all text to csv file File.WriteAllText(path, sb.ToString()); } protected void btnGet_Click(object sender, EventArgs e) { GetPerson(); } protected void btnadd_Click(object sender, EventArgs e) { Person obj = new Person(); obj.Employeeid =Convert.ToInt16(txtemployeeid.Text); obj.Fristname = txtfirstname.Text; obj.lastname = txtlastname.Text; obj.Emailid = txtemailid.Text; obj.Address = txtaddress.Text; bool status=AddPerson(obj); if (status) { Response.Write("<script>alert('insert successfully!')</script>"); GetPerson(); } else { Response.Write("<script>alert('Try again!')</script>"); GetPerson(); } } protected void Btngetupdate_Click(object sender, EventArgs e) { Person obj = GetPerson(txtemployeeid.Text); txtemployeeid.Text = obj.Employeeid.ToString(); txtfirstname.Text = obj.Fristname; txtlastname.Text = obj.lastname; txtemailid.Text = obj.Emailid; txtaddress.Text = obj.Address; } protected void btnupdate_Click(object sender, EventArgs e) { Person obj = new Person(); obj.Employeeid = Convert.ToInt16(txtemployeeid.Text); obj.Fristname = txtfirstname.Text; obj.lastname = txtlastname.Text; obj.Emailid = txtemailid.Text; obj.Address = txtaddress.Text; bool status = UpdatePerson(txtemployeeid.Text,obj); if (status) { Response.Write("<script>alert('updated successfully!')</script>"); GetPerson(); } else { Response.Write("<script>alert('Try again!')</script>"); GetPerson(); } } protected void btndelete_Click(object sender, EventArgs e) { int i = Delete(txtemployeeid.Text); if (i > 0) { Response.Write("<script>alert('deleted successfully!')</script>"); GetPerson(); } else { Response.Write("<script>alert('Try again!')</script>"); GetPerson(); } } }
The post Insert delete update records in CSV file -ASP.NET appeared first on Software Development | Programming Tutorials.
Read More Articles
- Write a value which contain comma to a CSV file in c#?
- Reading CSV File with cells containing commas c#
- Split CSV with columns may contain ‘,’ Comma C#
- [Simple Way]-Cascading DropDownList in Asp.Net Mvc Using Jquery Ajax
- [Simple Way]-How to get data from database using JQuery Ajax in asp net MVC
- [Simple Way]-ASP.NET Core Upload Multiple File Web API model
- [Simple Way]- Image Upload in .NET Core Web API
- [Easy Way]-Receive File and other form data together in ASP.NET Core Web API
- Replace image in word document using C#
- How to add new rows to an existing word document table in C#
- Simple Way Find and replace text in Word document using C#
- Implementing “Remember Me” Feature in ASP.NET MVC
- [Solved]-Cookie loses value when page is changed in MVC
- How to post File Upload and other form fields to one action Asp .Net MVC C#
- How To Post File and Data to API using HttpClient C#
- Create ASP.NET Core Web API Without Entity Framework
- .NET Core Web API Using Code First Entity Framework Approach
- Create Asp.Net Core Web Api Using Entity Framework Database First
- Registration form with image upload in MVC using jquery Ajax
- How to make an Inline editable table in MVC using jquery?
- CRUD operation using partial view in MVC with modal popup
- Insert Update Delete Using Jquery Ajax and Modal Popup in Mvc
- Crud Operations in MVC Without Entity Framework
- Create Login,Signout and Registration in Asp .Net Mvc Using Entity
- Export Gridview to Excel and Csv in Asp .Net With Formatting Using c#
- How to Display Binary Image in Gridview from Database in Asp .Net c#
- [Solved]-How to Upload pdf file using jquery MVC?
- [Solved]-Uploading both data and files in FormData using Ajax MVC
- C# -Saving a base64 string as an image into a folder on server in Web Api
- [Solved]-Download pdf file from link and save in local file folder in Asp .Net
- [Solved]-Delete Files older than X Months,Days,Minute in a Directory- C# .NET
- [Solved]-LEFT OUTER JOIN in LINQ With Where clause in C#
- INNER JOIN,RIGHT JOIN,LEFT JOIN USING LINQ IN Multiple Table C#
- [Solved]-Convert Base64 To Image And Save in folder and Display it- C#
- [Solved]-How to Overlay Two Images in .NET-C#
- How to Create Multilingual Website in Asp .Net
- C# – How to add double quotes to a string that is inside a variable
- Update an Image with Upload Button For Each Row in Gridview-Asp .Net
- How to Bind Data in DataList Control in Asp.Net – C#
- Upload and Display an Image in Gridview From Image Path in Database -C#