Test Code

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Test code

database Connection string


SqlConnection con = new SqlConnection(" Data Source=AHMAD\\SQLEXPRESS;Initial
Catalog=hms;Integrated Security=True");
con.Open();

___________________________________________________________________________

Log in:
SqlConnection con = new SqlConnection(" Data Source=AHMAD\\SQLEXPRESS;Initial
Catalog=hms;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand(" Select * From login where user_name=
'"+main_log.Text+"' AND Password= '"+main_pass.Text+"' ",con);
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{

mainform add = new mainform();


add.Show();
this.Hide();

}
else
{
MessageBox.Show("Invalid UserName Or Pqssword");
} ("Invalid UserName Or Password");
}

___________________________________________________________________________

Add:
private void button1_Click(object sender, EventArgs e)
{
textBox3.Text = (Convert.ToInt32(textBox1.Text) +
Convert.ToInt32(textBox2.Text)).ToString();
}

_____________________________________________________________________________________

Date time picker


private void button2_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(" Data
Source=AHMAD\\SQLEXPRESS;Initial Catalog=test;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("Insert into Dob(e_name,DOB)Values('" +
e_name.Text + "','" + dateTimePicker1.Text + "') ", con);
cmd.ExecuteNonQuery();
MessageBox.Show("Successfully Added");

Clear function
Action<Control.ControlCollection > func = null;
func = (controls) =>
{
foreach(Control control in controls)
{
if (control is TextBox)
{
(control as TextBox).Clear();
}
else
{
func(control.Controls);
}
}
};
func(Controls);

Search by Name
public void loaddata()
{
con.Open();
SqlDataAdapter sd = new SqlDataAdapter("select * from add_bloodp where
name='"+search.Text+"'", con);
DataTable dt = new DataTable();
sd.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();

Button
Loaddata();

Save data/Insert data in table


con.Open();
if (name.Text == "" || f_name.Text == "" || cnic.Text == "" ||
cell_nbr.Text == "")
{
MessageBox.Show("Some Entries Are Missing Please Enter Proper
Values");
}
else {
SqlCommand cmd = new SqlCommand("Insert into
add_bloodp(name,f_name,age,dob,cell_nbr,cnic,city,address,height,eye_color,disease,blo
od_group,dod)Values('" + name.Text + "','" + f_name.Text + "','" + age.Text + "','" +
dateTimePicker1.Text + "','" + cell_nbr.Text + "','" + cnic.Text + "','" + city.Text +
"','" + address.Text + "','" + height.Text + "','" + eye_color.Text + "','" +
disease.Text + "','" + blood_group.Text + "','" + dod.Text + "')", con);
cmd.ExecuteNonQuery();
MessageBox.Show("Person Added");
}

con.Close();

Auto Increment id
public void incrementid()
{

con.Open();
SqlCommand cmd = new SqlCommand("Select Top (1)id from dept order by id
DESC",con);
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
int i = Convert.ToInt32(sdr["id"].ToString());
txtdept_id.Text = Convert.ToString(i + 1);
}
con.Close();
}

Valid Data in email

public static bool emailIsValid(string email)


{
string expresion;
expresion = "\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
if (Regex.IsMatch(email, expresion))
{
if (Regex.Replace(email, expresion, string.Empty).Length == 0)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}

Only Char or digit


if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
{
e.Handled = true;
}

if (!char.IsControl(e.KeyChar) && !char.IsLetter(e.KeyChar) &&


!char.IsWhiteSpace(e.KeyChar))
{
e.Handled = true;
}

Valid Data Type


private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.Handled = (char.IsDigit(e.KeyChar)) || e.KeyChar == (char)Keys.Back
|| e.KeyChar == (char)Keys.Space)
{
e.Handled = false;
}
else
{
e.Handled = true;
;
textBox4.BackColor = Color.Blue;
MessageBox.Show("Please Enter the valid values", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
textBox4.Focus();

}
}
Enter The Value
private void button5_Click_1(object sender, EventArgs e)
{

if(textBox1.Text == "")
{
textBox1.BackColor = Color.LightBlue;
MessageBox.Show("Plz Enter the Medicine ID", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
textBox1.Focus();
return;

For the white textbox

private void textBox1_TextChanged(object sender, EventArgs e)


{
Control cntrl = (Control)sender;
cntrl.BackColor = Color.White;
}

From Gridview to textbox


if (e.RowIndex >= 0)
{
DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
invoiceno.Text = row.Cells["Invoice"].Value.ToString();
patientid.Text = row.Cells["Patient_ID"].Value.ToString();
patientname.Text = row.Cells["Patient_Name"].Value.ToString();
medid.Text = row.Cells["Med_ID"].Value.ToString();
medname.Text = row.Cells["Med_Name"].Value.ToString();
medtype.Text = row.Cells["Med_Type"].Value.ToString();
dateTimePicker1.Text = row.Cells["Date"].Value.ToString();
qty.Text = row.Cells["Quantity"].Value.ToString();
unit.Text = row.Cells["Med_Unit"].Value.ToString();
unitprice.Text = row.Cells["Unit_Price"].Value.ToString();
dateTimePicker3.Text = row.Cells["Mfg_Date"].Value.ToString();
dateTimePicker2.Text = row.Cells["Exp_Date"].Value.ToString();
amoun.Text = row.Cells["Amount"].Value.ToString();
tamount.Text = row.Cells["Total_Amount"].Value.ToString();
}

You might also like