?
Solved

classic asp to asp.net/vb.net

Posted on 2011-09-11
12
Medium Priority
?
457 Views
Last Modified: 2012-05-12
Trying to convert the attached codes from classic asp to asp.net/vb.net.
See any experts can help ....

Thanks
Function SingleItem(Category,RowNumber)

SQL=" SELECT * FROM ( "
SQL=SQL & " Select Description, PartNo, "
SQL=SQL & " Row_Number() Over(Order By PartNo) as rownumber, "
SQL=SQL & " Count(*) Over() as TotalRecordCount "
SQL=SQL & " From Products "
SQL=SQL & " where Category = '" & Category & "' ) T1 "
SQL=SQL & " WHERE rownumber = '" & RowNumber & "' "
SQL=SQL & " Order By partno asc "

Set dbsecurecon  = Server.CreateObject("ADODB.Connection")
dbsecurecon = "PROVIDER=SQLOLEDB;DATA SOURCE=1xxx;DATABASE=xxx;User ID=sa;Password=xxx"
Set RS = Server.CreateObject("ADODB.recordset")
RS.open SQL, dbsecurecon,3,3
If Not RS.EOF Then
PartNo = RS("PartNo")
End If
 SingleItem=PartNo
End Function

Open in new window

0
Comment
Question by:Webboy2008
  • 4
  • 4
  • 2
  • +2
12 Comments
 
LVL 31

Expert Comment

by:Wayne Barron
ID: 36519790
OK, I understand why you referred to .net in the other post.

Sorry, I am a Classic ASP, and have very little experience with .net coding.

Good Luck
Carrzkiss
0
 
LVL 7

Expert Comment

by:Kishan Zunjare
ID: 36521719
Header of ASp.NET code file you need to write
Imports System.Data.SqlClient


'VB.NET function returns string value.
Private Function SingleItem(ByVal Category As Integer, ByVal RowNumber As Integer) As String
            Dim strPartNo As String = String.Empty

            Dim myConnection As New SqlConnection
            Dim myCommand As SqlCommand
            Dim dr As SqlDataReader

            Try
                myConnection.ConnectionString = "PROVIDER=SQLOLEDB;DATA SOURCE=1xxx;DATABASE=xxx;User ID=sa;Password=xxx"
                myConnection.Open()
                'opening the connection
                Dim sSQLCmd As String = String.Empty
                sSQLCmd = "SELECT * FROM (Select Description, PartNo,Row_Number() Over(Order By PartNo) as rownumber, Count(*) Over() as TotalRecordCount  From Products " _
                & "where Category = '" & Category & "' ) T1 WHERE rownumber = '" & RowNumber & "' Order By partno asc"

                myCommand = New SqlCommand(sSQLCmd, myConnection)
                'executing the command and assigning it to connection
                dr = myCommand.ExecuteReader()

                While dr.Read()
                    strPartNo = CStr(dr("PartNo"))
                    'displaying the data from the table
                End While
                dr.Close()
                myConnection.Close()
            Catch e As Exception
            End Try
            Return strPartNo
        End Function


Hope this will work, let me know if you face any issue.
0
 

Author Comment

by:Webboy2008
ID: 36522959
thank. since, it only returns one item. can we avoid using while statement to reduce some loading time?
0
Technology Partners: 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!

 
LVL 7

Expert Comment

by:Kishan Zunjare
ID: 36524201
If it only returns only one value then While loop repeat for one time only; so you no need to worry about loading, actually datareader is light wait.

If you still want to avoid then go for .net data table.



0
 

Author Comment

by:Webboy2008
ID: 36524269
I don't want data table.  Can you just modify your codes? Thanks
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 36524349
>>thank. since, it only returns one item. can we avoid using while statement to reduce some loading time? <<
Then try and IF statement instead.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 36524386
>>Can you just modify your codes?<<
How difficult can it be, instead of:
While dr.Read()
      strPartNo = CStr(dr("PartNo"))
     'displaying the data from the table
End While

Use:
If dr.Read()
      strPartNo = CStr(dr("PartNo"))
     'displaying the data from the table
Else
      ' Oops no data.
End If
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 36524395
Let's try that again:
If dr.Read() Then
      strPartNo = CStr(dr("PartNo"))
     'displaying the data from the table
Else
      ' Oops no data.
End If
 
0
 
LVL 7

Expert Comment

by:Kishan Zunjare
ID: 36524624
Private Function SingleItem(ByVal Category As Integer, ByVal RowNumber As Integer) As String
            Dim strPartNo As String = String.Empty

            Dim myConnection As New SqlConnection
            Dim myCommand As SqlCommand
            Dim dr As SqlDataReader

            Try
                myConnection.ConnectionString = "PROVIDER=SQLOLEDB;DATA SOURCE=1xxx;DATABASE=xxx;User ID=sa;Password=xxx"
                myConnection.Open()
                'opening the connection
                Dim sSQLCmd As String = String.Empty
                sSQLCmd = "SELECT * FROM (Select Description, PartNo,Row_Number() Over(Order By PartNo) as rownumber, Count(*) Over() as TotalRecordCount  From Products " _
                & "where Category = '" & Category & "' ) T1 WHERE rownumber = '" & RowNumber & "' Order By partno asc"

                myCommand = New SqlCommand(sSQLCmd, myConnection)
                'executing the command and assigning it to connection
                dr = myCommand.ExecuteReader()

                 If Not IsDBNull(dr("PartNo") Then
                      strPartNo = CStr(dr("PartNo"))
                  End If

                'displaying the data from the table
                dr.Close()
                myConnection.Close()
            Catch e As Exception
            End Try
            Return strPartNo
        End Function

This will work.. do not forget to provide rating
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 36525062
You can also do:

If dr.HasRows Then
      dr.Read()
      strPartNo = CStr(dr("PartNo"))
     'displaying the data from the table
Else
      ' Oops no data.
End If
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 total points
ID: 36526344
How about using ExecuteScalar() function if its only one string to return



Private Function SingleItem(ByVal Category As Integer, ByVal RowNumber As Integer) As String
            Dim strPartNo As String = String.Empty

            Dim myConnection As New SqlConnection
            Dim myCommand As SqlCommand
            Try
                myConnection.ConnectionString = "PROVIDER=SQLOLEDB;DATA SOURCE=1xxx;DATABASE=xxx;User ID=sa;Password=xxx"
                myConnection.Open()
                'opening the connection
                Dim sSQLCmd As String = String.Empty
                sSQLCmd = "SELECT IsNull(PartNo, '') FROM (Select Description, PartNo,Row_Number() Over(Order By PartNo) as rownumber, Count(*) Over() as TotalRecordCount  From Products " _
                & "where Category = '" & Category & "' ) T1 WHERE rownumber = '" & RowNumber & "' Order By partno asc"

                myCommand = New SqlCommand(sSQLCmd, myConnection)
                'executing the command and assigning it to connection 
                strPartNo = myCommand.ExecuteScalar()

                myConnection.Close()
            Catch e As Exception
            End Try
            Return strPartNo
        End Function

Open in new window

0
 
LVL 7

Expert Comment

by:Kishan Zunjare
ID: 36527365
ExecuteScalar will also work.

ExecuteScalar() in SqlCommand Object is used for get a single value from Database after its execution. It executes the query, and returns the first column of the first row in the result set returned by the query.
It returns the first field in the first row. This is very light-weight and is perfect when all your query asks for is one item.
This would be excellent for receiving a count of records in an sql statement, or for any query where only one specific field in one column is needed.

ExecuteNonQuery
ExecuteNonQuery() is used when you do not need to return any values back, it is most often used with INSERT UPDATE DELETE queries.
 These changes can be of to create table using executeNonQuery or to insert, update, delete Records in Table. These type of operations are know as catalog operations.

ExecuteReader
ExecuteReader method is used for sequential Data Reading from Database. while datareader is in use, the connection object related to DataReader is busy. we cannot use the connection for other operations. We can only use that connection object until we close either the connection or datareader.

http://dng-ado.blogspot.com/2007/05/what-is-difference-between.html

I hope this will be useful.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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.…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Integration Management Part 2
Suggested Courses

807 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