C# Windows Form Application - c# windows form application projects with source code



Login code

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

namespace WindowsForm
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        private void btn_login_submit_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-BM4R8M0;Initial Catalog=Jerry;Integrated Security=True");
            con.Open();
            SqlCommand cmd = new SqlCommand("sp_login", con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter param1 = new SqlParameter("@uname", SqlDbType.VarChar);
            cmd.Parameters.Add(param1).Value = txtbox_login_uname.Text;
            SqlParameter param2 = new SqlParameter("@pwd", SqlDbType.VarChar);
            cmd.Parameters.Add(param2).Value = txtbox_login_pwd.Text;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            int a = Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString());
            if (a > 0)
            {
                MessageBox.Show("Valid User");
                CRUD crud = new CRUD();
                crud.Show();
                this.Hide();
            }
            else
            {
                MessageBox.Show("InValid User");
            }
            con.Close();

        }

        private void lnk_lbl_login_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            NewUserPg obj1 = new NewUserPg();
            obj1.Show();
            this.Hide();
        }

New User Registration Page

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

namespace WindowsForm
{
    public partial class NewUserPg : Form
    {
        public NewUserPg()
        {
            InitializeComponent();
        }

       

        private void btn_NewUser_submit_Click(object sender, EventArgs e)
        {
            try
            {

                SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-BM4R8M0;Initial Catalog=Jerry;Integrated Security=True");
                con.Open();
                SqlCommand cmd = new SqlCommand("sp_newuser", con);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlParameter param1 = new SqlParameter("@user_id", SqlDbType.VarChar);
                cmd.Parameters.Add(param1).Value = txtbox_newusrpg_userid.Text;
                SqlParameter param2 = new SqlParameter("@name", SqlDbType.VarChar);
                cmd.Parameters.Add(param2).Value = txtbox_newusrpg_name.Text;
                SqlParameter param3 = new SqlParameter("@uname", SqlDbType.VarChar);
                cmd.Parameters.Add(param3).Value = txtbox_newusrpg_uname.Text;
                SqlParameter param4 = new SqlParameter("@pwd", SqlDbType.VarChar);
                cmd.Parameters.Add(param4).Value = txtbox_newusrpg_pwd.Text;
                int i = cmd.ExecuteNonQuery();
                if (i > 0)
                    MessageBox.Show("Registered successfully");
                else
                    MessageBox.Show("Registration Failed");
                con.Close();
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void lnk_lbl_login_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Login obj = new Login();    
            obj.Show();
            this.Hide();
        }
    }
}

CRUD Page

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

namespace WindowsForm
{
    public partial class CRUD : Form
    {
        public CRUD()
        {
            InitializeComponent();
        }

        private void btn_CRUD_Insert_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-BM4R8M0;Initial Catalog=Jerry;Integrated Security=True");
            con.Open();
            SqlCommand cmd = new SqlCommand("sp_insert", con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter param1 = new SqlParameter("@crud_id", SqlDbType.VarChar);
            cmd.Parameters.Add(param1).Value = txtbox_crud_userid.Text;
            SqlParameter param2 = new SqlParameter("@name", SqlDbType.VarChar);
            cmd.Parameters.Add(param2).Value = txtbox_crud_name.Text;
            SqlParameter param3 = new SqlParameter("@degree", SqlDbType.VarChar);
            cmd.Parameters.Add(param3).Value =txtbox_crud_degree.Text;
            SqlParameter param4 = new SqlParameter("@place", SqlDbType.VarChar);
            cmd.Parameters.Add(param4).Value = txtbox_crud_place.Text;
            int i = cmd.ExecuteNonQuery();
            if (i > 0)
                MessageBox.Show("Data inserted successfully");
            else
                MessageBox.Show("Data cannot be inserted");
            con.Close();
        }

        private void btn_CRUD_Update_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-BM4R8M0;Initial Catalog=Jerry;Integrated Security=True");
            con.Open();
            SqlCommand cmd = new SqlCommand("sp_update", con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter param1 = new SqlParameter("@crud_id", SqlDbType.VarChar);
            cmd.Parameters.Add(param1).Value = txtbox_crud_userid.Text;
            SqlParameter param2 = new SqlParameter("@name", SqlDbType.VarChar);
            cmd.Parameters.Add(param2).Value = txtbox_crud_name.Text;
            SqlParameter param3 = new SqlParameter("@degree", SqlDbType.VarChar);
            cmd.Parameters.Add(param3).Value = txtbox_crud_degree.Text;
            SqlParameter param4 = new SqlParameter("@place", SqlDbType.VarChar);
            cmd.Parameters.Add(param4).Value = txtbox_crud_place.Text;
            int i = cmd.ExecuteNonQuery();
            if (i > 0)
                MessageBox.Show("Data updated successfully");
            else
                MessageBox.Show("Data cannot be updated");
            con.Close();
        }

        private void btn_CRUD_delete_Click(object sender, EventArgs e)
        {

            SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-BM4R8M0;Initial Catalog=Jerry;Integrated Security=True");
            con.Open();
            SqlCommand cmd = new SqlCommand("sp_delete", con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter param1 = new SqlParameter("@crud_id", SqlDbType.VarChar);
            cmd.Parameters.Add(param1).Value = txtbox_crud_userid.Text;
           
            int i = cmd.ExecuteNonQuery();
            if (i > 0)
                MessageBox.Show("Data deleted successfully");
            else
                MessageBox.Show("Data cannot be deleted");
            con.Close();

        }

        private void btn_CRUD_FETCH_Click(object sender, EventArgs e)
        {

            SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-BM4R8M0;Initial Catalog=Jerry;Integrated Security=True");
            con.Open();
            SqlCommand cmd = new SqlCommand("sp_fetch", con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            dataGridView.DataSource = ds.Tables[0];
            con.Close();
        }

        private void btn_CRUD_SEARCH_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-BM4R8M0;Initial Catalog=Jerry;Integrated Security=True");
            con.Open();
            SqlCommand cmd = new SqlCommand("sp_search", con);
            cmd.CommandType = CommandType.StoredProcedure;

            SqlParameter param1 = new SqlParameter("@searchdata", SqlDbType.VarChar);
            cmd.Parameters.Add(param1).Value = txtbox_crud_search.Text;

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            dataGridView.DataSource = ds.Tables[0];
            con.Close();
        }
    }
}

Database Code for Windows Form

TABLE 1 tbl_user

create table tbl_user
(
Id int identity(1,1),
User_Id varchar(50),
Name varchar(50),
Username varchar(50),
Password varchar(50)
)

LOGIN

create procedure sp_login
(
		@uname varchar(50),
		@pwd varchar(50)
)
as begin
   select count(1) from tbl_user
    where Username=@uname and Password=@pwd
end


exec sp_login 'venkat','venkat'

New Registration - INSERT

create procedure sp_newuser
( 
        @user_id varchar(50),
        @name varchar(50),
		@uname varchar(50),
		@pwd varchar(50)
)
as begin
insert into tbl_user
   (User_Id ,Name,Username,Password)
   values ( @user_id,@name,@uname,@pwd)
end

exec sp_insert 'U1001','Venkat','venkat','venkat'
exec sp_newuser 'U1002','Praveen','praveen','praveen'

TABLE 2 tbl_crud

create table tbl_crud
(
Id int identity(1,1),
Crud_Id varchar(50),
Name varchar(50),
Degree varchar(50),
Place varchar(50)
)

INSERT

create procedure sp_insert
( 
        @crud_id varchar(50),
        @name varchar(50),
		@degree varchar(50),
		@place varchar(50)
)
as begin
insert into tbl_crud
   (Crud_Id ,Name,Degree,Place)
   values ( @crud_id ,@name,@degree,@place)
end


exec sp_insert 'C1001','Asha','MCA','Chennai'

UPDATE

create procedure sp_update
(
        @crud_id varchar(100),
        @name varchar(100),
		@degree varchar(100),
		@place varchar(100)
)
as begin
update tbl_crud set Crud_Id=@crud_id,
	Name=  @name,
	Degree= @degree ,
	Place = @place where Crud_Id =@crud_id
end

exec sp_update 'C1001','Asha','MCA','Thiruvalluvar'

DELETE

create procedure sp_delete
(
        @crud_id varchar(100)
)
as begin
delete from tbl_crud where Crud_Id =@crud_id
end

exec sp_delete 'C1001'

SELECT

create procedure sp_fetch
as begin
select * from tbl_crud
end

exec sp_fetch
 

SEARCH

create procedure sp_search
(
        @searchdata varchar(100)
)
as begin
select * from tbl_crud where Name like'%'+ @searchdata+'%'
end

exec  sp_search 'sh'
 

Related Searches to C# Windows Form Application - c# windows form application projects with source code