Copy records to another recordset using Active Server Page - asp-classic

I'm programming in Classic ASP. I'm trying to do the paging. My backend is SQL CE 3.5. Unfortunetly, it doesn't support paging in SQL Query (Like row_number() in sql server).
So I go with ASP Paging. But when i ask to the recordset, give me the first 10 records by setting the rs.PageSize and rs.AbsolutePage and all, it gives me all records. So I planned to copy only first 10 rows from the resultant recordset to another new recordset. So I coded like below:
Set rsTemp = CopyRecordsetStructure(objRs)
rsTemp.Open
iRecordsShown = 0
Set objFields = objRs.Fields
intFieldsCount = objFields.Count-1
Do While iRecordsShown < intPageSize And Not objRs.EOF
rsTemp.AddNew
For Idx = 0 To intFieldsCount
rsTemp.Fields(Idx).Value = objRs.Fields(Idx).Value
Next
rsTemp.Update
iRecordsShown = iRecordsShown + 1
objRs.MoveNext
Loop
Public Function CopyRecordsetStructure(ByVal rs)
Dim rsTemp
Set rsTemp = CreateObject("ADODB.Recordset")
Set objFields = rsTemp.Fields
intFieldCount = objFields.Count - 1
For Idx = 0 To intFieldCount
rsTemp.Fields.Append objFields(Idx).Name, _
objFields(Idx).Type, _
objFields(Idx).DefinedSize
Next
Set CopyRecordsetStructure = rsTemp
End Function
The issue is i could not open the" rsTemp". It throws me error
The connection cannot be used to perform this operation. It is either closed or invalid in this context.
If I use some dummy query and connection it doesn't work.
Please help to copy the records from one recordset to another new record set.
Thanks in advance
Ganesh.

Not sure, but this looks wrong
Set objFields = rsTemp.Fields
Shouldn't it be
Set objFields = rs.Fields

With the comments and fixed in the above comments, the function should be updated Set objFields = rs.Fields to:
Usage:
Dim rsTemp
Set rsTemp = CopyRecordset(rsPadicon)
Update Code
Public Function CopyRecordset(rs)
Dim rsTemp, objFields, intFieldsCount, intPageSize
Set rsTemp = CopyRecordsetStructure(rs)
rsTemp.Open
Set objFields = rs.Fields
intFieldsCount = objFields.Count-1
response.write("<li> rs.RecordCount :" & rs.RecordCount & "</li>")
' response.write("<li> intFieldsCount :" & intFieldsCount & "</li>")
rs.MoveFirst
Do While Not rs.EOF
rsTemp.AddNew
Dim i
For i = 0 to intFieldsCount 'use i as a counter
' response.write("<li> Name :" & rs.Fields(i).Name & "</li>")
' response.write("<li> Value :" & rs.Fields(i).Value & "</li>")
if Not IsNull(rs.Fields(i).Value) then
rsTemp.Fields(i).Value = rs.Fields(i).Value
End if
Next
rsTemp.Update
rs.MoveNext
Loop
Set CopyRecordset = rsTemp
End Function
Public Function CopyRecordsetStructure(ByVal rs)
Dim rsTemp, objFields, intFieldCount, Idx
Set rsTemp = CreateObject("ADODB.Recordset")
Set objFields = rs.Fields
intFieldCount = objFields.Count - 1
For Idx = 0 To intFieldCount
rsTemp.Fields.Append objFields(Idx).Name, _
objFields(Idx).Type, _
objFields(Idx).DefinedSize
Next
Set CopyRecordsetStructure = rsTemp
End Function

Related

SQL Server procedure has too many arguments specified

I get the error
Procedure or function has to many arguments specified
but only sometimes. It's a REST API and cellphones call this function to validate the user. Today it's onli 1-2 cellphones and they make the call every second minute.
It works well most times but sometimes I get this error code.
To see what's wrong, I've made a small loop that gathers the parameters into a string. And sometimes they are two #GUID and #Datum and sometimes repeat themselves.
Here is the VB code:
Public Shared Sub validatePhone(secretKey As String, ByRef _RS As phoneData, Fnr As Integer)
Dim connStr As String = System.Configuration.ConfigurationManager.AppSettings("ConnStringSQL")
Dim Conn As SqlConnection = New System.Data.SqlClient.SqlConnection(connStr)
Dim _theString As New System.Text.StringBuilder
Try
_RS.message = ""
_RS.status = True
Dim Status As Integer = 0
Dim _GUID As Guid = New Guid(secretKey)
Try
Conn.Open()
Catch ex As Exception
End Try
cmd.Connection = Conn
Apt.SelectCommand = cmd
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "API2017_checkPhone"
cmd.Parameters.Clear()
cmd.Parameters.Add("#GUID", SqlDbType.UniqueIdentifier).Value = _GUID
cmd.Parameters.Add("#Datum", SqlDbType.SmallDateTime).Value = Now
For Each item As SqlParameter In cmd.Parameters
_theString.Append(item.ParameterName & ";")
Next
Apt.Fill(ds, "DataCheckPhone")
If ds.Tables("DataCheckPhone").Rows.Count > 0 Then
With ds.Tables("DataCheckPhone").Rows(0)
Status = .Item("spStatus")
If Status = 0 Then
_RS.Namn = .Item("Namn")
_RS.SalesID = .Item("SalesID")
_RS.Anlaggning = .Item("Anlaggning")
_RS.Anlnr = .Item("Anlnr")
Funktions.URLEncodeStr(_RS.Namn)
Funktions.URLEncodeStr(_RS.Anlaggning)
End If
End With
Else
Dim _Lnum As Integer
Funktions.Logg(Fnr & ": " & "Fatal error", 999, _Lnum, 0)
_RS.message = "Error 999:" & _Lnum.ToString
Return
End If
Catch ex As Exception
_RS.status = False
_RS.Anlaggning = Nothing
_RS.Anlnr = 0
_RS.Namn = Nothing
_RS.SalesID = Nothing
Dim _Lnum As Integer
Funktions.Logg(Fnr & ": " & ex.Message & "{" & _theString.ToString & "}", 999, _Lnum, 0)
_RS.message = "Error 999:" & _Lnum.ToString
Finally
ds.Tables.Clear()
Try
ds.Tables("DataCheckPhone").Dispose()
Catch ex As Exception
End Try
End Try
Try
Conn.Close()
Conn.Dispose()
Catch ex As Exception
End Try
End Sub
And here is the stored procedure:
ALTER PROCEDURE [dbo].[API2017_checkPhone]
#GUID as uniqueidentifier,
#Datum as smalldatetime
AS
DECLARE #Status AS INT
DECLARE #Anlaggning AS NVARCHAR(50)
DECLARE #Namn AS NVARCHAR(50)
DECLARE #Anlnr AS INT
DECLARE #SalesID AS uniqueidentifier
DECLARE #LockedSalesman AS BIT
DECLARE #LockedAnlaggning AS BIT
SET #Status = 0
IF EXISTS (SELECT * FROM KK2017_connectedPhones WHERE [guid] = #guid)
BEGIN
SET #status = 0
SET #salesID = (SELECT salesID FROM KK2017_connectedPhones
WHERE [guid] = #guid)
SET #Anlnr = (SELECT anlnr FROM KK2017_Säljare WHERE salesID = #salesID)
SET #Namn = (SELECT Namn FROM KK2017_Säljare WHERE salesID = #salesID)
SET #Anlaggning = (SELECT namn FROM KK2017_Anlaggning WHERE anlnr = #Anlnr)
SET #LockedSalesman = (SELECT locked FROM KK2017_Säljare WHERE salesID = #salesID)
UPDATE KK2017_Säljare
SET inloggad = #Datum
WHERE salesID = #SalesID
IF #LockedSalesman = 1
BEGIN
SET #Status = 2
END
SET #LockedAnlaggning = (SELECT locked FROM KK2017_Anlaggning
WHERE AnlNr = #Anlnr)
IF #LockedAnlaggning = 1
BEGIN
SET #status = 3
END
END
ELSE
SET #status = 1
SELECT
#Status AS spStatus,
#Anlaggning AS Anlaggning,
#anlnr AS anlnr,
#Namn AS namn,
#SalesID AS salesID
I must have done something wrong but can not see it.
If anyone has a proposal, I would be grateful.
/Claes
You have a threading issue because you have defined cmd as shared, which means there is only one instance servicing all of the incoming requests. Sometimes two threads are in the process of adding parameters at the same time, and both threads end up with four, hence the error.
The standard pattern here would be declare cmd as a local variable, and instantiate a new instance each time the function is called. The overhead to this is negligible.
Also, avoid shared variables in multithreaded applications (such as web services). There is only one copy of the variable for all users and all threads, and 99% of the time that is not what you actually want. For the other 1% usually you'd use application variables or HttpCache. In this case you should use a local variable.

Index was out of range. Must be non-negative and less than the size of the collection - chart databind

I'm trying to build a datatable and then bind it to a gridview and chart object. The gridview appears fine but when I build the x,y array and bind them to the chart object I get the error above.
I've read many forums to understand that the index is looking at a field that is out of range but I've queried in the immediate window all the fields and the are clearly values there. There error occurs as soon as I bind it to the chart object and I don't know why the chart object doesn't just display properly.
This is my aspx codebehind:
Dim dt As DataTable = New DataTable()
dt.Columns.Add("CompanyName")
dt.Columns.Add("Amount")
dt.Columns.Add("Proportion")
Using DBContext As New fundmatrixEntities
Dim queryUnits = (From c In DBContext.Units Where c.SavingApplicationId = savAppId Select New With {.LoanAppId = c.LoanApplicationId}).Distinct().ToList()
Dim companyName As String = ""
Dim savingsAmount As String = ""
Dim totalProportion As String = ""
Dim totalSavingsAmount As String = ""
For Each loan In queryUnits
If Not loan.LoanAppId Is Nothing Then
companyName = classSQLDirect.ExecQuery("SELECT companyname FROM companyprofile where loanapplicationid='" & loan.LoanAppId.ToString & "'")
savingsAmount = classSQLDirect.ExecQuery("SELECT SUM(Amount) from unit where SavingApplicationId='" & savAppId.ToString & "' and LoanApplicationId='" & loan.LoanAppId.ToString & "'")
totalSavingsAmount = classSQLDirect.ExecQuery("SELECT amount FROM SavingApplication WHERE SavingApplicationId='" & savAppId.ToString & "'")
totalProportion = ((savingsAmount / totalSavingsAmount) * 100) & "%"
dt.Rows.Add(companyName, savingsAmount, totalProportion)
End If
Next
gvwBusinesses.DataSource = dt
gvwBusinesses.DataBind()
End Using
Dim x As String() = New String(dt.Rows.Count - 1) {}
Dim y As String() = New String(dt.Rows.Count - 1) {}
For i As Integer = 0 To dt.Rows.Count - 1
x(i) = dt.Rows(i)(0).ToString()
y(i) = dt.Rows(i)(2).ToString()
Next
chart1.Series(0).Points.DataBindXY(x, y)
chart1.Series(0).ChartType = SeriesChartType.Pie
The code errors on the line
chart1.Series(0).Points.DataBindXY(x, y)

Having a BOF or EOF error, after call recordCount

I have to call recordCount function to get the count of recordset.
But once I call recordCount function, the recordset is out of control.
...
Dim objRootDSE, strDNSDomain, adoCommand, adoConnection
Set adoCommand = CreateObject("ADODB.Command")
'Set adoRecordset = adoCommand.Execute
Set adoRecordset = Server.CreateObject ("ADODB.Recordset")
adoRecordset.cursorType = 3
adoRecordset.CursorLocation = adUseClient
adoRecordset = adoCommand.Execute
...
totalcnt = adoRecordset.recordCount
If totalcnt > 0 Then
...
Do until adoRecordset.EOF
' Retrieve values... But it fails because it seems adoRecordset is in EOF
...
So I use movefirst and try to retrieve values.
If adoRecordset.recordCount > 0 Then
adoRecordset.movefirst
...
But it occurs an error(below is translated by google)
ADODB.Recordset 오류 '800a0bcd'
BOF or EOF is True, or the current record has been deleted. Requested operation requires a current record.
If I didn't call recordCount, there's no problem. But I should know the count of record.
The whole code is :
<%
'On Error Resume next
Dim objRootDSE, strDNSDomain, adoCommand, adoConnection
Dim strBase, strFilter, strAttributes, strQuery, adoRecordset
Dim strDN, strUser, strPassword, objNS, strServer
Dim name,company,physicalDeliveryOfficeName
Const ADS_SECURE_AUTHENTICATION = 1
Const ADS_SERVER_BIND = 0
' Specify a server (Domain Controller).
strServer = "my_ad_server_domain"
' Specify or prompt for credentials.
strUser = "my_account"
strPassword = "my_passwrd"
' Determine DNS domain name. Use server binding and alternate
' credentials. The value of strDNSDomain can also be hard coded.
Set objNS = GetObject("LDAP:")
Set objRootDSE = objNS.OpenDSObject("LDAP://" & strServer & "/RootDSE", _
strUser, strPassword, _
ADS_SERVER_BIND Or ADS_SECURE_AUTHENTICATION)
strDNSDomain = objRootDSE.Get("defaultNamingContext")
' Use ADO to search Active Directory.
' Use alternate credentials.
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Properties("User ID") = strUser
adoConnection.Properties("Password") = strPassword
adoConnection.Properties("Encrypt Password") = True
adoConnection.Properties("ADSI Flag") = ADS_SERVER_BIND _
Or ADS_SECURE_AUTHENTICATION
adoConnection.Open "Active Directory Provider"
Set adoCommand.ActiveConnection = adoConnection
' Search entire domain. Use server binding.
strBase = "<LDAP://" & strServer & "/" & strDNSDomain & ">"
' Search for all users.
strFilter = "(&(objectCategory=user)(ExADObjectStatus=10)(samaccountname=*"&"my_search_value"&"*))"
' Comma delimited list of attribute values to retrieve.
strAttributes = "name,company,physicalDeliveryOfficeName"
' Construct the LDAP query.
strQuery = strBase & ";" & strFilter & ";" _
& strAttributes & ";subtree"
' Run the query.
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 60
adoCommand.Properties("Cache Results") = False
Set adoRecordset = adoCommand.Execute
if not adoRecordset.EOF then
totalcnt = adoRecordset.recordCount
If totalcnt > 0 Then
Response.write 111
Do until adoRecordset.EOF
name = adoRecordset.Fields("name").Value
company = adoRecordset.Fields("company").Value
physicalDeliveryOfficeName = adoRecordset.Fields("physicalDeliveryOfficeName").Value
Response.Write name & "<br/>"
Response.Write company & "<br/>"
Response.Write physicalDeliveryOfficeName
adoRecordset.MoveNext
Loop
end if
end if
' Clean up.
adoRecordset.Close
adoConnection.Close
%>
It shows only one result of record.
You can try to face the problem from a different angle. Instead of trying to fix the internal recordCount property (which you can't) simply count the records yourself:
totalcnt = 0
Do until adoRecordset.EOF
totalcnt = totalcnt + 1
adoRecordset.MoveNext
Loop
If totalcnt>0 Then
adoRecordset.MoveFirst
Do until adoRecordset.EOF
name = adoRecordset.Fields("name").Value
'...
adoRecordset.MoveNext
Loop
End If
Update: Looks like in that specific case, the MoveFirst just fails, maybe because it's LDAP and not ordinary query from a database. To bust this once and for all, you can populate your own collection when iterating the records then use that collection as much as you like:
Dim oData, oField, tempArray
Set oData = Server.CreateObject("Scripting.Dictionary")
totalcnt = 0
For Each oField In adoRecordset.Fields
oData.Add oField.Name, Array()
Next
Do until adoRecordset.EOF
For Each oField In adoRecordset.Fields
tempArray = oData(oField.Name)
ReDim Preserve tempArray(UBound(tempArray) + 1)
tempArray(UBound(tempArray)) = oField.Value
oData(oField.Name) = tempArray
Next
totalcnt = totalcnt + 1
adoRecordset.MoveNext
Loop
adoRecordset.Close
Dim x
If totalcnt>0 Then
Response.Write("found total of " & totalcnt & " records<br />")
For x=0 To totalcnt-1
name = oData("name")(x)
company = oData("company")(x)
physicalDeliveryOfficeName = oData("physicalDeliveryOfficeName")(x)
Response.Write name & "<br/>"
Response.Write company & "<br/>"
Response.Write physicalDeliveryOfficeName
Next
End If
As the error indicates, the recordCount fails if you have no records in the recordset.
You can test for this before your code block. Try this:
if not adoRecordset.EOF then
totalcnt = adoRecordset.recordCount
If totalcnt > 0 Then
...
Do while not adoRecordset.EOF
...
Loop
end if
end if
edit: Corrected the loop to test for not adoRecordset.eof

Microsoft VBScript runtime error '800a0009'

I recently inherited a website in ASP, which I am not familiar with. Yesterday, one of the pages began to throw an error:
Microsoft VBScript runtime error '800a0009'
Subscript out of range: 'i'
default.asp, line 19
Here is the code from lines 13-27:
<%
set rs = Server.CreateObject("ADODB.Recordset")
rs.open "SELECT * FROM VENDORS_LIST_TBL WHERE inStr('"& dVendorStr &"','|'&ID&'|')", Cn
DIM dTitle(100), dDescription(100), dLink(100)
i = 0 : Do while NOT rs.EOF : i = i + 1
dTitle(i) = rs.fields.item("dTitle").value
dDescription(i) = rs.fields.item("dDescription").value
dLink(i) = rs.fields.item("dLink").value : if dLink(i) <> "" then dTitle(i) = "" & dTitle(i) & ""
if NOT rs.EOF then rs.movenext
Loop
x = i
rs.Close : Set rs = Nothing
%>
Any ideas on what's going on here and how I can fix it?
Thank you!
You've declared dTitle, dDescription and dLink as Arrays with a size of 100. As you are walking through the recordset, you are assigning elements to those arrays. It would appear that you have more than 100 records in your recordset, so the logic is trying to do something like:
dTitle(101) = rs.fields.item("dTitle").value
This will throw an error because your array isn't big enough to hold all of your data.
The "solution" you chose is not very good. What if within 2 years there will be more than 500? You will forget all about this and waste hours yet again.
Instead of fixed size arrays you can just use dynamic arrays:
DIM dTitle(), dDescription(), dLink()
ReDim dTitle(0)
ReDim dDescription(0)
ReDim dLink(0)
i = 0
Do while NOT rs.EOF
i = i + 1
ReDim Preserve dTitle(i)
ReDim Preserve dDescription(i)
ReDim Preserve dLink(i)
dTitle(i) = rs.fields.item("dTitle").value
dDescription(i) = rs.fields.item("dDescription").value
dLink(i) = rs.fields.item("dLink").value
If (Not(IsNull(dLink(i)))) And (dLink(i) <> "") Then
dTitle(i) = "" & dTitle(i) & ""
End If
rs.movenext
Loop
This will start with one (empty) item in each array - for some reason the code seems to need this - then on each iteration one more item will be added, preserving the others.
Note that I've also fixed small issue that might have caused trouble - in case of NULL value in "dLink" field, you would get blank anchors in your HTML because NULL is not empty string in VBScript.
This how GetRows can be used to achieve the same goal.
<%
Function VendorSearch(sVendor)
Dim cn: Set cn = SomeLibraryFunctionThatOpensAConnection()
Dim cmd: Set cmd = Server.CreateObject("ADODB.Command")
cmd.CommandType = adCmdText
cmd.CommandText = "SELECT dTitle, dDescription, dLink FROM VENDORS_LIST_TBL WHERE inStr(?,'|'&ID&'|')"
cmd.Parameters.Append cmd.CreateParameter("Vendor", adVarChar, adParamInput, Len(sVendor), sVendor)
Set cmd.ActiveConnection = cn
Dim rs : Set rs = cmd.Execute()
VendorSearch = rs.GetRows()
rs.Close()
cn.Close()
End Function
Dim arrVendor : arrVendor = VendorSearch(dVendorStr)
Const cTitle = 0, cDesc = 1, cLink = 2
Dim i
For i = 0 To UBound(arrVendor, 2)
If IsNull(arrVendor(cLink, i) Or arrVendor(cLink, i) = "" Then
arrVendor(cTitle, i) = "" & arr(cTitle, i) & ""
End If
Next
%>
Notes:
The Select statement contains only those fields required in the results, the use of * should be avoided
A parameterised command is used to avoid SQL Injection threat from SQL contactenation.
Constants used for field indices into the resulting 2 dimensional array.
Whilst this code replicates the original munging of the title value this is here as an example only. In reality construction of HTML should be left as late as possible and outputing of all such strings as title and description should be passed through Server.HTMLEncode before sending to the response.

Why no data is appearing?

I am developing an ASPX web page to return results from two SQL stored procs via VB. When I execute these two stored procs, they both return valid data. But for some reason, when I run this report it doesn't return any errors, however, it doesn't return any records/data either!
Also, I can't debug it for some reason, although I can set breakpoints! This is using VS 2008, but I think reason I can't debug is I believe this is a limited version that just works for SSRS and SSIS.
Here is an excerpt of my code:
<HTML>
<SCRIPT LANGUAGE="VB" RUNAT="Server">
Sub Page_Load(Sender as Object, E as EventArgs)
If Not IsPostback Then
Dim TheMonthDate As Date = DateAdd(DateInterval.Month, -1, Today)
calStartDate.SelectedDate = CDate((TheMonthDate.Month) & "/1/" & Year(TheMonthDate)).ToString("MM/dd/yyyy")
calEndDate.SelectedDate = GlobalFunctions.GlobalF.MonthLastDate(CDate((TheMonthDate.Month) & "/1/" & Year(TheMonthDate)).ToString("MM/dd/yyyy"))
Dim arrLevel as New ArrayList()
arrLevel.Add("All")
arrLevel.Add("Inquiries")
arrLevel.Add("All Complaints")
arrLevel.Add("Elevated Complaints")
arrLevel.Add("Non-Elevated Complaints")
dLevel.DataSource = arrLevel
dLevel.DataBind()
dLevel.selectedvalue = "All Complaints"
End If
Main
End Sub
Sub Main()
'------------------------- Query database and get arrays for the chart and bind query results to datagrid ----------------------------------------
Dim FirstMonthDate as date = calStartDate.SelectedDate
Dim LastMonthDate as date = calEndDate.SelectedDate
Dim TheLevel As Integer
Dim TitleLevel as String
Select Case dLevel.SelectedValue
Case "All"
TheLevel = 5
TitleLevel = "Inquiries and Complaints"
Case "Inquiries"
TheLevel = 0
TitleLevel = "Inquiries"
Case "All Complaints"
TheLevel = 3
TitleLevel = "All Complaints"
Case "Elevated Complaints"
TheLevel = 2
TitleLevel = "Elevated Complaints"
Case "Non-Elevated Complaints"
TheLevel = 1
TitleLevel = "Non-Elevated Complaints"
End Select
Dim DSPageData as new System.Data.DataSet
DSPageData = GlobalFunctions.GlobalF.GetComplaintTrending2(FirstMonthDate, LastMonthDate, TheLevel)
...
Dim DSDetails As New System.Data.DataSet
DSDetails = GlobalFunctions.GlobalF.GetComplaintTrendingDetails2(FirstMonthDate, LastMonthDate, TheLevel)
dgTable.DataSource = DSDetails
dgTable.DataBind()
Where in my Global.vb file I have:
'Added by Ryan on 4/17/11
Public Shared Function GetComplaintTrending2(ByVal FirstMonth As DateTime, ByVal LastMonth As DateTime, ByVal rowLevel As Integer) As DataSet
Dim DSPageData As New System.Data.DataSet
Dim param(2) As SqlClient.SqlParameter
param(0) = New SqlParameter("#FirstMonthDate", SqlDbType.DateTime)
param(0).Value = FirstMonth
param(1) = New SqlParameter("#LastMonthDate", SqlDbType.DateTime)
param(1).Value = LastMonth
param(2) = New SqlParameter("#TheLevel", SqlDbType.Int)
param(2).Value = rowLevel
''# A Using block will ensure the .Dispose() method is called for these variables, even if an exception is thrown
''# This is IMPORTANT - not disposing your connections properly can result in an unrespsonsive database
Using conn As New SQLConnection(ConfigurationSettings.AppSettings("AMDMetricsDevConnectionString")), _
cmd As New SQLCommand("ComplaintTrending2", conn), _
da As New SQLDataAdapter(cmd)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddRange(param)
da.Fill(DSPageData)
End Using
Return DSPageData
End Function
'Added by Ryan on 4/17/11
Public Shared Function GetComplaintTrendingDetails2(ByVal FirstMonth As DateTime, ByVal LastMonth As DateTime, ByVal rowLevel As Integer) As DataSet
Dim DSPageData As New System.Data.DataSet
Dim param(2) As SqlClient.SqlParameter
param(0) = New SqlParameter("#FirstMonthDate", SqlDbType.DateTime)
param(0).Value = FirstMonth
param(1) = New SqlParameter("#LastMonthDate", SqlDbType.DateTime)
param(1).Value = LastMonth
param(2) = New SqlParameter("#TheLevel", SqlDbType.Int)
param(2).Value = rowLevel
''# A Using block will ensure the .Dispose() method is called for these variables, even if an exception is thrown
''# This is IMPORTANT - not disposing your connections properly can result in an unrespsonsive database
Using conn As New SQLConnection(ConfigurationSettings.AppSettings("AMDMetricsDevConnectionString")), _
cmd As New SQLCommand("ComplaintTrendingDetails2", conn), _
da As New SQLDataAdapter(cmd)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddRange(param)
da.Fill(DSPageData)
End Using
Return DSPageData
End Function
And these stored procs are defined as:
CREATE PROCEDURE [dbo].[ComplaintTrendingDetails2]
--DECLARE
#FirstMonthDate DATETIME,
#LastMonthDate DATETIME,
#TheLevel INT
AS
SET NOCOUNT ON;
--ComplaintTrendingDetails2 '2/1/11', '2/28/11 23:59:59', 2
--SET #FirstMonthDate = '2/1/11'
--SET #LastMonthDate = '2/28/11 23:59:59'
--SET #TheLevel = '2'
SELECT DISTINCT
A.QXP_EXCEPTION_NO, A.[LEVEL], A.pRE,
A.QXP_REPORT_DATE, A.CLOSE_DATE, A.EPA_PRD_NAME,
A.EPA_PRD_CODE, A.EPL_LOT_NUMBER,
A.QXP_SHORT_DESC, A.QXP_DESCRIPTION,
A.QXP_RESOLUTION_DESC, A.CXP_CLIENT_NAME, A.Country,
C.PRODUCT, C.PRODUCT_GROUP, C.PRODUCT_ORG_UNIT,
B.DOC_DOCUMENT_NO, A.TICKET_NUM, A.CENTER_NUM,
A.COUNTRY_CODE, A.QXP_ID, B.IRF_QEI_ID
FROM ALL_COMPLAINTS A LEFT OUTER JOIN
SMARTSOLVE.V_QXP_ISSUE_REF B ON A.QXP_ID = B.IRF_QXP_ID LEFT OUTER JOIN
MANUAL.PRODUCTS C ON A.EPA_PRD_CODE = C.LIST_NUMBER
LEFT OUTER JOIN SMARTSOLVE.V_CXP_CUSTOMER_PXP D ON A.QXP_ID = D.QXP_ID
WHERE A.QXP_REPORT_DATE >= #FirstMonthDate AND A.QXP_REPORT_DATE <= #LastMonthDate
AND (A.QXP_SHORT_DESC <> 'Design Control') AND LEVEL = #TheLevel
AND (D.QXP_EXCEPTION_TYPE <> 'Non-Diagnostic' OR D.QXP_EXCEPTION_TYPE IS NULL)
ORDER BY 4
and
--ALTER PROCEDURE [dbo].[ComplaintTrending2]
DECLARE
#FirstMonthDate DATETIME,
#LastMonthDate DATETIME,
#TheLevel INT
--AS
-- SET NOCOUNT ON;
--ComplaintTrending2 '2/1/11', '2/28/11 23:59:59', 2
SET #FirstMonthDate = '2/1/11'
SET #LastMonthDate = '2/28/11 23:59:59'
SET #TheLevel = '2'
SELECT
CASE ISNULL(PRODUCT_GROUP, '') WHEN '' THEN 'Unspecified' ELSE PRODUCT_GROUP END AS PRODUCT_GROUP,
COUNT(DISTINCT A.QXP_EXCEPTION_NO) AS CountOfTickets
FROM ALL_COMPLAINTS a
LEFT OUTER JOIN MANUAL.PRODUCTS b ON a.EPA_PRD_CODE = b.LIST_NUMBER
LEFT OUTER JOIN SMARTSOLVE.V_CXP_CUSTOMER_PXP c ON a.QXP_ID = c.QXP_ID
WHERE a.QXP_REPORT_DATE >= #FirstMonthDate AND a.QXP_REPORT_DATE <= #LastMonthDate
AND (a.QXP_SHORT_DESC <> 'Design Control') AND LEVEL = #TheLevel
AND (c.QXP_EXCEPTION_TYPE <> 'Non-Diagnostic' OR c.QXP_EXCEPTION_TYPE IS NULL)
GROUP BY PRODUCT_GROUP
ORDER BY COUNT(DISTINCT a.QXP_EXCEPTION_NO) DESC
Could the problem be due to the fact that this web page uses the same SQL connection for both stored procs? The first proc should be returned initially, and the second proc after this screen. So I don't need both stored procs information simultaneously, so I would think I could reuse the same SQL connection.
OK, I found where the problem is located. If I just comment out the Level statement in the Where clause, it does return data. So only thing I changed was that one line in my SQL code and it works. This means missing data must be due to datatype incompatibility, no? What is the problem?
I think the problem is your code does not even compile. Looking at the posted page right at the start we see this:
End If
Main
End Sub
This is not valid VB. You are running an old version of your code. This is also why you can't debug it. You don't have the current one to set break points on.

Resources