Solved

reuse CommandText

Posted on 2006-07-07
3
326 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

734 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