1

I have code that is pulling information from SQL server and displaying information about cars.

The problem is that the data is displayed but duplicated. The SQL Data has no duplications but the list boxes are displaying the same data twice.

The code is as follows:

private void Show_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("Data Source=XXXX");
            DataTable dt = new DataTable();
            SqlDataAdapter SDA = new SqlDataAdapter("SELECT Number_Plate,Registered_Keeper,Make,Model,Year_Of_Make,Colour,Engine_Size,Transmission,Fuel_Type FROM datatable WHERE Number_Plate like '%" + label1.Text + "%'", conn);
            SDA.Fill(dt);
            if (SDA.Fill(dt) == 0)
            {

                button5.Visible = false;
                button4.Visible = true;
                label11.Visible = true;
                Show.Visible = false;
                button3.Visible = true;
                Make.Visible = false;
                Model.Visible = false;
                Year_Of_Make.Visible = false;
                Colour.Visible = false;
                Engine_Size.Visible = false;
                Transmission.Visible = false;
                Fuel_Type.Visible = false;
                Registered_Keeper.Visible = false;
                Plate.Visible = false;
                label2.Visible = false;
                label3.Visible = false;
                label4.Visible = false;
                label5.Visible = false;
                label6.Visible = false;
                label7.Visible = false;
                label8.Visible = false;
                label9.Visible = false;
                label12.Visible = false;
            }
            else
            {
                Plate.DataSource = dt;
                Plate.DisplayMember = "Number_Plate";

                Make.DataSource = dt;
                Make.DisplayMember = "Make";

                Model.DataSource = dt;
                Model.DisplayMember = "Model";

                Year_Of_Make.DataSource = dt;
                Year_Of_Make.DisplayMember = "Year_Of_Make";

                Colour.DataSource = dt;
                Colour.DisplayMember = "Colour";

                Engine_Size.DataSource = dt;
                Engine_Size.DisplayMember = "Engine_Size";

                Transmission.DataSource = dt;
                Transmission.DisplayMember = "Transmission";

                Fuel_Type.DataSource = dt;
                Fuel_Type.DisplayMember = "Fuel_Type";

                Registered_Keeper.DataSource = dt;
                Registered_Keeper.DisplayMember = "RegIstered_Keeper";

                Show.Visible = false;
                button3.Visible = true;
            }

        }

Note: When I add the data in SQL the primary key is missing about 20 numbers, so my data is 20 cars, but when I add a new one its primary key is around 50.

3 Answers 3

3

Well, you do SDA.Fill(dt) twice. Try removing the one on the line before the if.

...
SqlDataAdapter SDA = ...
if (SDA.Fill(dt) == 0)
...
Sign up to request clarification or add additional context in comments.

2 Comments

Just for completeness - one could also define the DataTable to have a primary key and then calling fill multiple times will update the table rather than append new rows. Also... Sql injection: "...WHERE Number_Plate like '%" + label1.Text + "%'".
Correct, on both accounts.
1

you are calling SDA.Fill(dt); twice, once outside, once inside the if statement,

Comments

1

SDA.Fill(dt) was called 2 times, try to change it to dt.count < 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.