.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Visual Studio >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Login to database using Enter button on keyboard

Posted By:satyapriyanayak       Posted Date: September 30, 2013    Points: 40    Category: Visual Studio    URL: http://www.dotnetspark.com  

We will know how to login to database using Enter button on keyboard.
 

Method-1

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;
namespace Login_page_Msaccess
{
    public partial class Form1 : Form
    {
        string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
        OleDbCommand com;
        string str;
        
        object obj = null;
        public Form1()
        {
            InitializeComponent();
        }

        private void btn_login_Click(object sender, EventArgs e)
        {

            OleDbConnection con = new OleDbConnection(ConnectionString);
            con.Open();
            str = "select count(*) from login where UserName=@UserName and Password =@Password";
            com = new OleDbCommand(str, con);
            com.CommandType = CommandType.Text;
            com.Parameters.AddWithValue("@UserName", TextBox_user_name.Text);
            com.Parameters.AddWithValue("@Password", TextBox_password.Text);
            obj = com.ExecuteScalar();
            if ((int)(obj) != 0)
            {
                lb1.Text = "WELLCOME :: " + TextBox_user_name.Text;
            }
            else
            {
                lb1.Text = "invalid user name and password";
            }
            con.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.AcceptButton = btn_login;
        
        }

       

        
    }
}


Method-2

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;
namespace Login_page_Msaccess
{
    public partial class Form1 : Form
    {
        string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
        OleDbCommand com;
        string str;
        
        object obj = null;
        public Form1()
        {
            InitializeComponent();
        }

        private void btn_login_Click(object sender, EventArgs e)
        {

            OleDbConnection con = new OleDbConnection(ConnectionString);
            con.Open();
            str = "select count(*) from login where UserName=@UserName and Password =@Password";
            com = new OleDbCommand(str, con);
            com.CommandType = CommandType.Text;
            com.Parameters.AddWithValue("@UserName", TextBox_user_name.Text);
            com.Parameters.AddWithValue("@Password", TextBox_password.Text);
            obj = com.ExecuteScalar();
            if ((int)(obj) != 0)
            {
                lb1.Text = "WELLCOME :: " + TextBox_user_name.Text;
            }
            else
            {
                lb1.Text = "invalid user name and password";
            }
            con.Close();
        }

        

        private void TextBox_password_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar.Equals(Convert.ToChar(13)))
            {
                btn_login_Click(sender, e);
            }
        }

        
    }
}



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend