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
and Connection
directly to the constructor of the Command
.
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 valid DataSource
. See https://docs.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#remarks
While Taldr.Read
DataGridView1.DataSource = Taldr
End While
If all you are doing in you Catch
is showing a message box, show it in the user interface.
Private ConStr As String = "DSN=TallyODBC_9000;PORT=9000;DRIVER=Tally ODBC Driver;SERVER={(local)}"
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
DataGridView1.DataSource = GetNames()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Function GetNames() As DataTable
Dim dt As New DataTable
Using cn As New OdbcConnection(ConStr),
cmd As New OdbcCommand("SELECT $Name FROM Ledger", cn)
cn.Open()
Using reader = cmd.ExecuteReader
dt.Load(reader)
End Using
End Using
Return dt
End Function
Even though your problem is solved I am replying to thread As you are using ODBC connection which is dependent on ODBC driver, you will get issues if you change system or want to use multiple systems
Better approach to fetch data from tally is using XML API, You no need to change any configuration you can use the same config as you did for ODBC Since you want only Ledger Names you can use below code that fetches ledger names and posts to data from a2 Change company Name in XML in <SVCURRENTCOMPANY>ABC Company</SVCURRENTCOMPANY>
tag even multiple companies opened in tally it fetches data from target company
Public Sub GetTallyLedgersList()
Dim xmlhttp As New MSXML2.XMLHTTP60, myurl As String
Dim objXML As New MSXML2.DOMDocument60
myurl = "http://localhost:9000"
xmlhttp.Open "POST", myurl, False
xmlhttp.send "<ENVELOPE><HEADER><VERSION>1</VERSION>
<TALLYREQUEST>Export</TALLYREQUEST><TYPE>Data</TYPE><ID>List Of Ledgers</ID></HEADER><BODY><DESC><STATICVARIABLES><SVEXPORTFORMAT>$$SysName:XML</SVEXPORTFORMAT><SVCURRENTCOMPANY>ABC Company</SVCURRENTCOMPANY></STATICVARIABLES>" & _
"<TDL><TDLMESSAGE><REPORT ISMODIFY=""No"" ISFIXED=""No"" ISINITIALIZE=""No"" ISOPTION=""No"" ISINTERNAL=""No"" NAME=""List Of Ledgers""><FORMS>List Of Ledgers</FORMS></REPORT><FORM ISMODIFY=""No"" ISFIXED=""No"" ISINITIALIZE=""No"" ISOPTION=""No"" ISINTERNAL=""No"" NAME=""List Of Ledgers"">" & _
"<TOPPARTS>List Of Ledgers</TOPPARTS><XMLTAG>ListOfLedgers</XMLTAG></FORM><PART ISMODIFY=""No"" ISFIXED=""No"" ISINITIALIZE=""No"" ISOPTION=""No"" ISINTERNAL=""No"" NAME=""List Of Ledgers""><TOPLINES>List Of Ledgers</TOPLINES><REPEAT>List Of Ledgers : FormList Of Ledgers</REPEAT><SCROLLED>Vertical</SCROLLED>" & _
"</PART><LINE ISMODIFY=""No"" ISFIXED=""No"" ISINITIALIZE=""No"" ISOPTION=""No"" ISINTERNAL=""No"" NAME=""List Of Ledgers""><LEFTFIELDS>NAME</LEFTFIELDS></LINE><FIELD ISMODIFY=""No"" ISFIXED=""No"" ISINITIALIZE=""No"" ISOPTION=""No"" ISINTERNAL=""No"" NAME=""NAME""><SET>$NAME</SET><XMLTAG>NAME</XMLTAG></FIELD>" & _
"<COLLECTION ISMODIFY=""No"" ISFIXED=""No"" ISINITIALIZE=""No"" ISOPTION=""No"" ISINTERNAL=""No"" NAME=""FormList Of Ledgers""><TYPE>Ledger</TYPE></COLLECTION></TDLMESSAGE></TDL></DESC></BODY></ENVELOPE>"
Set objXML = New MSXML2.DOMDocument60
k = xmlhttp.responseText
If Not objXML.LoadXML(k) Then 'strXML is the string with XML'
Err.Raise objXML.parseError.ErrorCode, , objXML.parseError.reason
End If
Set Nameslist = objXML.SelectNodes("//LISTOFLEDGERS/NAME")
Count = 2
ActiveSheet.Cells(1, 1).Value = "Ledger Names"
For Each Name In Nameslist
s = Name.nodeTypedValue
ActiveSheet.Cells(Count, 1).Value = s
Count = Count + 1
Next
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.