none
how to show information from another table using textboxes and button? RRS feed

  • Question

  • hey guys. I have a form where i can view the city and which section of the city,  there are two textbox where the city and what section of the city is shown when I view the map image. is it possible to show information from another table in my database using the two textbox and a button i have in my form? for example, only the students who live in that city and section will be shown in the datagridview if I click a button. i don't know what to do so it wil be great if you could help me guys. thanks :)
    Wednesday, December 5, 2018 5:00 PM

All replies

  • hey guys. I have a form where i can view the city and which section of the city,  there are two textbox where the city and what section of the city is shown when I view the map image. is it possible to show information from another table in my database using the two textbox and a button i have in my form? for example, only the students who live in that city and section will be shown in the datagridview if I click a button. i don't know what to do so it wil be great if you could help me guys. thanks :)

    Hi, 

    Of Course you can do,  

    Sub showTable1()
            Using con As New OleDbConnection("provider=microsoft.ace.oledb.12.0; data source=TestDB.accdb;jet oledb:database password=yourpassword")
                con.Open()
    
                Using cmd As New OleDbCommand
                    cmd.Connection = con                               ''##
    
                    cmd.CommandText = "SELECT * FROM Table1"                                            
    
                    Using dt As New DataTable
                        dt.Load(cmd.ExecuteReader)
                        If dt.Rows.Count > 0 Then
                            textbox1.Text = dt.Rows(0).Item("column1").ToString
                            textbox2.Text = dt.Rows(0).Item("column2").ToString
                           
                        End If
                    End Using
                End Using
            End Using
        End Sub
    Sub showTable2()
            Using con As New OleDbConnection("provider=microsoft.ace.oledb.12.0; data source=TestDB.accdb;jet oledb:database password=yourpassword")
                con.Open()
    
                Using cmd As New OleDbCommand
                    cmd.Connection = con                               ''##
    
                    cmd.CommandText = "SELECT * FROM Table2"                                            
    
                    Using dt As New DataTable
                        dt.Load(cmd.ExecuteReader)
                        If dt.Rows.Count > 0 Then
                            textbox1.Text = dt.Rows(0).Item("column1").ToString
                            textbox2.Text = dt.Rows(0).Item("column2").ToString
                           
                        End If
                    End Using
                End Using
            End Using
        End Sub

    you can put showtable1() in any event you want then put showtable2() in another event or when text changed or whatever to achieve your mission 

    hope it helps


    Regards From Amr_Aly

    Wednesday, December 5, 2018 5:12 PM
  • Thanks for helping but what I mean is that the information of the students from that city and section will be shown into the data gridview. in my form I have a picturebox where the map is shwon and the two textbox that shows what city and section it is. and if i click the view button, all students from that city and section will be shown in the data gridview. sorry for my poor explanation haha
    Wednesday, December 5, 2018 6:05 PM
  • Thanks for helping but what I mean is that the information of the students from that city and section will be shown into the data gridview. in my form I have a picturebox where the map is shwon and the two textbox that shows what city and section it is. and if i click the view button, all students from that city and section will be shown in the data gridview. sorry for my poor explanation haha

    You need to fill a grid view with some special query(that meet your needs) this is what i understood till now from you explanation this code may help you ... if you have more than than one table you may need an(INNER JOIN statement) in your query 

        Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
            Using cn As New OleDbConnection(your connection string)
                cn.Open()
                Using cmd As New OleDbCommand("Select (city) AS [city],
                                              (section) AS [section]
                                              From yourtable WHERE city=@a ", cn)
                    cmd.Parameters.Add("@a", OleDbType.VarChar).Value = TextBox.Text
                    Using ds As New DataSet, da As New OleDbDataAdapter(cmd)
                        da.Fill(ds, "yourtable")
                        DataGridView3.DataSource = ds.Tables("yourtable").DefaultView
    
                    End Using
                End Using
            End Using
        End Sub

    hope it helps ...........


    Regards From Amr_Aly

    Wednesday, December 5, 2018 8:02 PM
  • Hello,

    What are the table schemas along with relationships between table?

    What database is being used?


    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

    Wednesday, December 5, 2018 8:48 PM
    Moderator
  • I have cities and students as my tables. I have id, city, section, image in my cities table and I have studentname, city, section in my students table. I'm using MySql database
    Saturday, December 8, 2018 8:07 PM
  • Seems you might consider having a key for city rather than city name in the event a name is not spelled correctly or changes having a key no problem while using the name can lead to orphan records. If you want to associate city to student or student to city I would suggest the following. That way you can create a connection and command where the command receives two parameters for the WHERE condition then use a DataReader to iterate returning records or a DataReader to populate a DataTable.


    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, December 8, 2018 8:46 PM
    Moderator