[英]Not displaying tally ledgers in datagridview
Tried to list all the ledgers from tally through odbc.试图列出从计数到 odbc 的所有分类帐。 got nothing.
一无所获。 Mycode is as follows, no errors while executing.
Mycode如下,执行时没有报错。 but not displaying the list of ledgers into the datagridview1
但不将分类帐列表显示到 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.我很好奇您使用的数据库需要 ODBC 并且没有特定的提供程序。
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. Connection、Command 和 DataReader 等数据库对象必须关闭并释放。
Using...End Using
blocks take care of this even it there is an error. Using...End Using
块即使有错误也会处理这个问题。
You can pass the CommandText
and Connection
directly to the constructor of the Command
.您可以将
CommandText
和Connection
直接传递给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.您多次将
DataSource
重置为相同的值。 But, it won't work anyway because a DataReader is not a valid DataSource
.但是,它无论如何都不起作用,因为 DataReader 不是有效的
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 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
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.如果您在
Catch
中所做的只是显示一个消息框,请在用户界面中显示它。
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即使您的问题已解决,我仍在回复线程因为您正在使用依赖于 ODBC 驱动程序的 ODBC 连接,如果您更改系统或想要使用多个系统,则会遇到问题
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从计数中获取数据的更好方法是使用 XML API,您无需更改任何配置,您可以使用与 ODBC 相同的配置,因为您只需要从代码中获取 L ledger 名称的数据并使用以下代码a2 在
<SVCURRENTCOMPANY>ABC Company</SVCURRENTCOMPANY>
标签中更改 XML 中的公司名称,甚至在计数中打开了多家公司,它从目标公司获取数据
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.