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?

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

Edited 4 Years Ago by romz

This question has already been answered. Start a new discussion instead.