Solved

classic asp to asp.net/vb.net

Posted on 2011-09-11
12
425 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 30

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
 
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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 500 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video discusses moving either the default database or any database to a new volume.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now