-1

I am using Visual Studio 2022 (VB.Net) and SQL Server 2022 Express. I am not a programmer; I just like teaching myself coding and trying to learn.

I have a program, and I am trying to access the data in the table I created, but when I try to connect a DataGrid view to the table there is nothing there. See the screenshots.

If someone could give me a hint, it would be greatly appreciated.

enter image description here

5
  • The following may be of interest: Populating a DataSet from a DataAdapter, youtube.com/watch?v=peiorDq5oF0 (I didn't watch it, it's a result from performing a quick search), and stackoverflow.com/a/76349235/10024425 Commented Aug 16, 2024 at 1:55
  • 1
    The image is absolutely unnecessary. Anyone using this site and having any knowledge at all of what a DataGrid is would understand what "there is nothing there" means without a graphic demonstration. Images should be used only when absolutely necessary to demonstrate an issue, which clearly is not the case here. Please see Please do not upload images of code/data/errors. for a list of the many reasons NOT to use images. Commented Aug 16, 2024 at 3:15
  • 2
    Also, without providing any code at all, it's impossible to explain why you're not getting any data. It could be an issue with the SQL you're using, or an issue with how you're attempting to connect the DataGrid, or another issue, none of which can be deduced from looking at nothing but a useless image. See How to Ask and minimal reproducible example, and then edit your post to provide some useful details so that we can try to help. Commented Aug 16, 2024 at 3:17
  • You don't populate a DataGridView or any other controls directly from a database. You use some data access technology to retrieve the data into a list of some sort and that list is the data source for the grid. You should learn about data access technologies in VB and WinForms first. You can use vanilla ADO.NET, as per the first comment above, or you can create a typed DataSet, or you can use Entity Framework, or you can use some other ORM. The choice is yours. Once you have such a data source, that can be used to populate your grid. Commented Aug 16, 2024 at 3:40
  • Note creating a typed DataSet would probably add an entry to the table you showed in your screenshot, although I've never done it that way so I'm not completely sure. Commented Aug 16, 2024 at 3:41

1 Answer 1

0

SQL Server

Imports System.Data.SqlClient

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Using connection As New SqlConnection(
            "Server=localhost\SQLEXPRESS;" &
            "Database=master;" &
            "Trusted_Connection=True;")

            Dim adapter As New SqlDataAdapter("select * from spt_values", connection)
            Dim table As New DataTable
            adapter.Fill(table)
            DataGridView1.DataSource = table

        End Using

    End Sub
End Class

MySQL

Imports MySql.Data.MySqlClient

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Using connection As New MySqlConnection(
            "server=localhost;" &
            "port=3306;" &
            "database=sakila;" &
            "user=root;" &
            "password=********;")

            Dim adapter As New MySqlDataAdapter("select * from city", connection)
            Dim table As New DataTable
            adapter.Fill(table)
            DataGridView1.DataSource = table

        End Using

    End Sub
End Class
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks mate, I'll have a play around with it. I'm a complete novice at this, tool making is my skill set to but I love trying to learn this.
@Kevin, it will be about the same for SQL Server. The class names are slightly different: SqlConnection instead of MySqlConnection. I just couldn't post the untested code.
@KonstantinMakarov You don't need to call connection.Open() when using adapter.Fill(ds)
Further to that previous comment, Fill and Update will both open the connection if required and leave the connection in the same state as it was originally. That means that any single call to Fill or Update should not be preceded with a call to Open on the connection. If you're making multiple calls though, you should call Open and Close yourself or else the connection will be closed and reopened between Fill or Update calls.
@Kevin, firstly, if this answer has resolved your issue, please accept it by clicking the check/tick mark. Secondly, don't ask "one more question" in a comment on an answer to a different question. This is a Q&A site, not a forum. If you have a separate question then post a new question and someone can answer that with another answer. If you believe that it is part of the same question then you should e4dit this question, but that's not appropriate in this case.
|

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.