Solved

VB.NET and AS/400 connection and bind to datagrid?

Posted on 2006-06-12
3
512 Views
Last Modified: 2008-02-01
How do I set up ODBC for the AS400 (DB2) and bind to a datagrid?
0
Comment
Question by:bfth1505
3 Comments
 
LVL 10

Expert Comment

by:vadim63
ID: 16888531
0
 
LVL 1

Accepted Solution

by:
donhenderson1961 earned 500 total points
ID: 16894389
Alter this to suit your needs:

Private Sub btnRetrieve_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRetrieve.Click
        Dim strConnection As String
        Dim strSQL As String
        btnReport.Enabled = False
        'Replace the connection string
        strConnection = "Driver={Client Access ODBC Driver (32-bit)};System=<ip address>;UID=ODBCUSER;PWD=<pwd>;"
        'strConnection = "Driver={Client Access ODBC Driver (32-bit)};System=<ip address>;UID=ODBCUSER;PWD=<pwd>;"
        Dim objConnection As New Odbc.OdbcConnection(strConnection)
        Dim objCommand As Odbc.OdbcCommand
        Me.Cursor = Cursors.WaitCursor
        Try
            If ValidateWindow() Then
                Dim strFromDate As String = Mid(dtpFromDate.Text, 7, 2) & Mid(dtpFromDate.Text, 9, 2) & Mid(dtpFromDate.Text, 1, 2) & Mid(dtpFromDate.Text, 4, 2)
                Dim strToDate As String = Mid(dtpToDate.Text, 7, 2) & Mid(dtpToDate.Text, 9, 2) & Mid(dtpToDate.Text, 1, 2) & Mid(dtpToDate.Text, 4, 2)
                Dim intFromDate As Integer
                Dim intToDate As Integer
                intFromDate = strFromDate
                intToDate = strToDate


                If txtFromClient.Text <> "" Then
                    strSQL = "Select pcomp, cltgrp, pclnt#, glast, gfirst, pdate, ptype, payamt, pbalaf, gdebt#, pcomm, pguar#, cltgrp, gcomp, ccomp, cclnt" & _
                             " from data.scpaymi, data.scacct, data.scclnt" & _
                                 " Where pcomp = gcomp and pguar# = gdebt# and pcomp = ccomp and pclnt# = cclnt" & _
                                        " And pcomp = " & txtDivision.Text & _
                                        " And cclnt >= " & txtFromClient.Text & " and cclnt <= " & txtToClient.Text & _
                                        " And pdate >= " & intFromDate & " And pdate <= " & intToDate & _
                                        " order by pcomp, pclnt#, glast, gfirst"
                Else
                    strSQL = "Select pcomp, cltgrp, pclnt#, glast, gfirst, pdate, ptype, payamt, pbalaf, gdebt#, pcomm, pguar#, cltgrp, gcomp, ccomp, cclnt" & _
                                    " from cdata.scpaymi, data.scacct, data.scclnt" & _
                                    " Where pcomp = gcomp and pguar# = gdebt# and pcomp = ccomp and pclnt# = cclnt and " & _
                                    "pcomp = " & txtDivision.Text & _
                                    " and cltgrp = " & txtClientGroup.Text & _
                                    " And pdate >= " & intFromDate & _
                                    " and pdate <= " & intToDate & _
                                    " order by pcomp, cltgrp, pclnt#, glast, gfirst"
                End If

                lblError.Text = "Attempting connection"

                objCommand = New Odbc.OdbcCommand(strSQL)
                objCommand.Connection = objConnection

                Dim dareport As New Odbc.OdbcDataAdapter(objCommand)
                Dim dsReport As New DataSet
                dareport.Fill(dsReport)

                Dim dvLocalReport As DataView = dsReport.Tables(0).DefaultView
                dvLocalReport = dsReport.Tables(0).DefaultView
                dgPaymentRpt.DataSource = dvLocalReport
                dgPaymentRpt.SetDataBinding(dvLocalReport, "")

                Me.ReportDataView = dvLocalReport

                lblError.Text = lblError.Text & vbCrLf & "Retrieve complete, no error."
            End If
            If Me.ReportDataView.Count = 0 Then
                lblNoRecords.Visible = True
                btnReport.Enabled = False
            Else
                btnReport.Enabled = True
                lblNoRecords.Visible = False
            End If

        Catch ex As Odbc.OdbcException
            btnReport.Enabled = False
            lblValidationError.Text = "Is the VPN connected?" & vbCrLf & "Is the ISeries setup?"
            lblError.Text = "btnRetrieve_Click: ODBC ERROR" & vbCrLf & "ex.Source: " & ex.Source & vbCrLf & "ex.Message: " & ex.Message & vbCrLf & " Stack Trace: " & ex.StackTrace
        Catch ex As Exception
            btnReport.Enabled = False
            lblValidationError.Text = "Is the VPN connected?" & vbCrLf & "Is the ISeries setup?"
            lblError.Text = "btnRetrieve_Click: Exception ERROR" & vbCrLf & "ex.Source: " & ex.Source & vbCrLf & "ex.Message: " & ex.Message & vbCrLf & " Stack Trace: " & ex.StackTrace
        Finally
            Me.Cursor = Cursors.Arrow
            objCommand = Nothing
            If objConnection.State = ConnectionState.Open Then
                objConnection.Close()
            End If
            objConnection = Nothing
        End Try
    End Sub
0
 

Author Comment

by:bfth1505
ID: 16894414
Wow, what a great answer.  I put it into my application and altered a few lines and it worked perfectly.  Thanks Don, that is a very professional answer!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now