Error when trying to cast db query to string - vb.net

I am trying to extract one integer from the database but am getting errors
My code in the controller is:
Dim SeqNumQuery = From a In db.RequestedServices
Where a.RequestedServiceId = id
Select a.StationId
Dim StationId As Integer = SeqNumQuery.ToString
And error is:
Range variable 'StationId' hides a variable in an enclosing block or a range variable previously defined in the query expression.

I think the issue is that your variable, StationId, is being declared somewhere already within the same scope. Try changing to:
Dim SeqNumQuery = From a In db.RequestedServices
Where a.RequestedServiceId = id
Select a.StationId
StationId = SeqNumQuery.ToString
But look into what the other variable is being used for. If you need it, use something other than StationId for your variable name, for example RequestedServicesStationId.
More information regarding this error:
Range variable hides a variable in an enclosing block or a range variable previously defined in the query expression.
On a side note, I don't understand why you are using .ToString extension method. This won't cause your script to throw an exception, however I would recommend changing this to CInt():
StationId = CInt(SeqNumQuery)

Related

MS Access correctly utilizing a string variable within DLookup

I am attempting to replace the domain parameter of DLookup with a variable, the intent being a single place to make a change if one is required. This is how I am declaring the variable:
Dim MnMnuSettingTbl As String
MnMnuSettingTbl = "'tblMainMenu'"
This is the original segment where the variable is to be used:
Me.MainMenuChoiceOne.Caption = DLookup("BtnText", "tblMainMenu", "ID = 1")
I wish to replace the domain criteria "tblMainMenu" with the variable, but when I attempt to do so it either does not compile, or I get an error message stating the table can not be found. I have reviewed several articles on this matter, and I am gathering I am not passing the variable correctly, via the improper use of single or double quotes. I'm rather embarrassed, so at this point I am looking for the correct way to either format the variable or the correct way to use it within the DLookup context.
The variable must contain the same constant string as you currently have in the DLookup.
MnMnuSettingTbl = "tblMainMenu"
Me.MainMenuChoiceOne.Caption = DLookup("BtnText", MnMnuSettingTbl , "ID = 1")
Single quotes would be needed for string parameters in the WHERE clause, e.g.
strTextID = "'QD42'"
x = DLookup("foo", "bar", "TextID = " & strTextID)

Make a string equal to what a named range refers to?

I have a named range called tempPrintArea
it refers to ="A1:J59"
I created it with VBA and the scope became local. I don't know if the scope matters.
I used this line to create the named range:
wks.Names.Add Name:="tempPrintArea", RefersTo:="A1:J59"
Now I want to set the value of a string to "A1:J59". I've tried the following:
Dim test As String
test = Range("tempPrintArea").RefersTo
but I get the error message Method 'Range' out of object '_Gloabl' failed
What can I change in these code lines to get this working?
When you're using
wks.Names.Add Name:="tempPrintArea", RefersTo:="A1:J59"
code doesn't create named range that refers to A1:J59, instead code creates named range with text "A1:J59".
For creating named range use this one instead:
wks.Names.Add Name:="tempPrintArea", RefersTo:=wks.Range("A1:J59")
and then
Dim test As String
test = wks.Range("tempPrintArea").Address(False, False) ' returns A1:J59

dr.Item("Value1") has value but always return 0 when assigned to another variable

I need to modify some VB.net code. There is a strange problem that I am facing. I am retrieving value from a DataTable and trying to assign it to a variable. When I check the value of some column in QuickWatch window then it has value but when I assign it to a variable then 0 is returned to the variable. Below is the simple statement that is causing the problem.
Dim MyAmount As Double = Double.Parse(dr.Item("Amount").ToString)
In the QuickWatch window when I check dr.Item("Amount") then it has value 30.12 and after executing the above statement MyAmount has value 0. May be VB.net work somewhat different that I do not know?
Edit:
It is kind of wierd that above mentioned statement is not returning value. The following statement is running absolutely fine.
Dim tmpVar As String() = dr.Item("Amount").ToString.Split(".")
Latest Edit:
I think it has become more wierd. The problem does not seem to be related with dr.Item("Amount"). Suppose I want to store the current culture value in a variable by following code,
Dim CultureInformation As String = System.Globalization.CultureInfo.CurrentCulture.DisplayName
Now CutlureInformation variable after the statement is executed contains "nothing" but the DisplayName has value of English (United States). So I think the problem is somewhere else.
You should be using this syntax:
Dim MyAmount As Double = dr.Field(Of Double)("Amount")
I am not sure why you are getting this behavior - your line should work too.
You can also try this:
Dim MyAmount As Double = DirectCast(dr.Item("Amount"), Double)
When facing a weird issue like this, always try various options to achieve the same result, do your research and compare the outputs. It greatly helps to answer a question on StackOverflow.

Razor VB - Pass a Null value if a Request variable is empty

I've been trying so hard to find a solution for this, but no luck. I'm fairly new to VB and SQL, but this shouldn't be too hard. I've inherited a lot of code and there's not too much room to change db attribute types or anything.
I'm trying to run an UPDATE query using parameters in Razor, like so:
Dim updateCommand = "UPDATE [tblJobRating] SET [ProjectManagement] = #0, [ProjComments] = #1, [Schedule] = #2, [SchedComments] = #3 WHERE [JobRatingID] = #4"
All of the columns in question need INT values, but I have one exception where I need to pass it a null value (passing another number or zero won't do). Essentially a "N/A" value for the user.
I assign the variables from Post requests, like so:
Dim projMgmt = Request.Form('projMgmt')
' ...
Dim sched = Request.Form('sched')
I have the "N/A" value posting no value right now (or it can be a string and I can check for IsNumber if need be, I guess). But when I call the query execution, it enters the value as a 0.
db.Execute(updateCommand, projMgmt, projComments, sched, schedComments, ratingId)
It needs to be a NULL value for the backend to work properly. I've tried type checking and passing Nothing, System.Data.SqlTypes.SqlInt32.Null, etc., but it either gives conversion errors or sets to 0. How can I pass it properly?
Edit: I left out the first param in the db.Execute method, passing in the updateCommand. Edited for clarity.
The problem is in your vb variable definition. I assume you have an integer, it needs to be a nullable(of integer) all the way through to the SQL. This can also be written as integer?.

Get a table count in SSIS from an Oracle view

In my SSIS package, I want to see if a particular Oracle view has data in it.
I have a Execute SQL Task with a select count statement:
Select CAST( count(*) as Integer) As Row_Count from OWNER.VIEW_MV
My RowCount is in the Result Set, pointing to my Variable User:TableCount. If I set TableCount to anything except Object, I get an error such as:
Error: 0xC002F309 at Check for data in
view, Execute SQL Task: An error
occurred while assigning a value to
variable "TableCount": "The type of
the value being assigned to variable
"User::TableCount" differs from the
current variable type. Variables may
not change type during execution.
Variable types are strict, except for
variables of type Object.
However, if I do make the data type to Object, then in my next step, a Script Task, when I read that object, I don't see how to convert it to an integer so that I can use and view it. Then I get the error:
Conversion from type 'Object' to type 'Integer' is not valid.
with the code
Dim nTableCount As Int32
nTableCount = CInt(Dts.Variables("TableCount").Value)
Perhaps I am going about this wrong. What is the best way to determine if an Oracle table is empty, and then act on that knowledge? Essentially, we want an error message about the empty table, rather than continuing on in our process.
Update:
I've tried Select CAST( count(*) as char) As Row_Count from OWNER.VIEW_MV where ROWNUM < 2, and sending it to a SSIS variable of type char. I've cast it to Numeric and Integer, with SSIS variables of Int32, Int64. I've tried varchar2(20) with SSIS type String. Everything gives an error except for SSIS datatype Object.
Right now, I'm saving as datatype Object, but when I try to get the value, setting my nTableCount as String, I can use nTableCount = Dts.Variables("TableCount").Value().ToString(), but it returns 0. How do I extract that string out of the Object variable? (Assuming of course, that it actually put it in there.)
I know nothing about SSIS so can't help you there, but, as a note, if you only want to check for the presence of data in a table, it's more efficient to include a ROWNUM clause in the SQL. e.g.
SELECT COUNT(*)
FROM table
WHERE ROWNUM < 2;
(will return 0 if the table is empty, 1 if any rows are in the table)
In this way Oracle can stop reading the results from the table/view as soon as it finds any rows, thus (potentially) finishing execution of the query much sooner.
SSIS has a lot of trouble talking to Oracle. I have faced similar issues before. Did you try using string data type? You will not have trouble converting string to integer in script.
I found a solution. It may or may not be the best solution, but it will work. Other solutions and improvements are always welcome.
First, rather than having a SQL Task, I have a Data Flow task with the Oracle source and the Data Access Mode is a SQL Command with the original shown in the question (although, I'll probably use cagcowboy's advice once this is working properly).
The output is a destination Script Transformation. I selected ROW_COUNT as my input column, didn't mess with the Inputs and Outputs, and for the Script selected my TableCount as ReadWriteVariables. My script is as below:
Public Class ScriptMain
Inherits UserComponent
Dim ScriptCount As Single
Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)
' Assign the variable to something outside of the sub
ScriptCount = Row.ROWCOUNT
End Sub
Public Overrides Sub PostExecute()
' Variable is only available to set in PostExecute
Me.Variables.TableCount = ScriptCount
End Sub
End Class
My public variable is of type Double, and in my final script task (outside of the data flow), I simply access it with the lines
Dim nTableCount As String
nTableCount = Dts.Variables("TableCount").Value().ToString()
I suspect I should do a bit more with the data types, because it doesn't seem like it should need to be converted to a string at this point. But that's ok, I can now determine if there is data in the view, and fork accordingly.
this is what worked for me. The idea is to convert count(*) to char and use String type variable in SSIS:
SELECT TO_CHAR(COUNT(*)) FROM yourtable;