Thursday 18 October 2012

Checking validations for user controls in Windows Application using C#.net. || Email validation in Windows Application using C#.net.

To implement the validations for user controls in Windows Forms Application you need to follow the below steps :

Step1 :

First you need to design a table and create a stored procedure in Ms Access 2007 to insert the records into database.


Step2:

On the File menu, click New Project. Select Windows Forms Application as your project type.

Design the Form using controls from Toolbox.

Step3: 
Now open the Form.cs page and write the following source code.


Form.cs Code : 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Text.RegularExpressions;
 
namespace Sample
{
    public partial class Sample_Example : Form
    {
        OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\App_Data\SampleDB.accdb; Jet OLEDB:Database Password=1234;Persist Security Info=False");

        public Sample_Example()
        {
            InitializeComponent();
        }


        private void btnSave_Click(object sender, EventArgs e)

        {

            con.Open();
            OleDbDataAdapter da = new OleDbDataAdapter("Select * from SampleDB_CreateClient Where Client_Name='" + txtClientName.Text + "'  ", con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            con.Close();

            if (txtClientName.Text == string.Empty || txtContactPerson.Text == string.Empty || txtAdress.Text == string.Empty || txtContactNo.Text == string.Empty || txtEmailId.Text == string.Empty)
            {

                MessageBox.Show("Please fill all the fields.", "Alert!!");

            }
            else if (dt.Rows.Count > 0)
            {
                MessageBox.Show("Client name already exists.", "Warning");
            }

            else
            {
                string pattern = null;
                pattern = "^([0-9a-zA-Z]([-\\.\\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})$";

                if (Regex.IsMatch(txtEmailId.Text, pattern))
                {
                    OleDbCommand cmd = new OleDbCommand("SampleDB_ClientInsert", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Client_Name", txtClientName.Text);
                    cmd.Parameters.AddWithValue("@Contact_Person", txtContactPerson.Text);
                    cmd.Parameters.AddWithValue("@Address", txtAdress.Text);
                    cmd.Parameters.AddWithValue("@Contact_No", txtContactNo.Text);
                    cmd.Parameters.AddWithValue("@Email_Id", txtEmailId.Text);
                    cmd.Parameters.AddWithValue("@CreatedDate", System.DateTime.Now.ToString("dd/MM/yyyy"));

                    con.Open();
                    int result = 0;
                    result = cmd.ExecuteNonQuery();
                    con.Close();
                    if (result > 0)
                    {
                        MessageBox.Show("Record inserted successfully.", "Alert");
                    }
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Invalid email address");
                }
            }
        }
    }
} 

 


Step4:
Now build the Solution and Debug it for the output.
 

No comments:

Post a Comment