Implementing database concepts to develop an application in agile approach : Part 1.3 (Coding)

Hello everyone. In my last two post, i have showed how to create a database according to the user's story. In this post, i will show you how to develop/code the application to access the database. I am assuming you all know how to create an application in visual studio 2008 and run it. So, i am not discussing the steps of creating a windows application in Visual Studio IDE. If you have problem in this, please find my other posts in where i have discussed about creating the desktop application in C#. Ok. Lets start.

  1. Open Visual Studio 2008
  2. Create a desktop application named ‘ABCUniversityApplication’
  3. Create a windows form named ‘StudentEntryUI’
  4. We have found an object student from our problem domain. So create a class Student which have name, email address, address and phone number as its data. Create a constructor to assign these values when creating a new student object. 

public class Student
      {
  private string name;
        private string emailAddress;
        private string address;
        private string phoneNumber;       

public Student(string name,string emailAddress, string address, string phoneNumber)
        {
            this.name = name;
            this.emailAddress = emailAddress;
            this.address = address;
            this.phoneNumber = phoneNumber;
        }

       public string Name
       {
         get { return name; }
       }

       public string EmailAddress
       {
         get { return emailAddress; }
       }

       public string Address
       {
         get { return address; }
       }

       public string PhoneNumber
       {
         get { return phoneNumber; }
       }

    }
      
  5.      Now create a user interface like the given figure


     6. Now take a note. Your ‘Student’ class holds the student data. Your UI class takes data from user. So   you need a class which will check the validity of the data input by the user. That’s why you need a manager  class which will perform different works related with the student object. Create a ‘StudentManager’ class       which will take data from UI and pass it to another class for saving.
public class StudentManager
       {
        public void SaveStudentManager(Student student)
        {
            StudentGateway studentGateway=new StudentGateway();
            studentGateway.SaveStudentIntoDatabase(student);
        }
      }


    7. Create the ‘StudentGateway’ class for saving the student object into the database.

    To do so, you need to use some built in class of C#.

a.       Create a connection string which will be used as a parameter to establish a connection with the database
string connectionString = @"server=Magicbox\SQLEXPRESS; Integrated Security = SSPI; database = ABCUniversityDatabase";
b.      To create a connection with the database, you need to use SqlConnection class. To use it, you have to import system’s SqlClient directive into your code
c.       Create an object of that class.
SqlConnection connection = new SqlConnection(connectionString);
d.      Open the connection of the databse.
connection.Open();
e.       Now create the insert query string for execution to save the data into the database.
Since your Student class doesn’t have the get properties of the data, change your Student class adding the get part for each of the data.
         string insertQueryString =
   @"INSERT INTO t_Student VALUES ('" +student.Name + @"','" +student.EmailAddress + @"','" +student.Address + @"','" +student.PhoneNumber + @"')";
f.       Now you need a SqlCommand object to execute the query you have built to save the data
SqlCommand command = new SqlCommand(insertQueryString, connection);
g.       Now execute the command
command.ExecuteNonQuery();
h.      So at the end, your StudentGateway class will have the following code snippet
    public class StudentGateway
    {
              public void SaveStudentIntoDatabase(Student student)
        {
string connectionString = @"server=Magicbox\SQLEXPRESS; Integrated Security = SSPI; database = ABCUniversityDatabase";
SqlConnection connection = new SqlConnection(connectionString);
            connection.Open();

                  string insertQueryString =
            @"INSERT INTO t_Student VALUES (
                                           '" +student.Name + @"',
                                           '" +student.EmailAddress + @"',
                                           '" +student.Address + @"',
                                           '" +student.PhoneNumber + @"'
                                        )";
SqlCommand command = new SqlCommand(insertQueryString, connection);
            command.ExecuteNonQuery();
        }
}
  1. Now go back to your UI class. Write down the following code into your saveButton method block
        private void saveButton_Click(object sender, EventArgs e)
        {
Student student=new Student(nameTextBox.Text,emailAddressTextBox.Text,
addressTextBox.Text,phoneNumberTextBox.Text);
            StudentManager studentManager = new StudentManager();
            studentManager.SaveStudentManager(student);
MessageBox.Show(student.Name+"'s information is saved into database");
nameTextBox.Text = "";
            emailAddressTextBox.Text = "";
            addressTextBox.Text = "";
phoneNumberTextBox.Text = "";           
        }
  1. Now build the application and run it. Enter the data given like the following figure and click on the save button




  1. If the data is saved, the messagebox will appear showing you the success of the saving of your data.
  2. Now go to your Sql Server Management Studio Express and right click on t_Student table
  3. Click ‘Open Table’ option to check whether our data is really saved or not
  4. You will see the following figure


  1. Thus we have saved a sample data into the database through an application.

Comments

Popular posts from this blog

Dropbox as SVN Repository

While Loop and Variable Casting in SQL Script