2 Replies - 1102 Views - Last Post: 30 April 2013 - 05:34 AM Rate Topic: -----

#1 parry2411   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 13-February 13

WPF Application SQL Database Check For Dupliate

Posted 30 April 2013 - 04:11 AM

Im building a car stock system, and i need to add a feature where, if the user types in a new record of a car and the REG of the car is in the table then it brings an error message to say that.

if not then the new item is added.

Here is my code:

private void AddNewCar_Click(object sender, RoutedEventArgs e)
            MySqlCommand cmd = new MySqlCommand();

            String carManufacture = textBoxCARManufac.Text;
            String carModel = textBoxCARModel.Text;
            String carColour = textBoxCarBodColo.Text;
            int carMileage = int.Parse(textBoxCArMillea.Text);
            int carYearReg = int.Parse(textBoxCARREGYear.Text);
            String carReg = textBox1CARregplate.Text;
            String fuel = textBoxCARFuel.Text;
            int carEng = int.Parse(textBoxCarEngSize.Text);
            float carprice = float.Parse(textBoxCARPrice.Text);
            float carTaxcost = float.Parse(textBoxCArTax.Text);

            cmd.CommandText = "INSERT INTO stockList (Manufacturer,CarModel,BodyColour,AquiredMileage,RegistartionYear,CarRegistration,FuelType,EngineSize,Price,CarTaxCost12Months) VALUES (@Manufacturer, @carModel, @carColour, @carMileage, @carYearReg, @carReg, @fuel, @carEng, @carPrice, @carTaxCost)";

            cmd = SDS1.Prepare(cmd);

            cmd.Parameters.AddWithValue("@Manufacturer", carManufacture);
            cmd.Parameters.AddWithValue("@carModel" , carModel);
            cmd.Parameters.AddWithValue("@carColour" , carColour);
            cmd.Parameters.AddWithValue("@carMileage" , carMileage);
            cmd.Parameters.AddWithValue("@carYearReg" , carYearReg);
            cmd.Parameters.AddWithValue("@carReg" , carReg);
            cmd.Parameters.AddWithValue("@fuel" , fuel);
            cmd.Parameters.AddWithValue("@carEng" , carEng);
            cmd.Parameters.AddWithValue("@carPrice" , carprice);
            cmd.Parameters.AddWithValue("@carTaxCost", carTaxcost);

How would i go abouts to achieve this?


Is This A Good Question/Topic? 0
  • +

Replies To: WPF Application SQL Database Check For Dupliate

#2 andrewsw   User is offline

  • RequestedRangeNotSatisfiable
  • member icon

Reputation: 6552
  • View blog
  • Posts: 26,565
  • Joined: 12-December 12

Re: WPF Application SQL Database Check For Dupliate

Posted 30 April 2013 - 04:50 AM

Presumably the registration number is the primary key in the table. In which case, your current code would produce an error, and you could handle this error using Try..Catch.


Alternatively, you could run a query to retrieve this registration number from the table. If this doesn't return anything then you know that the number isn't already in the table.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6217
  • View blog
  • Posts: 21,465
  • Joined: 05-May 12

Re: WPF Application SQL Database Check For Dupliate

Posted 30 April 2013 - 05:34 AM

I agree with andrewsw and recommend running a query to check for collision. Even better you could have an event that keys off the registration textbox change and disables the Add button and perhaps shows text elsewhere indicating that the car is already registered.

As an aside, you are using WPF, yet you are programming in it as if you were using WinForms or Win32. I know that the learning curve to follow the MVVM / MVC style of programming is steep, but it is a climb worth taking because it will lead you to better program infrastructures that support better modularity and testability.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1