Wrox Home  
Search P2P Archive for: Go

  Return to Index  

ado_dotnet thread: paramater query problem and more


Message #1 by "mike" <MIKEZCG@Y...> on Sat, 28 Dec 2002 17:55:24
question # 1
is it ok to use vbnullsting here ? if not why?
If Not dr.IsDBNull(intVendCf) Then
  sVendCFNum = dr.GetString(intVendCf)
 Else
sVendCFNum = vbNullString '
End If
cboVendNameNum.Items.Add(String.Format("{0}  {1}", sVendName, sVendCFNum))
Loop
 cboVendNameNum.SelectedIndex = 0

question # 2
I want a func that returns an open cn my ? is about disposing I know this 
doesnt work but if i dont dispose in the func will i be using resources?
 Public Function GetOpenCn() As OleDbConnection
        Try
            mkCn = New OleDbConnection(Constants.sCnCF)
            mkCn.Open()
            GetOpenCn = mkCn

        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            If Not GetOpenCn Is Nothing Then
                GetOpenCn.Dispose() ' SHOULD THIS BE HERE 
            End If
        End Try

    End Function


question # 3
'I have a btnDatareader HOW DO I RUN the click event for that buttn from 
another area in a diff sub? btnDataReader_Click() ? 


question # 4
what is wrong with this sql? It returns a ton of stuff instead of the 2 
items i know it should ?  
 Dim sSqlP As String 
        sSqlP = "SELECT NAME, Amount "
        sSqlP += " FROM Vouchers"
        sSqlP += " WHERE CKDate Is Null "
        sSqlP += " And amount > 0 "
        sSqlP += " And name  like @Vendor + '%'"
        sSqlP += " ORDER BY amount Desc"

 cboOpenAp.Items.Clear()
            cnn = New SqlConnection(Constants.sSqlCnCF)
            cnn.Open()
            cmd = New SqlCommand(sSqlP, cnn)
            prm = New SqlParameter("@vendor", cboVendNameNum.SelectedText)
            cmd.Parameters.Add(prm)
            drSql = cmd.ExecuteReader
            Dim oArray(drSql.FieldCount - 1) As Object
            MsgBox(sSqlP)
            While drSql.Read
                drSql.GetValues(oArray)
                cboOpenAp.Items.Add(String.Format("{0} {1} ", oArray(0), 
oArray(1)))
                cboOpenAp.SelectedIndex = 0

            End While
Message #2 by Richard Ainsley <rainsley@p...> on Sat, 28 Dec 2002 20:16:03 -0800
#1 - It should work - provided sVendCFNum has been dim'd as a string  -- do
NOT use a variant --  You may also need to getInteger(..).tostring if the
field is an integer type. The Getxxx functions must match the data types of
the fields as they are not conversion functions!

#2 - It is better to set the object to null and let the Garbage Disposer to
its thing.  You MUST guarantee that the connection is CLOSED however.
When you force the garbage collection, you are just wasting time usually.
Connections are pooled by the dot net framework - it is highly likely that
you are really wasting time if you force a connection object to be recycled
rather than being re-used!

#3 - see below

I hope this can help you some. Good luck

-----Original Message-----
From: mike [mailto:MIKEZCG@Y...]
Sent: Saturday, December 28, 2002 5:55 PM
To: ADO.NET
Subject: [ado_dotnet] paramater query problem and more

question # 1
is it ok to use vbnullsting here ? if not why?
If Not dr.IsDBNull(intVendCf) Then
  sVendCFNum = dr.GetString(intVendCf)
 Else
sVendCFNum = vbNullString '
End If
cboVendNameNum.Items.Add(String.Format("{0}  {1}", sVendName, sVendCFNum))
Loop
 cboVendNameNum.SelectedIndex = 0

question # 2
I want a func that returns an open cn my ? is about disposing I know this
doesnt work but if i dont dispose in the func will i be using resources?
 Public Function GetOpenCn() As OleDbConnection
        Try
            mkCn = New OleDbConnection(Constants.sCnCF)
            mkCn.Open()
            GetOpenCn = mkCn

        Catch ex As Exception
            MsgBox(ex.Message)
        Finally

'''''''''''' Finally blocks are guaranteed to run if an error occurred or
not!!!!!
'''''''''''''''''''''''''''''''''''''''''''''''' Do you really want to
dispose an OPEN connection object here?????????????

            If Not GetOpenCn Is Nothing Then
                GetOpenCn.Dispose() ' SHOULD THIS BE HERE   =>NO!
            End If
        End Try

    End Function


question # 3
'I have a btnDatareader HOW DO I RUN the click event for that buttn from
another area in a diff sub? btnDataReader_Click() ?
This is not as simple as before - you need to match the expected parameter
list!  -- or move the logic inside the btnDataReader click event handler to
a separate subroutine that has perhaps no parameters.




question # 4
what is wrong with this sql? It returns a ton of stuff instead of the 2
items i know it should ?
Since you are hard coding the SQL script, why not hard code the
cboVendNamrNum.SelectedText????
It seems likely that you have a data problem.  Use Query Analyzer, Access,
or the Query Builder in the Visual Studio dot net (least desirable as you
are stuck building a stored procedure you then need to remove) - build the
query and test it.  Your filter logic looks reasonable, but you need to play
with the result set to see what is really going on.


 Dim sSqlP As String
        sSqlP = "SELECT NAME, Amount "
        sSqlP += " FROM Vouchers"
        sSqlP += " WHERE CKDate Is Null "
        sSqlP += " And amount > 0 "
        sSqlP += " And name  like @Vendor + '%'"
        sSqlP += " ORDER BY amount Desc"

 cboOpenAp.Items.Clear()
            cnn = New SqlConnection(Constants.sSqlCnCF)
            cnn.Open()
            cmd = New SqlCommand(sSqlP, cnn)
            prm = New SqlParameter("@vendor", cboVendNameNum.SelectedText)
            cmd.Parameters.Add(prm)
            drSql = cmd.ExecuteReader
            Dim oArray(drSql.FieldCount - 1) As Object
            MsgBox(sSqlP)
            While drSql.Read
                drSql.GetValues(oArray)
                cboOpenAp.Items.Add(String.Format("{0} {1} ", oArray(0),
oArray(1)))
                cboOpenAp.SelectedIndex = 0

            End While
===
Fast Track ADO.NET with C# is a concise introduction to the concepts,
techniques, and libraries that you will need in order to start using ADO.NET
in your applications. The book covers DataSets and Typed DataSets, accessing
data using DataReaders and DataAdaptors, the close relationship between
ADO.NET and XML, how and where to use ADO.NET in your enterprise
applications, and how to use Web Services and ADO.NET to easily pass data
between applications.
http://www.wrox.com/books/1861007604.htm


  Return to Index