Tried to list all the ledgers from tally through odbc. got nothing. Mycode is as follows, no errors while executing. but not displaying the list of ledgers into the datagridview1
Try
Dim TalCon As OdbcConnection
TalCon = New OdbcConnection("DSN=TallyODBC_9000;PORT=9000;DRIVER=Tally ODBC Driver;SERVER={(local)}")
Dim Taldr As OdbcDataReader
Dim cmd As New OdbcCommand("SELECT $Name FROM Ledger")
TalCon.Open()
cmd.Connection = TalCon
Taldr = cmd.ExecuteReader()
While Taldr.Read
DataGridView1.DataSource = Taldr
End While
'displaying connection name to verify
TextBox1.Text = TalCon.ToString
Taldr.Close()
Catch ex As Exception
MsgBox("Master " & vbCrLf & ex.Message)
End Try
End Sub
I am curious what database you are using that requires ODBC and does not have a specific provider.
I have divided your code into user interface code and database code. This makes it easier to maintain.
Database objects like Connection, Command, and DataReader must be closed and disposed.
Using...End Using
blocks take care of this even it there is an error.You can pass the
CommandText
andConnection
directly to the constructor of theCommand
.Don't hold the connection open while you update the user interface. Comparatively, this is a long process and too long to keep a connection open
This doesn't make any sense. You are resetting the
DataSource
to the same value numerous times. But, it won't work anyway because a DataReader is not a validDataSource
. See https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.datagridview.datasource?view=netframework-4.8&f1url=%3FappId%3DDev16IDEF1%26l%3DEN-US%26k%3Dk(System.Windows.Forms.DataGridView.DataSource)%3Bk(TargetFrameworkMoniker-.NETFramework%2CVersion%253Dv4.8)%3Bk(DevLang-VB)%26rd%3Dtrue#remarksIf all you are doing in you
Catch
is showing a message box, show it in the user interface.