Solved

classic asp to asp.net/vb.net

Posted on 2011-09-11
12
435 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

831 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