Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with C# code (window Form) Instructions: Given Code: *****Form1.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Linq;

Need help with C# code (window Form)

Instructions:

image text in transcribedimage text in transcribed

Given Code:

*****Form1.cs

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;

namespace Prog210_1stDBAccess_WinForm { public partial class Form1 : Form { public Form1() { InitializeComponent(); }

private void Form1_Load(object sender, EventArgs e) { // prepare and format the listView listView1.BackColor = System.Drawing.Color.LightBlue; // just playing with the listview props and appearance listView1.ForeColor = System.Drawing.Color.Blue; listView1.BorderStyle = BorderStyle.Fixed3D; listView1.View = View.Details; // just like file explorer, there are several view options listView1.Columns.Add("CompanyName", 200, HorizontalAlignment.Left); // these numbers are pixels, not characters listView1.Columns.Add("ContactName", 150, HorizontalAlignment.Left); listView1.Columns.Add("City", 100, HorizontalAlignment.Left);

listView1.GridLines = true;

// our 3 ADO objects for reading data from Prog123: SqlCommand SqlDataReader SqlConnection SqlCommand selectCommand = new SqlCommand(); //sql command object

//// (the @ tells C# to accept funny characters, like the \ ) const string connectString = @"Server=localhost; Database=northwind; Integrated Security=True";

SqlConnection connection = new SqlConnection(connectString); // using the constructor that accpets the connection string, one of several choices selectCommand = new SqlCommand("SELECT c.CompanyName, c.ContactName, c.City FROM Customers AS c INNER JOIN Orders AS o ON c.CustomerID = o.CustomerID WHERE o.ShipCountry = '' ORDER BY OrderDate ASC;", connection); // using the constructor that accpets the SQL command string, and the connection // one of several choices. Those are just properties that can be set after you // create the object

//open the database connection selectCommand.Connection.Open();

// define a variable of type Reader. as that is what this sql command will return // from the ExecuteReader method call SqlDataReader sqlReader = selectCommand.ExecuteReader();

// note our command specified 2 columns // SqlCommand("Select CompanyName, Phone from Shippers" // we will access them using the objects indexer [0] company name [1] Phone

while (sqlReader.Read()) // reader object returns a false when there are no more rows { listView1.Items.Add(sqlReader[0].ToString()); listView1.Items[listView1.Items.Count - 1].SubItems.Add(sqlReader[1].ToString()); listView1.Items[listView1.Items.Count - 1].SubItems.Add(sqlReader[2].ToString()); }

//while (sqlReader.Read()) // or can use the column names directly //{ // listView1.Items.Add(sqlReader["CompanyName"].ToString()); // listView1.Items[listView1.Items.Count - 1].SubItems.Add(sqlReader["Phone"].ToString()); //}

sqlReader.Close(); selectCommand.Connection.Close(); } } }

*******Form1.Designer.cs

namespace Prog210_1stDBAccess_WinForm { partial class Form1 { ///

/// Required designer variable. /// private System.ComponentModel.IContainer components = null;

///

/// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); }

#region Windows Form Designer generated code

///

/// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.listView1 = new System.Windows.Forms.ListView(); this.button1 = new System.Windows.Forms.Button(); this.label1 = new System.Windows.Forms.Label(); this.textBox1 = new System.Windows.Forms.TextBox(); this.SuspendLayout(); // // listView1 // this.listView1.HideSelection = false; this.listView1.Location = new System.Drawing.Point(113, 100); this.listView1.Name = "listView1"; this.listView1.Size = new System.Drawing.Size(549, 307); this.listView1.TabIndex = 0; this.listView1.UseCompatibleStateImageBehavior = false; // // button1 // this.button1.Location = new System.Drawing.Point(538, 41); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 1; this.button1.Text = "Get Data"; this.button1.UseVisualStyleBackColor = true; // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(180, 43); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(80, 13); this.label1.TabIndex = 2; this.label1.Text = "Enter a Country"; // // textBox1 // this.textBox1.Location = new System.Drawing.Point(183, 59); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(182, 20); this.textBox1.TabIndex = 3; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(724, 476); this.Controls.Add(this.textBox1); this.Controls.Add(this.label1); this.Controls.Add(this.button1); this.Controls.Add(this.listView1); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); this.PerformLayout();

}

#endregion

private System.Windows.Forms.ListView listView1; private System.Windows.Forms.Button button1; private System.Windows.Forms.Label label1; private System.Windows.Forms.TextBox textBox1; } }

**********Program.cs

using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms;

namespace Prog210_1stDBAccess_WinForm { static class Program { ///

/// The main entry point for the application. /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }

Have a form with a ListView object that displays the results of a query. The form should have a text box where the user can enter a country name A button, when they click the button, the button event method should do a SQL query and display the results in the ListView. When the program starts, the form does not have to display any data. You can put your SQL code right in with the form code, you do not have to put it in a separate class, but you should! The data to display is from the Customers table in the Northwind database, you should display the CompanyName, ContactName, and City for all customers in the country that was entered in the textbox. Here is my program after I start: Form1 X Enter a Country Get Data ID PPX Here I have entered a first country and clicked the button: Form1 Enter a Country Gemany Get Data Company Name Alfreds Futterkiste Blauer See Delikatessen Drachenblut Delikatessen Frankenversand Koniglich Essen Lehmanns Marktstand Morgenstem Gesundkost Ottilies Kaseladen QUICK-Stop Toms Spezialitten Die Wandemde Kuh Contact Name Maria Anders Hanna Moos Sven Ottlieb Peter Franken Philip Cramer Renate Messner Alexander Feuer Henriette Pfalzheim Horst Kloss Karin Josephs Rita Mller City Berlin Mannheim Aachen Munchen Brandenburg Frankfurt aM Leipzig Koln Cunewalde Mnster Stuttgart Have a form with a ListView object that displays the results of a query. The form should have a text box where the user can enter a country name A button, when they click the button, the button event method should do a SQL query and display the results in the ListView. When the program starts, the form does not have to display any data. You can put your SQL code right in with the form code, you do not have to put it in a separate class, but you should! The data to display is from the Customers table in the Northwind database, you should display the CompanyName, ContactName, and City for all customers in the country that was entered in the textbox. Here is my program after I start: Form1 X Enter a Country Get Data ID PPX Here I have entered a first country and clicked the button: Form1 Enter a Country Gemany Get Data Company Name Alfreds Futterkiste Blauer See Delikatessen Drachenblut Delikatessen Frankenversand Koniglich Essen Lehmanns Marktstand Morgenstem Gesundkost Ottilies Kaseladen QUICK-Stop Toms Spezialitten Die Wandemde Kuh Contact Name Maria Anders Hanna Moos Sven Ottlieb Peter Franken Philip Cramer Renate Messner Alexander Feuer Henriette Pfalzheim Horst Kloss Karin Josephs Rita Mller City Berlin Mannheim Aachen Munchen Brandenburg Frankfurt aM Leipzig Koln Cunewalde Mnster Stuttgart

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Samsung Galaxy S23 Ultra Comprehensive User Manual

Authors: Leo Scott

1st Edition

B0BVPBJK5Q, 979-8377286455

More Books

Students also viewed these Databases questions

Question

(e) What is the resolution? Pg45

Answered: 1 week ago

Question

4. Describe the factors that influence self-disclosure

Answered: 1 week ago

Question

1. Explain key aspects of interpersonal relationships

Answered: 1 week ago