Hi, Im new in VB.Net. im having trouble with filling the datagridview with data. the program seems to skip the part where i declare the datagridview datasouce and only executes until da.fill(dt). here is my code, hope you could help me. thanks in advance.

  Imports System.Data.SqlClient



Public Class Form1

    Dim con As SqlConnection
    Dim cmd As SqlCommand
    Dim da As SqlDataAdapter
    Dim dt As DataTable
    Dim ds As DataSet

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

        Dim con = New SqlConnection("Data Source=SAMI-HP\SQLSERVER;Initial Catalog=INVENTORY;User ID=sa;Password=Administrator@123")
        con.Open()

        Dim cmd = New SqlCommand
        cmd.Connection = con
        cmd.CommandType = CommandType.Text
        cmd.CommandText = "SELECT * FROM Product"

        Dim da = New SqlDataAdapter
        da.SelectCommand = cmd
        da.Fill(dt)

        Me.DataGridView1.DataSource = dt
        Me.DataGridView1.Refresh()

    End Sub

End Class

Can you put a break point and check what is happening when da.fill(dt)? Whats coming into dt? is it blank?

yup its blank.

got it.. I didnt declare dt as new datatable. thanks for the reply pgmer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.