Google+ Followers

Connect

Get Updates Via Email

Enter your email address:

Delivered by FeedBurner

Monday, 8 July 2013

Login and Registration forms in C# windows application with Back end Microsoft SQL Server for data access


In this article, I'm gonna share about how to make login and register form with MS SQL database;

1. Flow Chart Logic





















2. Normal Features




3. Form Designs
Login Form Design


Sign in Form Design



Password Retrieve Form



4. Database Design and SQL queries and Stored Procedure

Create new Database as "schooldata"
create table registerdata
(
 ID int identity,
 Username nvarchar(100),
 Password nvarchar(100),
 Fullname  nvarchar(100),
 MobileNO nvarchar(100),
 EmailID nvarchar(100) 
)

select * from registerdata

create procedure regis
(
 @Username as nvarchar(100),
 @Password as nvarchar(100),
 @Fullname as nvarchar(100),
 @MobileNO as nvarchar(100),
 @EmailID as nvarchar(100) 
)
as begin
insert into registerdata (Username, Password, Fullname, MobileNO,EmailID)
values (@Username, @Password, @Fullname, @MobileNO, @EmailID)
end


5. App.config Settings

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <configSections>
  </configSections>
  <connectionStrings>
    <add name="ConnData"
        connectionString="Data Source=localhost\sqlexpress;Initial Catalog=Schooldata;Integrated Security=True"
        providerName="System.Data.SqlClient" />
  </connectionStrings>
  
</configuration>


6. SignIn Form Coding

 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.Sql;
using System.Data.SqlClient;
using System.Configuration;
using System.Net;
using System.Collections.Specialized;

namespace SchoolData
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SigninForm sf = new SigninForm();
            sf.Show();
            this.Hide();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if ((textBox1.Text != "") && (textBox2.Text != ""))
            {
                string constring = ConfigurationManager.ConnectionStrings["ConnData"].ConnectionString;
                SqlConnection con = new SqlConnection(constring);
                string q = "select * from registerdata where Username = @Username and Password = @Password ";
                SqlCommand cmd = new SqlCommand(q, con);
                cmd.Parameters.AddWithValue("@Username", this.textBox1.Text);
                cmd.Parameters.AddWithValue("@Password", this.textBox2.Text);
                con.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    if (dr.HasRows == true)
                    {
                        MessageBox.Show("Login Successfully Done");
                    }
                }
                if (dr.HasRows == false)
                { MessageBox.Show("Access Denied, password username mismatched"); }
            }
            else { MessageBox.Show("Enter username and password"); }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            RetrievePwdForm rf = new RetrievePwdForm();
            rf.Show();
            this.Hide();
        }
        
    }
}


7. Register Form

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.Sql;
using System.Data.SqlClient;
using System.Configuration;
using System.Net;
using System.Collections.Specialized;

namespace SchoolData
{
    public partial class SigninForm : Form
    {
        public SigninForm()
        {
            InitializeComponent();
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }
        //checking whether username already exist
        public void UserNameCheck()
        {
            string constring = ConfigurationManager.ConnectionStrings["ConnData"].ConnectionString;
            SqlConnection con = new SqlConnection(constring);
            SqlCommand cmd = new SqlCommand("Select * from registerdata where Username= @Username", con);
            cmd.Parameters.AddWithValue("@Username", this.textBox1.Text);
            con.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                if (dr.HasRows == true)
                {
                    MessageBox.Show("Username = "+ dr[1].ToString() +" Already exist");
                    break;
                }
            }
            //return textBox1.Text  ;
        }
        // clear all values in textbox
        public void clearall()
        {
            textBox1.Text = " ";
            textBox2.Text = " ";
            textBox3.Text = " ";
            textBox4.Text = " ";
            textBox5.Text = " ";

        }
        //checking whether mobile number already exist
        public void MobileNOCheck()
        {
            string constring = ConfigurationManager.ConnectionStrings["ConnData"].ConnectionString;
            SqlConnection con = new SqlConnection(constring);
            SqlCommand cmd = new SqlCommand("Select * from registerdata where MobileNO= @MobileNO", con);
            cmd.Parameters.AddWithValue("@MobileNO", this.textBox4.Text);
            con.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                if (dr.HasRows == true)
                {
                    MessageBox.Show("MobileNO = " + dr[4].ToString() + "is Already exist");
                    break;
                }
            }
            //return textBox4.Text;
        }
        public void datastore()
        {

            string Fname, Sname, Fullname, MobileNO, EmailID;
            Fname = textBox1.Text;
            Sname = textBox2.Text;
            Fullname = textBox3.Text;
            MobileNO = textBox4.Text;
            EmailID = textBox5.Text;
            try
            {
                string constring = ConfigurationManager.ConnectionStrings["ConnData"].ConnectionString;

                /* Declaring Connection Variable */
                SqlConnection con = new SqlConnection(constring);

                /* Checking Connection is Opend or not If its not open the Opens */
                if (con.State != ConnectionState.Open)
                    con.Open();

                /* Calling Stored Procedure as SqlCommand */
                SqlCommand cmd = new SqlCommand("regis", con);
                cmd.CommandType = CommandType.StoredProcedure;

                /* Passing Input Parameters with Command */
                cmd.Parameters.AddWithValue("@Username", Fname);
                cmd.Parameters["@Username"].Direction = ParameterDirection.Input;

                cmd.Parameters.AddWithValue("@Password", Sname);
                cmd.Parameters["@Password"].Direction = ParameterDirection.Input;

                cmd.Parameters.AddWithValue("@Fullname", Fullname);
                cmd.Parameters["@Fullname"].Direction = ParameterDirection.Input;

                cmd.Parameters.AddWithValue("@MobileNO", MobileNO);
                cmd.Parameters["@MobileNO"].Direction = ParameterDirection.Input;

                cmd.Parameters.AddWithValue("@EmailID", EmailID);
                cmd.Parameters["@EmailID"].Direction = ParameterDirection.Input;

                /* Executing Stored Procedure */
                cmd.ExecuteNonQuery();
                con.Close();
                MessageBox.Show("Data Inserted Succesfully");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
             
        }
        public void registerdata()
        {       datastore();
                this.Hide();
                LoginForm lf = new LoginForm();
                lf.Show();
            
        }
        private void button1_Click(object sender, EventArgs e)
        {
            registerdata();
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            //UserNameCheck();
            //clearall();
        }

        private void textBox4_KeyDown(object sender, KeyEventArgs e)
        {
            //MobileNOCheck();
            //clearall();
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            //UserNameCheck();
            //clearall();
        }

        private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
        {
            //MobileNOCheck();
            //clearall();
        }

        private void textBox4_KeyUp(object sender, KeyEventArgs e)
        {
            //MobileNOCheck();
            //clearall();
        }
        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            //UserNameCheck();
            //clearall();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            UserNameCheck();
           
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {
           // MobileNOCheck();
           
        }

        private void textBox4_TextChanged_1(object sender, EventArgs e)
        {
            MobileNOCheck();
        }
    }
}

8. Retrieve Password Form

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.Sql;
using System.Data.SqlClient;
using System.Configuration;
using System.Net;
using System.Collections.Specialized;

namespace SchoolData
{
    public partial class RetrievePwdForm : Form
    {
        public RetrievePwdForm()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.Hide();
            LoginForm lf = new LoginForm();
            lf.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string constring = ConfigurationManager.ConnectionStrings["ConnData"].ConnectionString;
            SqlConnection con = new SqlConnection(constring);
            SqlCommand cmd = new SqlCommand("Select * from registerdata where MobileNO= @MobileNO", con);
            cmd.Parameters.AddWithValue("@MobileNO", this.textBox1.Text);
            con.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                if (dr.HasRows == true)
                {
                    string str = "Your Password = ";
                    label2.Text=str +  dr[2].ToString();
                }
            }
        }
    }
}

Output


a) Register New User and store into database






b) Check Username already Exist

C) Check Mobile Number Already Exist


D) Forget Password



Finally Login Successfully done



















Sunday, 7 July 2013

Print and PrintPreview in C# Windows Application

In this article; we gonna see how to make print and preview options in C# windows application

1. Design View of the windows Form






















2. Code


3. Output 





Friday, 5 July 2013

Data Display Form Using DataGridview tool in C# Windows Application


1. Design Form 



Steps:

a. Choose Datagridview tool in tool kit
b. Choose new datasource from datagridview
c. After choosing particular database and data table the design of form will be look a like in this above acreenshot

2. Class view for data table 




























3. Code created automatically






4. Output data displayed


Thursday, 4 July 2013

C# Windows Application Data Form sample Module: Save and Retrieve Data using MS SQL database


Methods Used:

1. Created a database as NameDB and Database table as namedata as follows.

--table for saving retrieving datas

create table namedata
(
 ID int identity,
 Firstname nvarchar(500),
 Secondname nvarchar(500),
 Fullname nvarchar(500)
)


--get  query result
select * from namedata


2. Create a  stored procedure for saving data


-- creating stored procedure for saving data
create proc nameprocedure 
@Firstname as nvarchar(500),
@Secondname as nvarchar(500),
@Fullname as nvarchar(500)
as
begin 
 SET NOCOUNT ON;
Insert into  namedata(Firstname,Secondname,Fullname)
values(@Firstname,@Secondname,@Fullname)
end
-- execute strored procedure
exec nameprocedure

3. Windows Application design for save and Retrieve data form



set formborderstyle as none


























4. App.config settings to Connect database


<?xml version="1.0"?>
<configuration>
  <configSections>
  </configSections>
  <connectionStrings>
    <add name="Connectionstring1" connectionString="Data Source=localhost\sqlexpress; Initial Catalog=NameDB;Integrated Security=true"
      providerName="System.Data.SqlClient" />
    <add name="saveandretrieve.Properties.Settings.ConnectionString1"
      connectionString="Data Source=localhost\sqlexpress;Initial Catalog=NameDB;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>


5. Class Diagram analysis with SQL query and stored procedure




  
6. Form Designer Coding




6. Outputs

a) Storing Data from Windows Application to Sql DataBase.







b) Data stored in MS SQL Database





c) Retrieve Data(K Kumaran) from database to windows Application






















































After Clicking Retrieve Button







































Java Swing Full Screen window Application without Maximize, Minimize, Exit Buttons

Code For FullScreen Java Swing Desktop Application



Output Screen