Friday 31 May 2013

How to make own custom control in C#

How to make own custom control in C#

Create a Login control that has following features:
1.      Login Id and Password entry fields along with Login button
2.      Database validation feature for Login Id and Password
3.      Properties for database name, table name and any thing else to validate against

Test your control.


Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace lab11
{
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }
        private string v_Database_Name, v_Table_Name, v_Column_ID, v_Column_Password;
        public string Database_Name
        {
            get
            {
                return(v_Database_Name);
            }
            set
            {
                v_Database_Name = value;
            }
        }
        public string Table_Name
        {
            get
            {
                return(v_Table_Name);
            }
            set
            {
                v_Table_Name = value;
            }
        }
        public string Column_ID
                {
                    get
            {
                return(v_Column_ID);
            }
                    set
            {
                v_Column_ID = value;
            }
                }
        public string Column_Password
                {
                    get
                    {
                        return (v_Column_Password);
                    }
                    set
                    {
                        v_Column_Password = value;
                    }
                }
        private void btn_login_Click(object sender, EventArgs e)
        {
            con.ConnectionString=@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\Shoaib\My Documents\Visual Studio 2008\Projects\lab11\lab11\"+v_Database_Name+".mdf;Integrated Security=True;User Instance=True";
            SqlCommand com= new SqlCommand("select * from "+v_Table_Name+" where "+v_Column_ID+"=@ID and "+v_Column_Password+"=@password",con);
            SqlParameter p1= new SqlParameter("@ID",SqlDbType.Int,4,v_Column_ID);
            SqlParameter p2=new SqlParameter("@Password",SqlDbType.NVarChar,8,v_Column_Password);
            p1.Value=tb_id.Text;
            p2.Value=tb_password.Text;
            com.Parameters.Add(p1);
            com.Parameters.Add(p2);
            con.Open();
            SqlDataReader dr=com.ExecuteReader();
            if(dr.Read())
            {
                label4.ForeColor = Color.Green;
                label4.Text="Login Successful !!";
            }
            else
            {
                label4.ForeColor = Color.Red;
                label4.Text="Login Failed !!";
               
            }
            con.Close();
           
        }

        private void tb_id_TextChanged(object sender, EventArgs e)
        {
            label4.Text = "";
        }
        private void tb_password_TextChanged(object sender, EventArgs e)
        {
            label4.Text = "";
        }
    }
}

}

Sceen Shot

No comments:

Post a Comment