Simple ContactBook in WPF

To develop a simple contact book in WPF use the following procedure.

1. Open Visual Studio 2010.
2. Go to "File" -> "New" -> "Project..." or press Ctrl+Shift+N.
3. Select "Template Visual C#" -> "Windows" and now select "WPF Application". Then provide any name and location to save this application.
4. Make the following form like this:
You can create this form using the following XAML code:
<Window x:Class="MyphoneBook.MainWindow"
        Title="ADDRESS BOOK BY SOURABH SOMANI" Height="506" Width="763" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" WindowStyle="None">
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#B4FFFFFF" Offset="0" />
                <GradientStop Color="Black" Offset="1" />
                <GradientStop Color="#B4FFFFFF" Offset="0.254" />
        <TextBlock Height="23" HorizontalAlignment="Left" Margin="29,12,0,0" Name="textBlock1" Text="NAME:" VerticalAlignment="Top" />
        <TextBlock Height="23" HorizontalAlignment="Left" Margin="28,40,0,0" Name="textBlock2" Text="HOME:" VerticalAlignment="Top" />
        <TextBlock Height="23" HorizontalAlignment="Left" Margin="21,69,0,0" Name="textBlock3" Text="MOBILE:" VerticalAlignment="Top" />
        <TextBlock Height="23" HorizontalAlignment="Left" Margin="323,12,0,0" Name="textBlock4" Text="E-MAIL:" VerticalAlignment="Top" />
        <TextBlock Height="23" HorizontalAlignment="Left" Margin="311,42,0,0" Name="textBlock5" Text="ADDRESS:" VerticalAlignment="Top" />

       <DataGrid AutoGenerateColumns="False" Height="259" HorizontalAlignment="Left" AlternatingRowBackground="Red" Margin="12,174,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="717" SelectionChanged="dataGrid1_SelectionChanged" RowBackground="#FF4EFF00">
                <DataGridTextColumn Binding="{Binding NAME}" Width="100" Header="NAME"/>
                <DataGridTextColumn Binding="{Binding HOME}" Width="100" Header="HOME"/>
                <DataGridTextColumn Binding="{Binding MOBILE}" Width="100" Header="MOBILE"/>
                <DataGridTextColumn Binding="{Binding EMAIL}" Width="100" Header="EMAIL"/>
                <DataGridTextColumn Binding="{Binding ADDRESS}" Width="100" Header="ADDRESS"/>

        <TextBox Height="23" HorizontalAlignment="Left" Margin="79,11,0,0" Name="textBox1" VerticalAlignment="Top" Width="220" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="79,40,0,0" Name="textBox2" VerticalAlignment="Top" Width="220" />
        <TextBox Height="23" HorizontalAlignment="Right" Margin="0,102,442,0" Name="textBox3" VerticalAlignment="Top" Width="177" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="378,12,0,0" Name="textBox4" VerticalAlignment="Top" Width="220" />
        <TextBox Height="84" HorizontalAlignment="Left" Margin="378,41,0,0" Name="textBox5" VerticalAlignment="Top" Width="220" TextWrapping="Wrap" AcceptsReturn="True"/>
        <Button Content="SAVE" Height="33" HorizontalAlignment="Left" Margin="621,53,0,0" Name="SaveBtn" VerticalAlignment="Top" Width="75" Click="SaveBtn_Click" />
        <Button Content="EDIT" Height="33" HorizontalAlignment="Left" Margin="621,92,0,0" Name="EditBtn" VerticalAlignment="Top" Width="75" Click="EditBtn_Click" />
        <Button Content="NEW" Height="33" HorizontalAlignment="Left" Margin="621,12,0,0" Name="NewBtn" VerticalAlignment="Top" Width="75" Click="NewBtn_Click" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="533,145,0,0" Name="textBox6" VerticalAlignment="Top" Width="196" TextChanged="textBox6_TextChanged" />
        <TextBlock Height="23" HorizontalAlignment="Left" Margin="483,146,0,0" Name="textBlock6" Text="SEARCH:" VerticalAlignment="Top" />
        <ComboBox Height="23" HorizontalAlignment="Left" Margin="79,69,0,0" Name="comboBox1" VerticalAlignment="Top" Width="199" SelectionChanged="comboBox1_SelectionChanged" Text="" SelectedIndex="-1" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="79,102,0,0" Name="textBox7" VerticalAlignment="Top" Width="37" />
        <TextBlock Height="23" HorizontalAlignment="Left" Margin="15,437,0,0" Name="textBlock7" Text="Designed By: Sourabh Somani, Contect: [email protected], Mewar University" VerticalAlignment="Top" Width="714" Foreground="#FFDA3030" FontWeight="Bold" FontSize="16" />
        <Border BorderThickness="5" Height="489" HorizontalAlignment="Left" Name="border1" VerticalAlignment="Top" Width="763">
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="Black" Offset="1" />
                    <GradientStop Color="Red" Offset="0" />
                    <GradientStop Color="#FED5A400" Offset="0.164" />
                    <GradientStop Color="#FE00AD28" Offset="0.32" />
                    <GradientStop Color="#FE007983" Offset="0.484" />
                    <GradientStop Color="#FE000651" Offset="0.68" />
                    <GradientStop Color="#FEFF00FF" Offset="0.82" />

5. Now open SQL Server Management Studio 2008 and create a database (for example I am creating it with the name "ADDRESS_BOOK").
6. Now create 2 tables in it.
(i)  Provide the first name as "Address".
(ii) Provide the second name a "country".
7. Now insert the values of the country table using the "country.txt" given in the uploaded RAR File.
8. Now the next step is to return to Visual Studio and do the following:

a. First go to "<FileName>.xaml.cs"
b. Include the two namespaces as in "using System.Data;"and "using System.Data.SqlClient;"
c. Now define the following variables in the class:

SqlConnection con;
SqlCommand cmd;
DataTable dtForContry;
DataTable dtForGridView;
SqlDataAdapter adpt;
d. Now in the MainWindow() constructor define following:
            EditBtn.IsEnabled = false;
            textBox7.IsReadOnly = true;

            con=newSqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=ADDRESS_BOOK;Integrated Security=True");
            string qry ="select * from country";
            adpt = newSqlDataAdapter(qry, con);
            dtForContry = newDataTable();
            comboBox1.ItemsSource = dtForContry.DefaultView;
            comboBox1.DisplayMemberPath = dtForContry.Columns[2].ToString();
            comboBox1.SelectedValuePath = dtForContry.Columns[6].ToString();
            if (textBox6.Text ==string.Empty)

e. Write Following code for the "New" button click event: 
 textBox1.Text = textBox2.Text = textBox3.Text =
textBox4.Text = textBox5.Text=textBox7.Text = string.Empty;
            EditBtn.IsEnabled = false;
            SaveBtn.IsEnabled = true;


f. For the "Save" button write the following code:
 string qry = "insert
into Address(NAME,HOME,MOBILE,EMAIL,ADDRESS) VALUES('" +textBox1.Text + "','" 
+textBox2.Text + "','" +textBox7.Text + textBox3.Text + "', '"+ textBox4.Text + "','" +textBox5.Text + "')";
            cmd = new SqlCommand(qry, con);
            if (cmd.ExecuteNonQuery() == 1)
                MessageBox.Show("Successfully Save","Successful");
                MessageBox.Show("Sorry Invalid Entry","Error In Saving",MessageBoxButton.OK,MessageBoxImage.Error);

g. For the ccombo-box selection event use the following code:
 textBox7.Text ="+"+comboBox1.SelectedValue.ToString()+" - ";

h: For the grid view selection change event use the following code:
 if (dataGrid1.SelectedIndex != -1)
                int temp = dataGrid1.SelectedIndex;
                if (dataGrid1.SelectedIndex < dtForGridView.Rows.Count)
                    textBox1.Text = dtForGridView.Rows[temp][1].ToString();
                    textBox2.Text = dtForGridView.Rows[temp][2].ToString();
                    textBox3.Text = dtForGridView.Rows[temp][3].ToString();
                    textBox4.Text = dtForGridView.Rows[temp][4].ToString();
                    textBox5.Text = dtForGridView.Rows[temp][5].ToString();
                    EditBtn.IsEnabled = true;
                    SaveBtn.IsEnabled = false;
            dataGrid1.SelectedIndex = -1;

I: For the Edit button click use the following code:

string qry = "update Address SET NAME='" + textBox1.Text + "',HOME='" + textBox2.Text + "',MOBILE='" +
textBox3.Text + "',EMAIL='" + textBox4.Text + "',ADDRESS='" + textBox5.Text + "'";
            cmd = newSqlCommand(qry, con);
            if (cmd.ExecuteNonQuery() == 1)
                MessageBox.Show("Successfully Save", "Successful");
                MessageBox.Show("Sorry Invalid Entry", "Error In Saving",MessageBoxButton.OK,MessageBoxImage.Error);

After doing all that, your output will be as in the following:


Up Next
    Ebook Download
    View all
    View all