none
Connection to Database success along with the combobox display given info but it wont display the label with prices RRS feed

  • Question

  • Afternoon, I think I mentioned before I manage to operate the code to actually display the name of elements from access database 2002-03.

    However, I am not able to display the cost of the item at the same time. For example, I select in the combobox "War Factory" but it wont pop up the label showing how much does it cost. 

    Here is the code which establish the connection to database and combobox. I'm open to answers on how I can get the label to display for each selected Item simultaneously. Thanks. 

    Dim con As New OleDbConnection
        Dim ds As New DataSet
        Dim da As OleDb.OleDbDataAdapter
        Dim sql As String = "SELECT * From Infrastructure WHERE Elements"

    Private Sub Player1_Game_Grid_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    'This automatically loads the data within the given tables: Elements 
            Build_1.Items.Clear()
            Try
                con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;" & "Data Source = C:\Users\BusinessOffice1Room\Documents\Waliur File\A Level\Computer Science\Coursework\Work In progression\State of War League Table.mdb"
                'con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;" & "Data Source = N:\Computer Science Coursework\Work In progression\State of War League Table.mdb"
                con.Open()
                da = New OleDb.OleDbDataAdapter(sql, con)
                da.Fill(ds, "Infrastructure")
                sql = "SELECT * From Infrastructure WHERE Elements= " & "'" & Build_1.Text & "'"
                da = New OleDb.OleDbDataAdapter(sql, con)
                con.Close()
                con.Open()
                da.Fill(ds, "Infrastructure")
                For i As Integer = 0 To ds.Tables("Infrastructure").Rows.Count - 1
                    Me.Build_1.Items.Add(ds.Tables("Infrastructure").Rows(i)(0))
                Next
            Catch ex As Exception
                MsgBox("Error : " + ex.Message)
            Finally
                con.Close()
            End Try

    I hope the code above is a good starter to help anyone with this issue.

    Saturday, March 4, 2017 3:12 PM

Answers

  • Hello,

    What is the exact error message?

    Also, rather than using 

    sql = "SELECT * From Infrastructure WHERE Elements= " & "'" & Build_1.Text & "'"

    Create with parameters

    Imports System.Data.OleDb
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim da As New OleDbDataAdapter
            Using con As New OleDbConnection With
                {
                    .ConnectionString =
                    <c>
                        PROVIDER=Microsoft.Jet.OLEDB.4.0;
                        Data Source = C:\Users\BusinessOffice1Room\Documents\Waliur File\A Level\Computer Science\Coursework\Work In progression\State of War League Table.mdb
                    </c>.Value
                }
                da.SelectCommand = New OleDb.OleDbCommand With
                    {
                        .Connection = con,
                        .CommandText = "SELECT * From Infrastructure WHERE Elements = @Elements"
                    }
    
                da.SelectCommand.Parameters.AddWithValue("@Elements", Build_1.Text)
                con.Open()
                da.Fill(ds, "Infrastructure")
            End Using
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Waliur Rahman Monday, March 6, 2017 1:29 PM
    Saturday, March 4, 2017 3:43 PM
    Moderator

All replies

  • Hello,

    What is the exact error message?

    Also, rather than using 

    sql = "SELECT * From Infrastructure WHERE Elements= " & "'" & Build_1.Text & "'"

    Create with parameters

    Imports System.Data.OleDb
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim da As New OleDbDataAdapter
            Using con As New OleDbConnection With
                {
                    .ConnectionString =
                    <c>
                        PROVIDER=Microsoft.Jet.OLEDB.4.0;
                        Data Source = C:\Users\BusinessOffice1Room\Documents\Waliur File\A Level\Computer Science\Coursework\Work In progression\State of War League Table.mdb
                    </c>.Value
                }
                da.SelectCommand = New OleDb.OleDbCommand With
                    {
                        .Connection = con,
                        .CommandText = "SELECT * From Infrastructure WHERE Elements = @Elements"
                    }
    
                da.SelectCommand.Parameters.AddWithValue("@Elements", Build_1.Text)
                con.Open()
                da.Fill(ds, "Infrastructure")
            End Using
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Waliur Rahman Monday, March 6, 2017 1:29 PM
    Saturday, March 4, 2017 3:43 PM
    Moderator
  • The specific Error for the first issue: the <g class="gr_ gr_80 gr-alert gr_spell gr_run_anim ContextualSpelling multiReplace" data-gr-id="80" id="80">defense</g> <g class="gr_ gr_5 gr-alert gr_spell gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="5" id="5">combobox</g> to connect with the access database shows: error syntax From clause vb

    The second issue shows the error relating to connection to show the label in a database. I <g class="gr_ gr_232 gr-alert gr_spell gr_run_anim ContextualSpelling" data-gr-id="232" id="232">cant</g> remember the exact phrase but it goes along the lines of you <g class="gr_ gr_374 gr-alert gr_spell gr_run_anim ContextualSpelling" data-gr-id="374" id="374">cant</g> make label an object reference. 

    Saturday, March 4, 2017 3:54 PM
  • Apoologies of the weird html code I only realise grammarly edit was on for i dont know what reason
    Saturday, March 4, 2017 3:55 PM
  • Hello,

    I will not respond unless you remove the formatting in your last reply, note I had to remove your formatting in your initial reply before being able to read it.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, March 4, 2017 3:56 PM
    Moderator
  • Apologies about all that messy code I blame grammarly for that I tend to use it during essay writing but not this
    Saturday, March 4, 2017 4:03 PM
  • Im going to delete this Question and re structure it I might have not specified it clearly on what I plan on doing
    Saturday, March 4, 2017 4:07 PM
  • Morning, I can understand the way problem I have is difficult and takes time to solve. I think a hint is the try method I have used but somewhat I cant display the actual integers into the label. Has anyone been in a similar situation to this but manage to solve it?
    Sunday, March 5, 2017 11:12 AM