WPF-Validations On All Database Operations

In my previous article (which you can see by clicking this link "Click Here") we saw how we can perform all different-different operations using database.

In this article we will see validations on that data. 

Here we prevent the user from entering a value for Student Id that is greater than the Student Id in the database..

Second it will also change the value of the Student Name & it's result when the user changes the value of Student Id in that textbox.

If the user will keep Student Id textbox as empty then another two textbox viz. Student Name 7 Student Result will also empty automatically.

SqlCommand comm = new SqlCommand("SELECT MAX(StudId) FROM Student", conn);

Using the above line it will get the maximum student id from the database, so we can validate the student id easily by getting the maximum number; it will not allow a Student Id to be entered that is greater than maximum number + 1. If the user does enter more than that number then it will show a message and the textbox will be filled by the maximum student id automatically.

temp = Convert.ToInt16(dr.GetValue(0).ToString());

In the above line it will store that maximum number into "temp" variable.

if (txtStudId.Text == String.Empty)

{

conn.Close();

      txtStudName.Text = String.Empty;

      txtStudResult.Text = String.Empty;

}

When the textbox is empty the related value of student in the texbox will not be displayed, so that textbox value keep is empty.

if (Convert.ToInt16(txtStudId.Text) > (temp + 1))

{

MessageBox.Show("You Can't Enter Value Which Is More Than The Database Value.");

      conn.Close();

      txtStudId.Text = temp.ToString();

}

Using the above code it will allow only up to student id + 1. if more than that then it will give a message & textbox will be filled by a maximum of student id.

Here one more thing to keep in mind is that before you change the student id's textbox you must close the connection because we use here the textbox change event and when something changes in that textbox it will automatically fire that event so at that time if you will not close connection before changing then it will throw you exception that "Connection not closed".

if (Convert.ToInt16(txtStudId.Text) == (temp + 1))

{

conn.Close();

      txtStudName.Text = String.Empty;

      txtStudResult.Text = String.Empty;

}

When the user wants to add a new entry using the student id +1 number at that time there are no records found so at that time both textboxes also must be empty.

Main Code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data;
 
namespace WPFAllDatabaseOperationApplication
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
   
    public partial class MainWindow : Window
    {
        SqlConnection conn = new SqlConnection(@"Data
Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MyDatabase.mdf;Integrated Security=True;User Instance=True"
);
        SqlDataReader dr;
        public MainWindow()
        {
            InitializeComponent();
        }
 private void txtStudId_TextChanged(object sender, TextChangedEventArgs e)
       {
           try
           {
               conn.Open();
               int temp=0;
               SqlCommand comm = new SqlCommand("SELECT MAX(StudId) FROM Student", conn);
               dr = comm.ExecuteReader();
               while (dr.Read())
               {
                   temp = Convert.ToInt16(dr.GetValue(0).ToString());
               }
               if (txtStudId.Text == String.Empty)
               {
                   conn.Close();
                   txtStudName.Text = String.Empty;
                   txtStudResult.Text = String.Empty;
               }
               else
               {
                   if (Convert.ToInt16(txtStudId.Text) > (temp + 1))
                   {
                       MessageBox.Show("You Can't Enter Value Which Is More Than The Database Value.");
                       conn.Close();
                       txtStudId.Text = temp.ToString();
                   }
                   if (Convert.ToInt16(txtStudId.Text) == (temp + 1))
                   {
                       conn.Close();
                       txtStudName.Text = String.Empty;
                       txtStudResult.Text = String.Empty;
                   }
               }
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message.ToString());
           }
           finally
           {
               conn.Close();
           }
 
           if (txtStudId.Text == String.Empty)
           {
               conn.Close();
               txtStudName.Text = String.Empty;
               txtStudResult.Text = String.Empty;
           }
           else
           {
               try
               {
                   conn.Open();
                   SqlCommand comm = new SqlCommand("SELECT StudName,StudResult FROM Student WHERE StudId=" + txtStudId.Text +
"", conn);
                   dr = comm.ExecuteReader();
                   while (dr.Read())
                   {
                       txtStudName.Text = dr.GetValue(0).ToString();
                       txtStudResult.Text = dr.GetValue(1).ToString();
                   }
               }
               catch (Exception ex)
               {
                   MessageBox.Show(ex.Message.ToString());
               }
               finally
               {
                   conn.Close();
               }
           }
       }
    }
}

See Below Images Which Covers All the validations :

Validation1.JPG

Validation2.JPG

Validation3.JPG

In the image shown below it will not allow a user to enter more than the student id + 1; if the user does
then an error message is shown.

Validation4.JPG