Subham Dasgupta

Subham Dasgupta

  • NA
  • 1
  • 1.1k

login form

Jun 28 2015 3:06 AM
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Class_Project
{
public partial class Form1 : Form
{
public string id, pwd;
SqlConnection cn = new SqlConnection();
public void con()
{
cn.ConnectionString = "Data Source=SUBHAM-PC;Initial Catalog=hm;User id=sa;Password=subham";
}
public Form1()
{
InitializeComponent();
}
private void cmdLogin_Click(object sender, EventArgs e)
{
id = textBox1.Text;
pwd = textBox2.Text;
if (id != "")
{
con();
cn.Open();
try
{
string qry = "select Username,Password from login where Username = @uid and Password = @pass";
SqlCommand cmd = new SqlCommand(qry, cn);
cmd.Parameters.Add(new SqlParameter("@uid", id));
cmd.Parameters.Add(new SqlParameter("@pass", pwd));
SqlDataReader rdr = cmd.ExecuteReader();
if (rdr.Read() == true)
{
this.Hide();
form2 f2 = new form2();
f2.Show();
}
else
{
MessageBox.Show("Invalid login.Try Again", "login", MessageBoxButtons.OK, MessageBoxIcon.Warning);
textBox1.Text = "";
textBox2.Text = "";
textBox1.Focus();
}
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message, "System", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message, "System", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
cn.Close();
}
}
else
{
MessageBox.Show("Input Id and Password", "Login", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void cmdCancel_Click(object sender, EventArgs e)
{
Close();
}
}
}
the coding is not working.... 

Answers (2)