6 Replies - 332 Views - Last Post: 19 November 2019 - 10:42 AM Rate Topic: -----

#1 dakako   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-November 19

Can Someone Help Me in My Assignment?

Posted 18 November 2019 - 01:41 PM

Create an application in which all of the cars in a database are displayed in a checked list box. When the user makes a selection, the car’s price is displayed. Use the HonestRalphsUsedCars database in your downloadable student files. Save the project as CarPriceFinder.

The issue that I have is that the output [PRICE] is incorrect. I am attaching a picture of the output.

Here is my code.

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

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

        private void Form1_Load(object sender, EventArgs e)
        {
            this.tblCarsTableAdapter.Fill(this.honestRalphsUsedCarsDataSet.tblCars);


        }

        private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

            this.tblCarsTableAdapter.Fill(this.honestRalphsUsedCarsDataSet.tblCars);
            for (int i = 0; i < checkedListBox1.Items.Count; i++)
            {
                
                checkedListBox1.SetItemCheckState(i, CheckState.Unchecked);
            }
                checkedListBox1.SetItemCheckState(checkedListBox1.SelectedIndex, CheckState.Checked);

            var query =
                from s in this.honestRalphsUsedCarsDataSet.tblCars
                where s.ID.ToString() == checkedListBox1.SelectedIndex.ToString()
                select s;
            foreach (var car in query)
            
                priceLabel.Text = "Price of the car is " + car.Price;
            
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dataGridView1_Selectionchanged(object sender, EventArgs e)
        {

        }
    }
}
:code:

This post has been edited by modi123_1: 18 November 2019 - 01:45 PM
Reason for edit:: In the future, please use the [code] tag button in the editor.


Is This A Good Question/Topic? 0
  • +

Replies To: Can Someone Help Me in My Assignment?

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15427
  • View blog
  • Posts: 61,802
  • Joined: 12-June 08

Re: Can Someone Help Me in My Assignment?

Posted 18 November 2019 - 01:46 PM

In what way is it wrong?
Was This Post Helpful? 0
  • +
  • -

#3 dakako   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-November 19

Re: Can Someone Help Me in My Assignment?

Posted 18 November 2019 - 03:01 PM

for example: car 1 has no price output
car 2 has price of car 1
car 3 has price of car 2 and so on.
Was This Post Helpful? 0
  • +
  • -

#4 maceysoftware   User is offline

  • Member Title
  • member icon

Reputation: 386
  • View blog
  • Posts: 1,640
  • Joined: 07-September 13

Re: Can Someone Help Me in My Assignment?

Posted 19 November 2019 - 07:57 AM

Does your ID in tblCars start at zero? Because CheckListBox.SelectedIndex will be zero based.
Was This Post Helpful? 0
  • +
  • -

#5 dakako   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-November 19

Re: Can Someone Help Me in My Assignment?

Posted 19 November 2019 - 08:08 AM

View Postmaceysoftware, on 19 November 2019 - 07:57 AM, said:

Does your ID in tblCars start at zero? Because CheckListBox.SelectedIndex will be zero based.


Nope it starts at 1.

View Postmaceysoftware, on 19 November 2019 - 07:57 AM, said:

Does your ID in tblCars start at zero? Because CheckListBox.SelectedIndex will be zero based.


Nope it starts at 1.
Was This Post Helpful? 0
  • +
  • -

#6 maceysoftware   User is offline

  • Member Title
  • member icon

Reputation: 386
  • View blog
  • Posts: 1,640
  • Joined: 07-September 13

Re: Can Someone Help Me in My Assignment?

Posted 19 November 2019 - 08:14 AM

Well if your comment was just about your table doesn't start at zero, then that is your issue.

car 1 has no price output (ID - 1 = SelectedIndex - 0)
car 2 has price of car 1 (ID - 2 = SelectedIndex - 1)
car 3 has price of car 2 and so on. (ID - 3 = SelectedIndex - 2)

You need to plus/minus one of these numbers so the ID and index match, if that is the way you want to do this.

This post has been edited by maceysoftware: 19 November 2019 - 08:15 AM

Was This Post Helpful? 0
  • +
  • -

#7 h4nnib4l   User is online

  • The Noid
  • member icon

Reputation: 1456
  • View blog
  • Posts: 2,070
  • Joined: 24-August 11

Re: Can Someone Help Me in My Assignment?

Posted 19 November 2019 - 10:42 AM

More generally - in C# (and many/most programming languages), collections are 0-based, meaning that the first item in the collection has an index of 0. SQL Server, when generating an auto-incremented ID, will start with 1. So when you write code that compares collection indexes to database record IDs, you have to manage that offset.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1