using System; using System.Windows.Forms; using System.Drawing; using System.Data.SqlClient; class ex : Form { Label lb1 = new Label(); Label lb2 = new Label(); Label lb3 = new Label(); Label lb4 = new Label(); Label lb5 = new Label(); Label lb6 = new Label(); TextBox tb1 = new TextBox(); TextBox tb2 = new TextBox(); TextBox tb3 = new TextBox(); TextBox tb4 = new TextBox(); TextBox tb5 = new TextBox(); TextBox tb6 = new TextBox(); Button b1 = new Button(); DataGridView dgv = new DataGridView(); int rw = 0; ex() { Width = 620; Height = 400; Text = "Customer Orders"; lb1.Text = "CustomerID:"; lb2.Text = "First Name:"; lb3.Text = "Last Name:"; lb4.Text = "Phone Number:"; lb5.Text = "Address:"; lb6.Text = "Notes:"; lb1.Width = 85; lb2.Width = 85; lb3.Width = 85; lb4.Width = 85; lb5.Width = 85; lb6.Width = 40; lb2.Location = new Point(0, 25); lb3.Location = new Point(0, 50); lb4.Location = new Point(195, 25); lb5.Location = new Point(195, 50); lb6.Location = new Point(385, 25); Controls.Add(lb1); Controls.Add(lb2); Controls.Add(lb3); Controls.Add(lb4); Controls.Add(lb5); Controls.Add(lb6); tb6.Width = 175; tb6.Multiline = true; tb6.Height = 45; tb1.Location = new Point(90, 0); tb2.Location = new Point(90, 25); tb3.Location = new Point(90, 50); tb4.Location = new Point(280, 25); tb5.Location = new Point(280, 50); tb6.Location = new Point(425, 25); Controls.Add(tb1); Controls.Add(tb2); Controls.Add(tb3); Controls.Add(tb4); Controls.Add(tb5); Controls.Add(tb6); b1.Text = "Return"; b1.Click += new EventHandler(button_b1_click); b1.Location = new Point(200, 0); dgv.Location = new Point(0, 75); dgv.Height = 400; dgv.Width = 600; dgv.ReadOnly = true; dgv.AllowUserToAddRows = false; dgv.BackgroundColor = Color.White; dgv.GridColor = Color.FromArgb(230, 230, 230); dgv.ColumnCount = 4; dgv.RowHeadersDefaultCellStyle.BackColor = Color.FromArgb(251, 251, 251); dgv.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single; dgv.RowTemplate.Height = 18; dgv.Columns[0].Name = "OrderID"; dgv.Columns[1].Name = "OrderDate"; dgv.Columns[2].Name = "DeliveryMethod"; dgv.Columns[3].Name = "DeliveryDate"; Controls.Add(dgv); Controls.Add(b1); } void button_b1_click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("Data Source = (localdb)\\sql;" + "User id= sample_user;" + "Password = 'abc'; " + "Database = ex;"); conn.Open(); SqlCommand command = new SqlCommand( "select FirstName, LastName, " + "PhoneNumber, Address, Notes " + "from Customers " + "where CustomerID = @0", conn); command.Parameters.Add(new SqlParameter("0", tb1.Text)); SqlDataReader reader = command.ExecuteReader(); if (reader.Read()) { tb2.Text = reader[0].ToString(); tb3.Text = reader[1].ToString(); tb4.Text = reader[2].ToString(); tb5.Text = reader[3].ToString(); tb6.Text = reader[4].ToString(); } else { tb2.Text = ""; tb3.Text = ""; } reader.Close(); dgv.Rows.Clear(); rw = 0; command = new SqlCommand( "select o.OrderID, OrderDate, DeliveryMethod, DeliveryDate " + "from Orders o " + "join CustomerOrder co on o.OrderID = co.OrderID " + "join Customers c on co.CustomerID = c.CustomerID " + "where c.CustomerID = @0", conn); command.Parameters.Add(new SqlParameter("0", tb1.Text)); reader = command.ExecuteReader(); while (reader.Read()) { dgv.Rows.Add(); dgv.Rows[rw].HeaderCell.Value = (rw + 1).ToString(); dgv.Rows[rw].Cells[0].Value = reader[0]; dgv.Rows[rw].Cells[1].Value = reader[1]; dgv.Rows[rw].Cells[2].Value = reader[2]; dgv.Rows[rw].Cells[3].Value = reader[3]; rw = rw + 1; } conn.Close(); } static public void Main() { Application.Run(new ex()); } }