This project help you to build some knowledge about data connections of database and how to use tools in c#.
So,let's get started.
first drag and drop the required elements to bulit a phonebook project
Elements required:
4 Textboxes
4 Labels
3 buttons
1 Datagridview
Then create a database , which is required to store contact information of people, follow the below steps to create a database.
So. I'm creating a local SQL database file.
1.Right click on "Data Connections" which is present in the server explorer and Click on "Add Connection"
2. Select "Microsoft SQL server database file" as data source in the pop-up window, then give a name to your database file eg: phonebook and after this a database file will appears on the server explorer.
3.Now in this we have to create a table in database which is used to store your phone book details
Double click on "phonebook.mdf " file and go to table section and right click on it
This will show a list of options, in that select add new table then after write down your column names in a window and select a datatype of it and save the table by clicking "ctrl + s"
4.Now create a data source which is required to access by datgridview and it will show the data present in the database file.
For this, Right click on "phonebook.mdf " file and click on "add new data source", then click on next button until thw below window appears, in this select your connection string which is related to your database file name.
Now , data source has been created now add data source to your datagridview ,for this go to datagridview1 properties and go to data source and select "table1bindingsource".
The source code :
First we have to create a sql connection ,which is used to maintain link between form and database,so i created sql connection by clicking phonebook.mdf
and then in property window "connection string will be appear ,then cop that and paste it in the green highlighted area.
Then change the textbox names as
firstnameTextBox
lastnameTextBox
emailTextBox
mobileTextBox
public partial class Form1 : Form
{
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=c:\users\aravind\documents\visual studio 2010\Projects\phonebook1\phonebook1\phonebook.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
public Form1()
{
InitializeComponent();
}
save button:
private void button1_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "INSERT INTO Table1 VALUES('"+firstnameTextBox.Text+"','"+lastnameTextBox.Text+
"','"+emailTextBox.Text+"','"+mobileTextBox.Text+"')";
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("data inserted");
empty();
Application.Exit();
}
Delete button
private void button4_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "DELETE FROM Table1 WHERE firstname='" + firstnameTextBox+ "'";
cmd.ExecuteNonQuery();
MessageBox.Show("data deleted");
con.Close();
}
now we have to create a extra method which is used to clear the textboxes.
public void empty()
{
firstnameTextBox.Text = "";
lastnameTextBox.Text = "";
emailTextBox.Text = "";
mobileTextBox.Text = "";
}
Cancel button:
private void button2_Click(object sender, EventArgs e)
{
empty();
}
For downloading source code click on download button below