Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 27th, 2006, 10:57 PM
Authorized User
 
Join Date: Apr 2006
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default Changing Primary Key Value

Hello. In case you didn't know, I have written a program that stores information about the vehicles you own in a database. I have a problem, however, now that all of you have helped me write the changes in the dataset to the database.

I use a combobox to show all of the cars currently in the database. However, when a car is deleted from the database, the combobox doesn't work. Let me explain. Let's say there are 3 cars in the database. To display the information on the windows form, I use textboxes. When the user selects a car in the combobox, I use the 'SelectedIndex' property of the combobox to display the correct row of data in the textboxes, like so:

txtCarMake.Text = vehiclesDataSet1.Cars.FindByCarID(cbxCurrentVehicl e.SelectedIndex).Make.ToString();

However, if the second car in the list is deleted, the data for the third will no longer be displayed because using this code, when the third car entry is selected in the combobox, it will actually be the second (only the first and third cars will remain because the second was deleted), so the textboxes will try to display data from the second row which will no longer exist.

My question is, is there a way to change the values of the primary key so that this will not happen, or is there a better way to go about displaying the data in the textboxes? Thank-you in advance.


Computers will never surpass the human brain; no computer will ever be able to replicate human stupidity.
__________________
If only computers could write programs themselves...
 
Old April 28th, 2006, 09:34 PM
Authorized User
 
Join Date: Apr 2006
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default

problem is that when you delete your car info you should bind again your combobox with the correct data, dataset with the correct info, and clean the textbox information as well. Is this the problem?

 
Old April 30th, 2006, 10:14 PM
Authorized User
 
Join Date: Apr 2006
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm still new. Could you possibly give me an example of how to do this in code? I did it with the properties window before, so I'm not sure how to do this 'manually'.

Computers will never surpass the human brain; no computer will ever be able to replicate human stupidity.
 
Old May 4th, 2006, 10:58 PM
Authorized User
 
Join Date: Apr 2006
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Please, someone tell me how to bind the combobox with the correct data again as rumbafum said. I'm desperate! I'm begging you, tell me how to do this. Thank-you!

Computers will never surpass the human brain; no computer will ever be able to replicate human stupidity.
 
Old May 8th, 2006, 05:14 AM
Authorized User
 
Join Date: Apr 2006
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry for the late answer... Need more info about this... Are you deleting you're info with your app or directly in your db? Can you post some code where you're setting values to the combo, textbox?

 
Old May 8th, 2006, 11:43 AM
Authorized User
 
Join Date: Apr 2006
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default

First, I am deleting the info from the app, with

        private void btnYes_Click(object sender, EventArgs e)
        {

            vehiclesDataSet1.Cars.Rows[storeSelectedCar].Delete();
            carsTableAdapter1.Update(vehiclesDataSet1);
            vehiclesDataSet1.Cars.AcceptChanges();
        }



Here is the code which binds the textboxes with the first row in the database when the program first starts:

                txtYear.Text = vehiclesDataSet1.Cars.FindByCarID(0).Year.ToString ();
                txtMake.Text = vehiclesDataSet1.Cars.FindByCarID(0).Make.ToString ();
                txtModel.Text = vehiclesDataSet1.Cars.FindByCarID(0).Model.ToStrin g();
                txtMileage.Text = vehiclesDataSet1.Cars.FindByCarID(0).Mileage.ToStr ing();




When the "selectedIndex" property of the combobox is changed, the program displays the row of data with the primary key which matches the "selectedIndex" value of the combobox (ie. if the first entry in the combobx is selected (0), row 0 is diplayed).

        private void cbxCurrentVehicle_SelectedIndexChanged(object sender, EventArgs e)
        {
            int currentlySelectedCar = cbxCurrentVehicle.SelectedIndex;
            int selectedItemIndex = cbxCurrentVehicle.SelectedIndex;

            try
            {
                txtYear.Text = vehiclesDataSet1.Cars.FindByCarID(selectedItemInde x).Year.ToString();
                txtMake.Text = vehiclesDataSet1.Cars.FindByCarID(selectedItemInde x).Make.ToString();
                txtModel.Text = vehiclesDataSet1.Cars.FindByCarID(selectedItemInde x).Model.ToString();
                txtMileage.Text = vehiclesDataSet1.Cars.FindByCarID(selectedItemInde x).Mileage.ToString();
            }

            catch (NullReferenceException ex)
            {

            }

            catch (Exception ex)
            {
                MessageBox.Show("An error has occurred while updating vehicle information boxes.");
            }
        }



And finally, I pulled this code from mainWindow.Designer.cs (the designer's file, as if you didn't know :)). I believe this is what binds the combobox with the data, but I'm not sure.

// cbxCurrentVehicle
            //
            this.cbxCurrentVehicle.DataSource = this.carsBindingSource;
            this.cbxCurrentVehicle.DisplayMember = "Model";
            this.cbxCurrentVehicle.FormattingEnabled = true;
            this.cbxCurrentVehicle.Location = new System.Drawing.Point(13, 46);
            this.cbxCurrentVehicle.Name = "cbxCurrentVehicle";
            this.cbxCurrentVehicle.Size = new System.Drawing.Size(165, 21);
            this.cbxCurrentVehicle.TabIndex = 0;
            this.cbxCurrentVehicle.ValueMember = "CarID";
            this.cbxCurrentVehicle.SelectedIndexChanged += new System.EventHandler(this.cbxCurrentVehicle_Selecte dIndexChanged);



I hope this is what you needed. Thanks again.

Computers will never surpass the human brain; no computer will ever be able to replicate human stupidity.
 
Old May 8th, 2006, 08:05 PM
Authorized User
 
Join Date: Apr 2006
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok... Some issues here... When you change index for instance you should use the combo value member and not the index:

 private void cbxCurrentVehicle_SelectedIndexChanged(object sender, EventArgs e)
        {
            long selectedItemValue = (long)cbxCurrentVehicle.SelectedValue;

            try
            {
                txtYear.Text = vehiclesDataSet1.Cars.FindByCarID(selectedItemValu e).Year.ToString();
                txtMake.Text = vehiclesDataSet1.Cars.FindByCarID(selectedItemValu e).Make.ToString();
                txtModel.Text = vehiclesDataSet1.Cars.FindByCarID(selectedItemValu e).Model.ToString();
                txtMileage.Text = vehiclesDataSet1.Cars.FindByCarID(selectedItemValu e).Mileage.ToString();
            }

            catch (NullReferenceException ex)
            {

            }

            catch (Exception ex)
            {
                MessageBox.Show("An error has occurred while updating vehicle information boxes.");
            }
        }

Now you're ComboBox has a DataSource that is this.carsBindingSource which is not the same you are using to delete? Is this correct? If you use vehiclesDataSet1 as DataSource all your problems are solved...

So when you're deleting rows using vehiclesDataSet DataSet you'll need to update this DataSource as well to display only the information you want...

private void btnYes_Click(object sender, EventArgs e)
{

       vehiclesDataSet1.Cars.Rows[storeSelectedCar].Delete();
       carsTableAdapter1.Update(vehiclesDataSet1);
       vehiclesDataSet1.Cars.AcceptChanges();
       carsBindingSource.Cars.Clear();
       carsBindingSource.Merge(vehiclesDataSet);
       txtYear.Text = vehiclesDataSet1.Cars.FindByCarID((long)cbxCurrent Vehicle.SelectedValue).Year.ToString();
            txtMake.Text = vehiclesDataSet1.Cars.FindByCarID((long)cbxCurrent Vehicle.SelectedValue).Make.ToString();
            txtModel.Text = vehiclesDataSet1.Cars.FindByCarID((long)cbxCurrent Vehicle.SelectedValue).Model.ToString();
            txtMileage.Text = vehiclesDataSet1.Cars.FindByCarID((long)cbxCurrent Vehicle.SelectedValue).Mileage.ToString();
}

If you use vehiclesDataSet as DataSource:

private void btnYes_Click(object sender, EventArgs e)
{

       vehiclesDataSet1.Cars.Rows[storeSelectedCar].Delete();
       carsTableAdapter1.Update(vehiclesDataSet1);
       vehiclesDataSet1.Cars.AcceptChanges();
       txtYear.Text = vehiclesDataSet1.Cars.FindByCarID((long)cbxCurrent Vehicle.SelectedValue).Year.ToString();
            txtMake.Text = vehiclesDataSet1.Cars.FindByCarID((long)cbxCurrent Vehicle.SelectedValue).Make.ToString();
            txtModel.Text = vehiclesDataSet1.Cars.FindByCarID((long)cbxCurrent Vehicle.SelectedValue).Model.ToString();
            txtMileage.Text = vehiclesDataSet1.Cars.FindByCarID((long)cbxCurrent Vehicle.SelectedValue).Mileage.ToString();
}


 
Old May 9th, 2006, 05:26 PM
Authorized User
 
Join Date: Apr 2006
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default

OMG!!! Thank-you so much rumbafum it works perfectly now! The code you just posted me didn't work at first, but I quickly figured out why; I forgot to tell you that the deleting of a car was done in a seperate form opened as a DialogResult, so I just placed some of the code from "btnYes" from the delete car window to the mainwindow, got rid of the "long" definition, and used "Convert.ToInt32" so that the selectedValue was explicitly converted from "object" to "int".

Once again, thank-you so much!

Computers will never surpass the human brain; no computer will ever be able to replicate human stupidity.
 
Old May 9th, 2006, 05:27 PM
Authorized User
 
Join Date: Apr 2006
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default

BTW, on a final note, that's two threads I've needed for this program already, hopefully it won't be three! Maybe this is too hard of a program for a beginner to databases, but after this I should be on my way to being an expert!

Computers will never surpass the human brain; no computer will ever be able to replicate human stupidity.
 
Old May 9th, 2006, 07:19 PM
Authorized User
 
Join Date: Apr 2006
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ups sorry about the long thingy... Glad i helped, good luck with your project






Similar Threads
Thread Thread Starter Forum Replies Last Post
Foreign key not updating with Primary key xavier1945 BOOK: Access 2003 VBA Programmer's Reference 2 July 4th, 2007 09:48 PM
Primary key cf2006 BOOK: Beginning Visual Basic 2005 Databases ISBN: 978-0-7645-8894-5 1 July 31st, 2006 07:21 PM
FOREIGN KEY and PRIMARY KEY Constraints junemo Oracle 10 June 15th, 2004 01:00 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.