Monday, 9 October 2017

Create Login Window in C# Using SQL Server (Project Accounting Part:1) i...

               

Aslam-u-Alikuum Beautiful People


Acountting Software Part1

Log In Page

SQL 

Create database acoountingsoftware
 

CREATE TABLE [dbo].[Login](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Username] [nvarchar](50) NULL,
[Password] [nvarchar](50) NULL,
 CONSTRAINT [PK_Login] PRIMARY KEY CLUSTERED 
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

Visual C# Code

Return Class
 class returnclass
    {
        
  public string scalarReturn(string q)
        {
            string s ;
            SqlConnection conn = new SqlConnection(@"Data Source=DESKTOP-DODHH2V\SAAD;Initial Catalog=acoountingsoftware;Integrated Security=True"); //connection obj
            conn.Open(); //opening connection
            try
            {
                SqlCommand cmd = new SqlCommand(q, conn); //querry execution

              s  = cmd.ExecuteScalar().ToString();
            
            }
            catch (Exception)
            {

                s = " ";
            }
            conn.Close();

            return s;

        }
    }


On Button Click Event:

 private void button1_Click(object sender, EventArgs e)

        {

            string user = textBox1.Text;

            string password = textBox2.Text;

            string userdb, passworddb;



            returnclass rc = new returnclass();

            userdb = rc.scalarReturn("select Username from Login where Username='" + user + "'");





            if (userdb.Equals("0"))

            {

                MessageBox.Show("Invalid user name!");

            }

            else

            {

                passworddb = rc.scalarReturn("select Password from Login where Password='" + password + "'");



                if (passworddb.Equals(password))

                {

                   

                    Form2 f = new Form2();

                    f.Show();



                }

                else

                {

                    MessageBox.Show("Invalid Password!");

                }





            }



        }


1 comment:

  1. sir mene same code kiya hai but jab mai last par username or password enter kar raha tu invalid aa raha please tell me

    ReplyDelete