datagrid help

Smithbr

Member
Joined
Jun 3, 2003
Messages
15
I am trying to use a datagrid for the first time to display and eventually update and add data from a msAccess database. Here is the code I have so far and at the bottom I have the error message I am getting. If anyone knows why I am getting this based on the code, I could really use the help. I am still new to Vb.Net.


Code:
Public Class frminvoice
    Inherits System.Windows.Forms.Form
    Dim CmdInvoice As New OleDb.OleDbCommand
    Dim dacomm As New OleDb.OleDbDataAdapter
    Dim dscomm As New DataSet

    Private Sub frminvoice_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DbConnect.Open()
        dacomm.SelectCommand = CmdInvoice
    End Sub

    Private Sub cmdfind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdfind.Click
        With CmdInvoice
            .Connection = DbConnect
            .CommandType = CommandType.Text
            .CommandText = "SELECT InvoiceDate, InvoiceNumber, 
                     CustomerNumber, BrokerID, LineSEQNo, ItemNumber, 
                      ProductLine, QuantityShipped, LastUnitPrice, Total, 
                      COMM, Commissions, Terms, DiscountPercentage FROM 
                      Pending Commissions WHERE InvoiceNumber = " & txtinvnum.Text
        End With

        dscomm.Clear()

        dacomm.Fill(dscomm)

        dbgcomm.DataSource = dscomm.Tables(0)

    End Sub
When I go to run it, I get the error:
"An unhandled exception of type System.Data.OleDb.OleDbException occurred in system.data.dll " referring to the code "Dacomm.fill(dscomm)" located under the find button. I get this error when I type in the invoice number adn then click FIND. Any ideas on what is going on or what I need to change?
Thanks,
Brent
 
Last edited by a moderator:
Likely failure points:
1. DbConnect
2. SQL error

Try stepping thru the code one line at a time. If DbConnect is OK, copy and paste CommandText into MS Access and see if the query is valid.
 
Try putting the following around the entire code in cmdfind_Click

try
....
Catch ex As Exception
msgbox("Exception: " & ex.message)

finally
*** close down
end try
 
Smithbr,
The DataAdapter object opens and closes the connection as needed.
Code:
Dim dacomm as New OleDbDataAdapter(cmdInvoice, DbConnect)
Dont leave your connection hanging open, i.e. remove the DbConnect.Open from the form load event.

I assume you have a valid connection string defining the DbConnect object that you didnt show us since the form load event didnt choke on the .Open call...might be helpful to see the whole thing.


The number you type into a text box is stored as a string. You therefore are querying a data column that is probably formatted as an integer (or maybe a double) with a string value. Your db wont like that.

Convert.ToInt32(txtinvnum.Text) or whatever you need to convert it into to match the column data type in the Access table.


Jon
 
Back
Top