0

I have four columns in Datagridview. I want to fill first two columns with data from sql database. I try to fill Datagridview. It not display data, but it generate rows.

This is my code:

getConnect()
    Try
        Conn.Open()
        Dim strSQL As String = "SELECT EMP_ID, EMP_NAME FROM EMPLOYEE ORDER BY EMP_NAME ASC"
        Conn.Close()
        Dim da As New SqlDataAdapter(strSQL, Conn)
        Dim dt As New DataTable("EMPLOYEE")
        da.Fill(dt)
        ATCGRID.DataSource = dt
    Catch ex As SqlException
        MsgBox(ex.Message, MsgBoxStyle.Critical, "SQL Error")
    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Critical, "General Error")
    End Try

Please check my code and give me solution...

Dennis
  • 32,200
  • 11
  • 64
  • 79
Thanzeem
  • 133
  • 5
  • 13
  • 22

3 Answers3

2

Try this code .

getConnect()
Try
    Conn.Open()
    Dim strSQL As String = "SELECT EMP_ID, EMP_NAME FROM EMPLOYEE ORDER BY EMP_NAME ASC"
    Conn.Close()
    Dim da As New SqlDataAdapter(strSQL, Conn)
    Dim ds As new Dataset
    da.Fill(ds,"EMPLOYEE")
    ATCGRID.DataSource = ds.tables(0)
Catch ex As SqlException
    MsgBox(ex.Message, MsgBoxStyle.Critical, "SQL Error")
Catch ex As Exception
    MsgBox(ex.Message, MsgBoxStyle.Critical, "General Error")
End Try
saysansay
  • 96
  • 3
0

Thanks for the sub getConnect() it worked perfectly. mine also worked.

Sub RefreshGrid()
 ' refresh the datagrid
  OpenConnect()

CmdSql.CommandText = "SELECT manager_id,manager_name FROM   tbl_Manager"
    Dim ds As DataSet = New DataSet()
    adp.Fill(ds)
    dgvMgr.DataSource = ds.Tables(0)
    'THIS MODULE WORKED JUST Please Fill Property Columns 
    'DataPropertyName as Field Database, 
    'Eg : Column1-DataPropertyName=manager_id and so on.
End Sub
CoderPi
  • 12,985
  • 4
  • 34
  • 62
0

Public Sub OpenConnect()

    Try
        CmdSql.Connection = conn
        conn.Open()
        CmdSql.CommandType = CommandType.Text

    Catch ex As Exception
        ' MsgBox(ex.Message)
    End Try
End Sub

' this worked perfectly