Вы находитесь на странице: 1из 7

Q. Write a GUI application to check whether a string is a valid password.

Password Rules are that


password must have at least 10 characters, password consist of only letters and digits & password
must contain atleast two digits.

Ans: using System;


using System.Collections.Generic;
using System.Collections;

public class Exercise11


{

public static readonly int PASSWORD_LENGTH = 8;

public static void main(string[] args)


{

Console.Write(
"1. A password must have at least eight characters.\n" +
"2. A password consists of only letters and digits.\n" +
"3. A password must contain at least two digits \n" +
"Input a password (You are agreeing to the above Terms and Conditions.): ");
string s = Console.ReadLine();

if (is_Valid_Password(s))
{
Console.WriteLine("Password is valid: " + s);
}
else
{
Console.WriteLine("Not a valid password: " + s);
}

public static bool is_Valid_Password(string password)


{

if (password.Length < PASSWORD_LENGTH) return false;

int charCount = 0;
int numCount = 0;
for (int i = 0; i < password.Length; i++)
{

char ch = password[i];

if (is_Numeric(ch)) numCount++;
else if (is_Letter(ch)) charCount++;
else return false;
}

return (charCount >= 2 && numCount >= 2);


}

public static bool is_Letter(char ch)


{
ch = Char.ToUpper(ch);
return (ch >= 'A' && ch <= 'Z');
}

public static bool is_Numeric(char ch)


{

return (ch >= '0' && ch <= '9');


}

Q2. There is a database in MS-Accsess named as edb. It contains a table employee. The table contain
attributes id, name, dateofjoin and designation (all are text type). Design an ASP Application to
insert, update, delete and display the record in the table.

Ans: using System;


using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace app
{
public partial class frmMain : Form
{
SqlConnection con= new SqlConnection("Data Source=.;Initial Catalog=edb;Integrated
Security=true;");
SqlCommand cmd;
SqlDataAdapter adapt;
//ID variable used in Updating and Deleting Record
int ID = 0;
public frmMain()
{
InitializeComponent();
DisplayData();
}
//Insert Data
private void btn_Insert_Click(object sender, EventArgs e)
{
if (txt_Name.Text != "" && txt_Dof.Text != "" && txt_Designation.Text != "")
{
cmd = new SqlCommand("insert into employee(Name,DOF,Designation)
values(@name,@dof,@desig)", con);
con.Open();
cmd.Parameters.AddWithValue("@name", txt_Name.Text);
cmd.Parameters.AddWithValue("@dof", txt_Dof.Text);
cmd.Parameters.AddWithValue("@desig", txt_Designation.Text);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Record Inserted Successfully");
DisplayData();
ClearData();
}
else
{
MessageBox.Show("Please Provide Details!");
}
}
//Display Data in DataGridView
private void DisplayData()
{
con.Open();
DataTable dt=new DataTable();
adapt=new SqlDataAdapter("select * from employee",con);
adapt.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();
}
//Clear Data
private void ClearData()
{
txt_Name.Text = "";
txt_Dof.Text = "";
txt_Designation.text="";
ID = 0;
}
//dataGridView1 RowHeaderMouseClick Event
private void dataGridView1_RowHeaderMouseClick(object sender,
DataGridViewCellMouseEventArgs e)
{
ID = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
txt_Name.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
txt_Dof.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
txt_Designation.Text =
dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
}
//Update Record
private void btn_Update_Click(object sender, EventArgs e)
{
if (txt_Name.Text != "" && txt_Dof.Text != "" && txt_Designation.Text != "" )
{
cmd = new SqlCommand("update employee set Name=@name,Dof=@dof,
Designation=@desig where ID=@id", con);
con.Open();
cmd.Parameters.AddWithValue("@id", ID);
cmd.Parameters.AddWithValue("@name", txt_Name.Text);
cmd.Parameters.AddWithValue("@dof", txt_Dof.Text);
cmd.Parameters.AddWithValue("@desig", txt_Desgination.Text);

cmd.ExecuteNonQuery();
MessageBox.Show("Record Updated Successfully");
con.Close();
DisplayData();
ClearData();
}
else
{
MessageBox.Show("Please Select Record to Update");
}
}
//Delete Record
private void btn_Delete_Click(object sender, EventArgs e)
{
if(ID!=0)
{
cmd = new SqlCommand("delete employee where ID=@id",con);
con.Open();
cmd.Parameters.AddWithValue("@id",ID);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Record Deleted Successfully!");
DisplayData();
ClearData();
}
else
{
MessageBox.Show("Please Select Record to Delete");
}
}
}
}

Вам также может понравиться