Solved

reuse CommandText

Posted on 2006-07-07
3
325 Views
Last Modified: 2012-06-21
I have the following code.  I try to reset CommandText SQL based on a condition.   If ct = 1 Then reset CommandText SQL .  But, I don't think it works.  Can I change the SQL inside CommandText like that?  Thanks.
 
     CMD.Connection = conn
        conn.Open()
        CMD.CommandText = "SELECT  count(*) ct FROM  XX where LEASE='" & Request.QueryString("LEASE") & "'"

        da.SelectCommand = CMD
        da.Fill(ds, "ContactRequests")

        Dim ct As Integer = ds.Tables(0).Rows(0)("ct")

        If ct = 1 Then
            CMD.CommandText = "SELECT objID FROM  XX where LEASE='" & Request.QueryString("LEASE") & "'"
            da.SelectCommand = CMD
            da.Fill(ds, "ContactRequests")
            Dim objID As String = ds.Tables(0).Rows(0)("objID").ToString()
end if
   
0
Comment
Question by:ewang1205
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 4

Accepted Solution

by:
gregg1ep00 earned 500 total points
ID: 17061062
There is a MUCH easier, much more efficient way to do this.  There is no need to use DataAdapters or DataSets if you're retrieving this tiny amount of data.

------------------------------
CMD.Connection = conn
CMD.CommandText = "SELECT count(*) ct FROM XX where LEASE='" & Request.QueryString("LEASE") & "'"

conn.Open()
Dim ct As Integer = CInt( CMD.ExecuteScalar() )

If ct = 1 Then
    CMD.CommandText = "SELECT objID FROM XX where LEASE='" & Request.QueryString("LEASE") & "'"
    Dim objID As String = CStr( CMD.ExecuteScalar() )
End If
------------------------------

Hope that helps!
Greg
0
 
LVL 4

Expert Comment

by:gregg1ep00
ID: 17061085
Oops, forgot to close the connection.  After the code above, add this line:

conn.Close()
0
 

Author Comment

by:ewang1205
ID: 17061157
gregg1ep00:  That is great.  Faster and simpler and working great!  Thanks.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

756 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