Solved

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

Posted on 2006-06-12
3
515 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Round a string to two digits 12 32
VB.Net - CSV to Oracle table 4 52
I need help embedding an image as HTML in my vb.net application 3 50
Advice in Xamarin 21 57
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

867 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

13 Experts available now in Live!

Get 1:1 Help Now