CRUD Operation Using Text File. (Create Phone Directory Using Text File)
Hello everyone!
Today's article is about "CRUD" operation using text file. We have seen many application which uses Database or "SQL" to create CRUD application, but in this article you will learn about how you can create "CRUD" application using text file. The application which we are going to create is windows based and developed on Visual Studio by using C# programming language.
Application we creating is Phone Directory in which user can Add, Update, Delete, Search details about user.
First of all open Visual Studio, select New Project > Select Windows Forms Application. (If Windows Form Application is not visible then select Visual C# or C# from left panel)
STEP 1
Design a user interface according to this interface.
Items included in this user interface.
STEP 2
Implementing Method which add user details to text file.
Above method takes user details as parameter and save to text. here we use "|" to separate the values.
Now insert following code in button click event of Add button. this code takes text from text box and send to "SaveData();" method.
PS (Ignore showData(), will implement in next step)
Hare is video how you can implement this code.
STEP 3
Implementing method which show data to Grid View.
public void showData()
{
dataGridView1.Rows.Clear();
String[] data = File.ReadAllLines("UserData.txt");
//now spliting data
// and we will set data on grid view one by one in loop
for (int i = 0; i < data.Length; i++)
{
String[] row = data[i].Split('|');
dataGridView1.Rows.Add(row[0], row[1], row[2], row[3], "Edit/Delete");
}
}
Above code read all data from text file and display on Data Grid View. Breaking each line of text file and converting into array and pass the value of array into Data Grid View.
Here is the video how you can display data on Grid View.
STEP 4
Implementing method that delete user details.
There will be Edit/Delete button beside every record on grid view, where we click on that button, details of that specific user will be show on text boxes and unique key will also generated of that specific user details with the help of following code. PS (In video of part 4 you will learn how you can enable this button. Not visible by default).
if (e.ColumnIndex == 4)
{
String[] data = File.ReadAllLines("UserData.txt");
string row = data[e.RowIndex];
String[] rowarr = row.Split('|');
textBox1.Text = rowarr[1];
textBox2.Text = rowarr[2];
textBox3.Text = rowarr[3];
uID = e.RowIndex;
}
Add following code to button click event of Delete button.
dataGridView1.Rows.Clear();
String[] data = File.ReadAllLines("UserData.txt");
File.WriteAllText("UserData.txt",String.Empty);
StreamWriter sw = new StreamWriter("UserData.txt",true);
for (int i = 0; i < data.Length; i++)
{
if (i != uID)
{
sw.WriteLine(data[i]);
}
}
sw.Close();
showData();
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
Here is the video how you can delete data from text file.
STEP 5
Implementing method which update user details.
Add following code to button click event of Update button.
dataGridView1.Rows.Clear();
String[] data = File.ReadAllLines("UserData.txt");
data[uID] = ((uID + 1).ToString()) + "|" + textBox1.Text + "|" + textBox2.Text + "|" + textBox3.Text;
File.WriteAllText("UserData.txt",String.Empty);
StreamWriter sw = new StreamWriter("UserData.txt",true);
for (int i = 0; i < data.Length; i++)
{
sw.WriteLine(data[i]);
}
sw.Close();
showData();
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
Above code update data with the help of unique id which generated using click button beside every record in Grid View. Simply takes take line which we want to update and replace it with text box string.
Here is the video how you can update data.
Please Like/Subscribe/Share CodeGranted
If you have any query feel free to comment
THANK YOU
Today's article is about "CRUD" operation using text file. We have seen many application which uses Database or "SQL" to create CRUD application, but in this article you will learn about how you can create "CRUD" application using text file. The application which we are going to create is windows based and developed on Visual Studio by using C# programming language.
Application we creating is Phone Directory in which user can Add, Update, Delete, Search details about user.
First of all open Visual Studio, select New Project > Select Windows Forms Application. (If Windows Form Application is not visible then select Visual C# or C# from left panel)
STEP 1
Design a user interface according to this interface.
Items included in this user interface.
- Text Box (4)
- Button (6)
- Label (6)
- Data Grid View (1)
- Calendar View (Optional)
Implementing Method which add user details to text file.
public void SaveData(string name, string phoneNumber, string email)
{
string userID = getID();
StreamWriter sw = new StreamWriter("UserData.txt",true);
sw.WriteLine(userID+"|"+name+"|"+phoneNumber+"|"+email);
sw.Close();
}
Above method takes user details as parameter and save to text. here we use "|" to separate the values.
Now insert following code in button click event of Add button. this code takes text from text box and send to "SaveData();" method.
PS (Ignore showData(), will implement in next step)
if (textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != "")
{
string name = textBox1.Text;
string phoneNumber = textBox2.Text;
string email = textBox3.Text;
SaveData(name, phoneNumber, email);
MessageBox.Show("Data Inserted");
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
showData(); //Ignore this now implement this on next step
}
else
{
MessageBox.Show("Please Enter Correct Data");
}
Implementing method which show data to Grid View.
public void showData()
{
dataGridView1.Rows.Clear();
String[] data = File.ReadAllLines("UserData.txt");
//now spliting data
// and we will set data on grid view one by one in loop
for (int i = 0; i < data.Length; i++)
{
String[] row = data[i].Split('|');
dataGridView1.Rows.Add(row[0], row[1], row[2], row[3], "Edit/Delete");
}
}
Above code read all data from text file and display on Data Grid View. Breaking each line of text file and converting into array and pass the value of array into Data Grid View.
Here is the video how you can display data on Grid View.
STEP 4
Implementing method that delete user details.
There will be Edit/Delete button beside every record on grid view, where we click on that button, details of that specific user will be show on text boxes and unique key will also generated of that specific user details with the help of following code. PS (In video of part 4 you will learn how you can enable this button. Not visible by default).
if (e.ColumnIndex == 4)
{
String[] data = File.ReadAllLines("UserData.txt");
string row = data[e.RowIndex];
String[] rowarr = row.Split('|');
textBox1.Text = rowarr[1];
textBox2.Text = rowarr[2];
textBox3.Text = rowarr[3];
uID = e.RowIndex;
}
Add following code to button click event of Delete button.
dataGridView1.Rows.Clear();
String[] data = File.ReadAllLines("UserData.txt");
File.WriteAllText("UserData.txt",String.Empty);
StreamWriter sw = new StreamWriter("UserData.txt",true);
for (int i = 0; i < data.Length; i++)
{
if (i != uID)
{
sw.WriteLine(data[i]);
}
}
sw.Close();
showData();
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
Here is the video how you can delete data from text file.
STEP 5
Implementing method which update user details.
Add following code to button click event of Update button.
dataGridView1.Rows.Clear();
String[] data = File.ReadAllLines("UserData.txt");
data[uID] = ((uID + 1).ToString()) + "|" + textBox1.Text + "|" + textBox2.Text + "|" + textBox3.Text;
File.WriteAllText("UserData.txt",String.Empty);
StreamWriter sw = new StreamWriter("UserData.txt",true);
for (int i = 0; i < data.Length; i++)
{
sw.WriteLine(data[i]);
}
sw.Close();
showData();
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
Above code update data with the help of unique id which generated using click button beside every record in Grid View. Simply takes take line which we want to update and replace it with text box string.
Here is the video how you can update data.
Here we go ! we have successfully created phone directory using text file.
Here is the link of Source Code
Please Like/Subscribe/Share CodeGranted
If you have any query feel free to comment
THANK YOU
CRUD Operation Using Text File. (Create Phone Directory Using Text File)
Reviewed by Unknown
on
7:14 AM
Rating:
The best casino slots games - DRMCD
ReplyDelete1. 시흥 출장샵 Wild Wild Megaways 정읍 출장샵 · 2. Sweet Bonanza · 3. Triple Tigers · 4. Crazy Juicy Diamonds · 5. Gonzo's 김포 출장마사지 Quest · 대전광역 출장안마 6. 영천 출장마사지 Sweet Bonanza · 7. King