Thursday, 17 October 2013

Register and login code c#

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.SqlClient;

namespace login_form
{
    public partial class Form1 : Form
    {
        public string connection = "Data Source=user-PC;Initial Catalog=db_test;Integrated Security=True";
        public Form1()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connection);
            con.Open();
            SqlCommand cmd = new SqlCommand("insert into userpassword values('"+txtuser.Text+"','"+txtpass.Text+"')",con);
            if (txtpass.Text == txtconformpass.Text)
            cmd.ExecuteNonQuery();
            MessageBox.Show("insert successfully");
            con.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connection);
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from userpassword where username='"+textBox1.Text+"' and password='"+textBox2.Text+"' ",con);
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                MessageBox.Show("registered user");
            }
            else
                MessageBox.Show("please register 1st");
        }
    }
}




1 comment: