Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I am trying to display data from my database in textboxes that matches the text of the combo box however, have been unsuccessful. All the textboxes display are 0's
Here is the code I already have.
VB.NET
Private Sub ComboBox42_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox42.SelectedIndexChanged

        dataFile = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Simon\Documents\Visual Studio 2010\Projects\UWOShipTool\UWOShipTool\Ship.mdb"
        connString = dataFile
        myConnection.ConnectionString = connString
        Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM [Ships] WHERE [Name] = '" & ComboBox42.Text & "'", myConnection)
        myConnection.Open()
        Dim dr As OleDbDataReader = cmd.ExecuteReader

  
        While dr.Read
            Found = True
            Name = dr("Name")
            Dura = dr("Dura")
            Vertical = dr("Vertical")
            Horizontal = dr("Horizontal")
            Row = dr("Row")
            Turn = dr("Turn")
            Wave = dr("Wave")
            Armor = dr("Armor")
            Cabin = dr("Cabin")
            CannonChambers = dr("Cannon Chambers")
            Cargo = dr("Cargo")
        End While

        TextBox1.Text = Dura
        TextBox2.Text = Vertical
        TextBox3.Text = Horizontal
        TextBox4.Text = Row
        TextBox5.Text = Turn
        TextBox6.Text = Wave
        TextBox7.Text = Armor
        TextBox8.Text = Cabin
        TextBox9.Text = CannonChambers
        TextBox10.Text = Cargo
        TextBox11.Text = Adv
        TextBox12.Text = Trade
        TextBox13.Text = Battle

        myConnection.Close()


Thanks in advance. :)
Posted
Updated 4-Jun-20 4:55am
Comments
CHill60 31-Jan-16 18:16pm    
When you debug this code do you actually enter the loop While dr.Read? Do you only enter it once?
Consider (Seriously) using parameterised queries to avoid the risk of sql injection and to make your sql queries more robust.
Try reading this article - Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Sdolby 29-Aug-17 9:37am    
In this case, I don't need to worry about injection. As there is no way they can input data and it's all controlled inputs. However, when I wrote this code I was quite new to SQL and now I know better I paramaterise it(And barely even write in VB.net)


1 solution

I think you have to try the following:
VB.NET
Dim DB_Adapter As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM [Ships] WHERE [Name] = '" & ComboBox42.Text & "'", myConnection)

Dim dt As DataTable
DB_Adapter.Fill(dt)

For Each dr As DataRow In dt.Rows

' Here what you want to do with the info stored in the "dr" datarow from the select statement of your data base.

Next
 
Share this answer
 

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