The below code was written to bind the datatable to the grid, but i am unable to fill the grid.... I am unable to fill the datagrid, I'll be very thankfull for your help.

Private Sub BindGrid()

    Dim CMD As New SqlCommand
    Dim ad As New SqlDataAdapter(CMD)
    Dim DT As DataTable
    CMD.Connection = constring
    CMD.CommandText = "Exec  SP_View_Emp_details ' " & txtPayrollPeriod.Text & "'"
    DT = CMD.ExecuteScalar
    constring.Open()
    ad.Fill(DT)
    DataGridView1.DataSource = DT
    CMD.ExecuteNonQuery()
    'DataGridView1.DataBind()
    constring.Close()

End Sub

ExecuteScalar is for queries that return a single result, eg SELECT Count(*) FROM table1

To load the data into the DataTable you will need to use DT.Load(CMD.ExecuteReader) or something similar. The second Execute (ExecuteNonQuery) is not needed.

Also, you can leave out the DataAdaptor completely, if you are binding the gridview to the DataTable directly the DataAdapter is not needed. Alternately, If you intend to use the DataAdaptor for updating you should bind the gridview to the DataAdaptor instead.

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.