Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
81 views

Form1: Using Using Using Using Using Using Using Using Namespace Public Partial Class Public

The document contains code for a Windows forms application with two forms - Form1 and Form2. Form1 handles user login validation against a database table. Form2 allows adding, updating, deleting and viewing records in another database table, with data binding to populate and update text boxes and a datagrid on the form. Navigation buttons are included to move between records in the bound dataset.

Uploaded by

Nand Ni
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views

Form1: Using Using Using Using Using Using Using Using Namespace Public Partial Class Public

The document contains code for a Windows forms application with two forms - Form1 and Form2. Form1 handles user login validation against a database table. Form2 allows adding, updating, deleting and viewing records in another database table, with data binding to populate and update text boxes and a datagrid on the form. Navigation buttons are included to move between records in the bound dataset.

Uploaded by

Nand Ni
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

Form1

using using using using using using using using

System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Text; System.Windows.Forms; System.Data.SqlClient;

namespace WindowsApplication4 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { this.WindowState = FormWindowState.Maximized; } private void button1_Click(object sender, EventArgs e) { int flag = 0; SqlConnection cs = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=E:\\1.ME-SWE\\SEM I\\Lab\\SE9217-

Case_Tools\\SAS\\WindowsApplication3\\WindowsApplication3\\Admin_Login.mdf;In tegrated Security=True;Connect Timeout=30;User Instance=True"); cs.Open(); //SqlDataAdapter da = new SqlDataAdapter(); SqlCommand myCommand = new SqlCommand("SELECT * FROM Admin_Login",cs); SqlDataReader myReader = myCommand.ExecuteReader(); while (myReader.Read()) { //if (string.Equals(textBox1.Text, myReader.GetString(0))) //{ //if(textBox2.Text == myReader["Password"].ToString()) if (textBox1.Text == myReader.GetString(0) && textBox2.Text == myReader.GetString(1)) //if(string.Compare(textBox1.Text,myReader["Login_Id"].ToStri ng())== 0) { MessageBox.Show("Login Successfull"); flag = 1; //Form1 f1 = new Form1(); //f1.Show(); this.Hide(); Form2 s = new Form2(); s.Show(); } } if (flag == 0) MessageBox.Show("Login Unsuccessfull"); //da.InsertCommand = new SqlCommand("INSERT INTO Admin_Login VALUES(@Login_Id,@Password)", cs); //da.InsertCommand.Parameters.Add("@Login_Id", SqlDbType.NVarChar).Value = textBox1.Text; //da.InsertCommand.Parameters.Add("@Password", SqlDbType.NVarChar).Value = textBox2.Text; //da.InsertCommand.ExecuteNonQuery(); //MessageBox.Show("Added"); // MessageBox.Show(cs.State.ToString()); cs.Close(); } } FORM2 }

using using using using using using using using

System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Text; System.Windows.Forms; System.Data.SqlClient;

namespace WindowsApplication4 { public partial class Form2 : Form { DataSet ds = new DataSet(); SqlConnection cs = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=E:\\1.ME-SWE\\SEM I\\Lab\\SE9217Case_Tools\\SAS\\WindowsApplication3\\WindowsApplication3\\Admin_Login.mdf;In tegrated Security=True;Connect Timeout=30;User Instance=True"); SqlDataAdapter da = new SqlDataAdapter(); BindingSource bs = new BindingSource(); public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { this.WindowState = FormWindowState.Maximized; }

private void button1_Click(object sender, EventArgs e) { if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "") { MessageBox.Show("Invalid Addition"); textBox1.Clear(); textBox2.Clear(); textBox3.Clear(); } else { da.InsertCommand = new SqlCommand("INSERT INTO Faculty_Details VALUES(@FACULTY_NAME,@DEPT,@FACULTY_CODE)", cs); da.InsertCommand.Parameters.Add("@FACULTY_NAME", SqlDbType.VarChar).Value = textBox1.Text; da.InsertCommand.Parameters.Add("@DEPT", SqlDbType.VarChar).Value = textBox2.Text; da.InsertCommand.Parameters.Add("@FACULTY_CODE", SqlDbType.VarChar).Value = textBox3.Text; cs.Open(); da.InsertCommand.ExecuteNonQuery(); MessageBox.Show("Record(s) has been Added"); textBox1.Clear(); textBox2.Clear(); textBox3.Clear(); cs.Close();

} }

private void button2_Click(object sender, EventArgs e) { da.SelectCommand = new SqlCommand("select * from Faculty_Details order by FACULTY_CODE asc", cs); ds.Clear(); da.Fill(ds); dataGridView1.DataSource = ds.Tables[0]; bs.DataSource = ds.Tables[0]; if (textBox1.Text == "" && textBox2.Text == "" && textBox3.Text == "") { textBox1.DataBindings.Add(new Binding("Text", bs, "FACULTY_NAME")); textBox2.DataBindings.Add(new Binding("Text", bs, "DEPT"));

textBox3.DataBindings.Add(new Binding("Text", bs, "FACULTY_CODE")); records(); } } private void button3_Click(object sender, EventArgs e) { if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "") { } else { MessageBox.Show("Invalid Updation");

da.UpdateCommand = new SqlCommand("UPDATE Faculty_Details SET FACULTY_NAME=@FACULTY_NAME,DEPT=@DEPT WHERE FACULTY_CODE=@FACULTY_CODE", cs); da.UpdateCommand.Parameters.Add("@FACULTY_NAME", SqlDbType.VarChar).Value = textBox1.Text; da.UpdateCommand.Parameters.Add("@DEPT", SqlDbType.VarChar).Value = textBox2.Text; da.UpdateCommand.Parameters.Add("@FACULTY_CODE", SqlDbType.VarChar).Value = textBox3.Text; cs.Open(); da.UpdateCommand.ExecuteNonQuery(); MessageBox.Show("Record(s) has been Updated"); cs.Close(); } }

private void button4_Click(object sender, EventArgs e) { bs.MoveFirst(); dataGridView1Update(); records(); } private void button5_Click(object sender, EventArgs e) { bs.MovePrevious(); dataGridView1Update(); records(); } private void button6_Click(object sender, EventArgs e) { bs.MoveNext(); dataGridView1Update(); records(); } private void button7_Click(object sender, EventArgs e)

bs.MoveLast(); dataGridView1Update(); records();

} private void dataGridView1Update() { dataGridView1.ClearSelection(); dataGridView1.Rows[bs.Position].Selected=true; records(); } private void records() { label4.Text = " Record " + bs.Position + " of " + (bs.Count - 1); } private void button8_Click(object sender, EventArgs e) { if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text { } else { MessageBox.Show("Invalid Deletion");

== "")

DialogResult dr;

dr = MessageBox.Show("Are you sure?\nThere is no undo once data is deleted", "Confirm Deletion", MessageBoxButtons.YesNo); if (dr == DialogResult.Yes) { da.DeleteCommand = new SqlCommand("Delete from Faculty_Details WHERE FACULTY_CODE=@FACULTY_CODE", cs); da.DeleteCommand.Parameters.Add("@FACULTY_CODE", SqlDbType.VarChar).Value = textBox3.Text; cs.Open(); da.DeleteCommand.ExecuteNonQuery(); MessageBox.Show("Record(s) has been Deleted"); cs.Close(); ds.Clear(); da.Fill(ds); } else { MessageBox.Show("Deletion Cancelled"); } } } }

You might also like