Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
private void combopop()
        {
            using (connection = new SqlConnection(connectionString))
            using (SqlDataAdapter adapter = new SqlDataAdapter("Select * FROM BiludstyrTABEL", connection))
            {
                DataTable dt3 = new DataTable();
                adapter.Fill(dt3);

                OScomboBox.DataSource = dt3;
                OScomboBox.ValueMember = "StregkodeID";
                OScomboBox.DisplayMember = "Name";
                OScomboBox.SelectedIndex = -1;
            }
            
        }

        private void OScomboBox_Click(object sender, EventArgs e)
        {
            combopop();
        }

        private void OScomboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            OSfyld1();
        }

        private void OSfyld1()
        {
            using (connection = new SqlConnection(connectionString))
            using (SqlDataAdapter adapter = new SqlDataAdapter("Select * FROM BiludstyrTABEL WHERE StregkodeID = '" + OScomboBox.SelectedIndex + "'", connection))
            {
                DataTable dt4 = new DataTable();
                adapter.Fill(dt4);

                foreach (DataRow dr in dt4.Rows)
                {
                    OSnavntextBox.Text = dr["Navn"].ToString();
                    OSproducenttextBox.Text = dr["Producent"].ToString();
                    OSkategoritextBox.Text = dr["Kategori"].ToString();
                    OSprismmomstextBox.Text = dr["Pris m. Moms"].ToString();
                    OSantaltextBox.Text = dr["Antal"].ToString();
                }
            }
        }


What I have tried:

When i Click my combobox dropdown arrow it displays the valuemembers (StregkodeID) in a dropdowncombobox. The OScomboBox.SelectedIndex =-1; makes all the ID's appear in the dropdownLIST, if the i didnt write this line, the first ID (the number 1 - database self identifier) would appear in the combobox when clicking the dropdown arrow.

My problem: When click the ID 2 (second line on the ID list - of course) it displays the first entry in the database (it display the entry of the FIRST database entry) !!
The index is shiftet when populating the combobox right? So that the first entry from the database is set to index 0 of the combobox? When i click the ID of 1, nothing appears (my textboxes are empty). SO every time i pick an ID in the combobox it displays the data of the prior index (pick 2 get 1, pick 5 get 4 and so on).

Been working on this for several days, and cant seem to find any answers. PLEASE HELP!
Posted
Updated 20-Oct-17 2:04am
v2
Comments
CHill60 20-Oct-17 8:09am    
You've answered your own question - the combo box list starts at 0 not 1. BUT you should be using OSComboBox.SelectedValue not the index
Richard Deeming 20-Oct-17 9:44am    
NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]

using (SqlDataAdapter adapter = new SqlDataAdapter("Select * FROM BiludstyrTABEL WHERE StregkodeID = @StregkodeID", connection))
{
    adapter.SelectCommand.Parameters.AddWithValue("@StregkodeID", OScomboBox.SelectedValue);
    ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900