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.Windows.Forms;

namespace CarsRUS
{
    public partial class Cars : Form
    {
        private SqlConnection conn;
        private SqlDataAdapter daAddresses;
        private DataSet dsAddresses;
        private DataGrid dgAddresses;
        private const string tableName = "CarRUS";
        public class Customer
        {
            public Customer(string CustomerID, string Forename, string Surname, string Address, string PostCode)
            {
                this.customerID = CustomerID;
                this.forename = Forename;
                this.surname = Surname;
                this.address = Address;
                this.postcode = PostCode;
            }
            public string customerID;
            public string forename;
            public string surname;
            public string address;
            public string postcode;
        }
        
        //button for new customer
        private void button1_Click(object sender, EventArgs e)
        {
            Customer c = new Customer(textBox1.Text, textBox2.Text, textBox5.Text, textBox6.Text, textBox9.Text);
            customerQueue.Enqueue(c);
            fillListBox(customerQueue);
            textBox1.Text = "";//text box 1 will show the value that the user types in the customer id text box
            textBox2.Text = "";//text box 2 will show the value that the user types in the forename text box
            textBox5.Text = "";//text box 5 will show the value that the user types in the surname text box
            textBox6.Text = "";//text box 6 will show the value that the user types in the address text box
            textBox9.Text = "";//text box 9 will show the value that the user types in the postcode text box
            textBox10.Text = "";//text box 10 will show the value that the user types in the telno text box
        }

        private void fillListBox(Queue<Customer> customers)//fill the listbox of the customer details
        {
            IEnumerator<Customer> myEnumerator = customerQueue.GetEnumerator();
            listBox1.Items.Add("----Start of Customer Booking----");
            //display the message above to show the start of the queue
            while (myEnumerator.MoveNext())
            {
                listBox1.Items.Add(myEnumerator.Current.customerID + " " + myEnumerator.Current.forename + " " + myEnumerator.Current.surname + " " + myEnumerator.Current.address + " " + myEnumerator.Current.postcode);
            }//list the details of the customer by customer id and then forename followed by the surname and then the address and then the postcode
        }

        private void Cars_Load(object sender, EventArgs e)
        {
        }
        //Button to quit program
        private void button3_Click(object sender, EventArgs e)
            {
                this.Close();
            }
        Queue<Customer> customerQueue = new Queue<Customer>();
        public Cars()
        {
            InitializeComponent();
            // fill dataset
            Initdata();
                        // set up datagrid
            dgAddresses = new DataGrid();
            dgAddresses.Location = new Point(5, 5);
            dgAddresses.Size = new Size(500, 500);
            dgAddresses.DataSource = null;
            dgAddresses.DataMember = null;
            // create update button
            Button btnUpdate = new Button();
            btnUpdate.Text = "Update";
            btnUpdate.Location = new Point(600, 150);
            btnUpdate.Click += new EventHandler(btnUpdateClicked);
            // make sure controls appear on form
            Controls.AddRange(new Control[] { dgAddresses, btnUpdate });
        }
        // set up ADO.NET objects
        public void Initdata()
        {
           try
            {
             conn = new SqlConnection(
"Data Source=.\\SQLEXPRESS;AttachDbFilename=G:\\CarRUS.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
           }
            catch (System.Exception ex)
            {
                MessageBox.Show("Exception" + ex.ToString());
            } 
            // 1. instantiate a new DataSet
            dsAddresses = new DataSet();
            // 2. init SqlDataAdapter with select command and connection
            daAddresses = new SqlDataAdapter("select CustomerID, Forename, Surname, Address, PostCod, TelNo from CarRUS", conn);
            // 3. fill in insert, update, and delete commands
            SqlCommandBuilder cmdBldr = new SqlCommandBuilder(daAddresses);
            // 4. fill the dataset
            //daAddresses.Fill(dsAddresses, tableName);
        }
        // Update button was clicked
        public void btnUpdateClicked(object sender, EventArgs e)
        {
            // write changes back to DataBase
            daAddresses.Update(dsAddresses, tableName);
        }
        private void button2_Click(object sender, EventArgs e)
        {
        }

        private void GridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}//end

Edited 7 Years Ago by Nick Evan: n/a

First, change this post to NOT be a snippet because that is not what a SNIPPET is for!

Where it asks for "What are you posting?", just use the default: Forum Thread

Edited 7 Years Ago by DdoubleD: n/a

First, change this post to NOT be a snippet because that is not what a SNIPPET is for!

Where it asks for "What are you posting?", just use the default: Forum Thread

I've gone ahead and changed. I couldn't find a question anywhere in there though. :icon_confused:

Edited 7 Years Ago by Nick Evan: n/a

You should not be querying an SQL server in the constructor of a class for best-practices reasons and it appears you are not calling .Dispose() on IDisposable instances in your class. Most, if not every, control you will encounter in .NET uses a late or delayed binding mechanism so the first place you should really consider populating a local data store (dataset, datatable) is in the form's load event.

In your cars class you are exposting public fields -- this is also not recommended for best practices. You should use public properties. Microsoft added "auto properties" for this reason: public string SomeProp { get; set; } Lastly why are you creating all of this in code? You can use the designer to create these classes for you and generate the related code.

Now as nothing above related to your actual question -- DdoubleD answered your question. You need to specify Select, Update, Delete, and Insert queries for your DataSet if you intend to do those operations (typically called CRUD)

This article has been dead for over six months. Start a new discussion instead.