0

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

Edited by prancode: I am unable to fill the datagrid, I'll be very thankfull for your help.

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by |-|x
0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.