Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 42326

VS 2010 Passing parameter to another form...almost there

$
0
0
Many thanks to everyone who has helped in the last few days on this issue...

I have decided to take a different route. Instead of building a datatable in memory and trying to pass that from Form1 to Form2's datagrid, I am simply loading Form2 and executing my query on Form2 load. I am almost there...In the code below, Form2 loads and creates a datatable and binds that to the empty datagrid (I have formatted the columns and headers with Designer). Now, if I remove my WHERE clause, the datagrid loads correctly. Now the trick is to capture the invoice number entered into Form1 as a parameter and pass that to my SELECT statement's WHERE clause. No matter what I try, I just can't seem to use my parameter as written.

Any clues as to how I need to format my parameter? In all my other queries, I use the @ symbol and they work perfectly (I have also tried using a ? as the parameter's prefix, but to no avail...)
Code:

'Form2 Loads
Dim byinvnum As String = "SELECT * from billtime WHERE invnum=@myinvnum"

'All my connection specific declarations (conn) are held in a module and work fine
        Dim dtbyinv As New DataTable
        Dim dabyinv As New MySqlDataAdapter(byinvnum, conn)
        dabyinv.SelectCommand.Parameters.AddWithValue("@myinvnum", main1.rptinvnum.Text.Trim)

        Try
            conn.Open()
            dabyinv.Fill(dtbyinv)
            dg7byInvNum.DataSource = dtbyinv
            conn.Close()
            conn.Dispose()
            Exit Sub
        Catch ex As Exception
            MsgBox("Database Error" & vbCrLf & ex.Message, MsgBoxStyle.Critical)
            conn.Close()
            conn.Dispose()
            Exit Sub
        End Try

Many thanks for the fresh eyes!

Viewing all articles
Browse latest Browse all 42326