I have VB.Net code which value taking from db and if it is null do some job
While reader.Read
tocken = reader.GetInt16("Tocken_No")
If tocken.HasValue = False Then
TxtPTocken.Text = 1
Else
TxtPTocken.Text = tocken + 1
End If
End While
Else statement working properly but first part not. tocken is nullable short. What I'm missing? Please help
That code doesn't make sense. You're calling GetInt16, which means you're going to get an Int16. Your tocken variable may be type Nullable(Of Short) but you're assigning a value to it so HasValue is ALWAYS going to be True.
Your issue there is that if your database field is NULL then your code is going to throw an exception. Methods like GetIn16 fail if there isn't a value of the expected type available. It's up to you to check for NULL and handle that accordingly:
tocken = If(reader.IsDBNull(reader.GetOrdinal("Tocken_No")),
CType(Nothing, Short?),
reader.GetInt16("Tocken_No"))
Related
working on a trigger in Postgres. Currently running into an issue where I'm trying to set a value as either an empty array or an array with just the one NEW value, depending on if there is a NEW value or not:
project_ids =
CASE NEW.project_tracking_id
WHEN null THEN ARRAY
ELSE ARRAY[NEW.project_tracking_id]
END
"Set project_ids to either [] (if null) or [NEW.project_tracking_id]"
The else block works as expected and will store that new value as the first element in a new array. The WHEN null THEN ARRAY part does not work for me though. It just adds null to the array, producing [null].
How do I specify that it should just be an empty array in that case? Not sure if this helps, but my sequelize type for that field is:
type: Sequelize.ARRAY(Sequelize.STRING),
defaultValue: [],
project_ids =
CASE NEW.project_tracking_id
WHEN null THEN ARRAY[]::XXX[] //XXX is the type of the array.
ELSE ARRAY[NEW.project_tracking_id]
END
The problem is that your CASE expression implicitly compares NEW.project_tracking_id = NULL, which is never true, even if the left side is NULL. Use the other form of CASE:
CASE WHEN NEW.project_tracking_id IS NULL
THEN ARRAY[]::integer[]
ELSE ARRAY[NEW.project_tracking_id]
END
This assumes that the data type of NEW.project_tracking_id is integer; change the array type if that is not the case.
A shorter version of the above would be
coalesce(NEW.project_tracking_id, ARRAY[]::integer[])
Tired brain - perhaps you can help.
My table has two bit fields:
1) TestedByPCL and
2) TestedBySPC.
Both may = 1.
The user interface has two corresponding check boxes. In the code I convert the checks to int.
int TestedBySPC = SearchSPC ? 1 : 0;
int TestedByPCL = SearchPCL ? 1 : 0;
My WHERE clause looks something like this:
WHERE TestedByPCL = {TestedByPCL.ToString()} AND TestedBySPC = {TestedBySPC.ToString()}
The problem is when only one checkbox is selected I want to return rows having the corresponding field set to 1 or both fields set to 1.
Now when both fields are set to 1 my WHERE clause requires both check boxes to be checked instead of only one.
So, if one checkbox is ticked return records with with that field = 1 , regardless of whether the other field = 1.
Second attempt (I think I've got it now):
WHERE ((TestedByPCL = {chkTestedByPCL.IsChecked} AND TestedBySPC = {chkTestedBySPC.IsChecked})
OR
(TestedByPCL = 1 AND TestedBySPC = 1 AND 1 IN ({chkTestedByPCL.IsChecked}, {chkTestedBySPC.IsChecked})))
Misunderstood the question.
Change the AND to an OR:
WHERE TestedByPCL = {chkTestedByPCL.IsChecked} OR TestedBySPC = {chkTestedBySPC.IsChecked}
Also:
SQL Server does not have a Boolean data type, it's closest option is a bit data type.
The usage of curly brackets suggests using string concatenations to build your where clause. This might not be a big deal when you're handling checkboxes but it's a security risk when handling free text input as it's an open door for SQL injection attacks. Better use parameters whenever you can.
i want to count the data type of each redis key, I write following code, but run error, how to fix it?
local detail = {}
detail.hash = 0
detail.set = 0
detail.string = 0
local match = redis.call('KEYS','*')
for i,v in ipairs(match) do
local val = redis.call('TYPE',v)
detail.val = detail.val + 1
end
return detail
(error) ERR Error running script (call to f_29ae9e57b4b82e2ae1d5020e418f04fcc98ebef4): #user_script:10: user_script:10: attempt to perform arithmetic on field 'val' (a nil value)
The error tells you that detail.val is nil. That means that there is no table value for key "val". Hence you are not allowed to do any arithmetic operations on it.
Problem a)
detail.val is syntactic sugar for detail["val"]. So if you expect val to be a string the correct way to use it as a table key is detail[val].
Possible problem b)
Doing a quick research I found that this redis call might return a table, not a string. So if detail[val] doesn't work check val's type.
i have this query as linq:
Dim result = (From c In query _
Where Not bannedCCList.Contains(c.num_reserv) _
Group c By c.code_operation, c.code_type _
Into nbr = Count(CInt(c.Code_bien)), acmp = Sum(CDec(c.TotalAcomp)) _
Select nbr, acmp).ToList
but i get the error:
An object that allows Null must have a value
how i can put acmp=0 if c.TotalAcomp is nothing
i that my query look like:
acmp =IIF(Sum(CDec(c.TotalAcomp)) is nothing,0, Sum(CDec(c.TotalAcomp)) _
but it doesn't work.
try this:
acmp =IIF(Sum(CDec(c.TotalAcomp)) is DBNull.Value,0, Sum(CDec(c.TotalAcomp))
Don´t use IIF. It´s old VB6 garbage, e.g. it evaluates always both sides even if the true-part returns true. Use If instead.
Your check if the variable is Nothing happens at the wrong place. You have to check it inside the Sum function not afterwards.
Also use HasValue and Value properties of nullable types.
In this case you don´t have to cast any of your expression (besides you casted to the wrong type. If your variable is of type Double don´t cast to Decimal).
The following code worked for me:
acmp = Sum(If(Not c.TotalAcomp.HasValue, 0, c.TotalAcomp.Value))
I am using Entity Framwork and have an Entity Customer with a property of:
CustomerStatus(possible values NULL, 0, 1)
I had code that read
If Not Customer.CustomerStatus = 1 Then
' I want this to execute when Customer.CustomerStatus Is NULL or 0
'Do This
End If
However I noticed that this code does not get executed when Customer.CustomerStatus Is Nothing.
Is this correct for all objects or am I doing soemthing wrong?
Your CustomerStatus is a Nullable(int). This means it can be NULL (it doesn't have a value) or it can be any other Int.
Comparing NULL to 1 returns false. If you want the test to pass one NULL and 1 you could use Customer.CustomerStatus = 1 Or (Not Customer.CustomerStatus.HasValue)