user1271146
user1271146

Reputation: 45

Displaying data in Gridview in VB

I want to display data from database in DataGridView...This is my code...Its not working...Can anyone help me wat to do......

    Dim DBCONSRT, QRYSTR As String
    Dim strSQL, skunbr As String
    Dim DBCON, myConn, myCommand, rs As Object
    Dim NoOfRecords As Long
    skunbr = TextBox1.Text
    rs = CreateObject("ADODB.Recordset")
    Const DB_CONNECT_STRING = "Provider=MSDASQL.1;Persist Security Info=False;User ID=cpa5k;Data Source=NP1;DSN=NP1;UID=user;PASSWORD=pass;SDSN=Default;HST=ibslnpb1.sysplex.homedepot.com;PRT=4101;Initial Catalog=QA1MM;"
    myConn = CreateObject("ADODB.Connection")
    myCommand = CreateObject("ADODB.Command")
    myConn.Open(DB_CONNECT_STRING)
    myCommand.ActiveConnection = myConn
    myCommand.CommandText = "update QA1MM.STRSK_OH set OH_QTY = 250 where SKU_NBR = 100013 and STR_NBR = 116;"
    myCommand.Execute()
    strSQL = "select * from QA1MM.STRSK_OH where SKU_NBR =  " & skunbr & "  with ur FETCH FIRST 10 ROWS ONLY;"
    rs.Open(strSQL, myConn)
    DataGridView1.DataSource = rs
    DataGridView1.Refresh()
    myConn.Close()

Upvotes: 0

Views: 2327

Answers (2)

Instead of assigning a ADODB.RecordSet directly to the datasource of datagridview like that, first convert/fill the recordset values to a dataset like below

Dim myDA As OleDbDataAdapter = New OleDbDataAdapter
Dim myDS As DataSet = New DataSet

myDA.Fill(myDS, rs, "MyTable")
DataGridView1.DataSource = myDS.Tables(0)     
DataGridView1.Refresh() 

Upvotes: 1

PraveenVenu
PraveenVenu

Reputation: 8337

This is the classic VB way. Not .net

Please refer this MSDN link

Upvotes: 0

Related Questions