Working on an application that relies on an older version of entity, and I'm trying to insert a NULL into an int field. The field in SQL Server is (int, null).
Here's the definition of the object in EF:
<EdmScalarPropertyAttribute(EntityKeyProperty:=false, IsNullable:=true)>
<DataMemberAttribute()>
Public Property application_id() As Nullable(Of Global.System.Int32)
...and here is where I'm trying to set it:
applications.application_id = IIf(IsNumeric(txtAppID.Text), CInt(txtAppID.Text), Nothing)
The error thrown in response is:
An exception of type 'System.InvalidCastException' occurred in ... but was not handled in user code
Additional information: Specified cast is not valid.
I can confirm that this issue is being thrown due to the Nothing portion because previously it was applications.application_id = CInt(txtAppID.Text) and all was fine.
I've tried DBNull.Value instead of Nothing, though the error reads the same. Done a fair bit of research though most issues relate to ES6 or datetime fields, and as such I felt my issue was specific enough to warrant its own question.
Thanks.
The IIf function doesn't short circuit, and therefore always evaluates both the true and false parts, so it's not going to work in that situation. The If keyword does short circuit, but you will probably run into issues with the return type and nullable value types (e.g. Dim x As Integer? = If(False, 1, Nothing) results in x = 0 because the If is returning Integer and not Integer?).
So, I would recommend either using a regular If statement:
If IsNumeric(txtAppID.Text) Then
applications.application_id = CInt(txtAppID.Text)
Else
applications.application_id = Nothing
End If
or you could create a helper function:
Function NullableCInt(value As String) As Integer?
If IsNumeric(value) Then Return CInt(value)
Return Nothing
End Function
and use that:
applications.application_id = NullableCInt(txtAppID.Text)
You can get working If method with casting
Dim temp As Integer
applications.application_id = If(Integer.TryParse(value, temp), temp, DirectCast(Nothing, Integer?))
For better readability you can introduce "default" value
Static DEFAULT_VALUE As Integer? = Nothing
Dim temp As Integer
applications.application_id = If(Integer.TryParse(value, temp), temp, DEFAULT_VALUE)
With Integer.TryParse you need "check/convert" string to integer only once.
Related
I'm trying to use the values inside of a Variant.
An example of the code:
Dim Holder as Variant
Holder = "1,1,1,1,1"
Later I will be using this Variant inside of an Array().
The Declaration is like this:
.TextFileColumnDataTypes = Array(Holder)
There was an error right after this
Run-Time Error '5':
Invalid Procedure call or argument*
Is there a way to insert the Variant's values into Array(<Here?).
Holder's value is not constant, it will change depending on a function I created.
A way to accomplish this is as suggested in comments, using the split
If you are getting an error I am guessing that you are not using it correctly.
This is how the code should look.
dim i as long
dim var
Holder = "1,1,1,1,1"
var = Split(Holder,",")
for i = 0 to 4
.TextFileColumnDataTypes = var(i)
next i
I'm a beginner and I'm encountering a problem with an index of a one-dimensional array of strings (m_nameList in the code) that I'm struggling to understand. I have one method (in a class of a Windows Form Application that is not the MainForm) that returns FALSE if a string of the array at a certain index is empty, and viceversa. It looks like this:
Public Function IsReserved(index As Integer) As Boolean
Dim reserved As Boolean = False
If (Not String.IsNullOrEmpty(m_nameList(index))) Then
reserved = True
Else
reserved = False
End If
Return reserved
End Function
I have also a method on the MainForm that calls for it, and if the first method returns true then the second one displays a dialog box, otherwise it doesn't (lstResults is a listbox that has for items the strings of the array):
Private Function CheckIfSeatIsAlreadyReserved() As Boolean
Dim reserved As Boolean = m_seatMngr.IsReserved(lstResults.SelectedIndex)
If (reserved) Then
Dim msgBox As Integer = MessageBox.Show("The seat is already reserved. Would you like to continue?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If (msgBox = DialogResult.Yes) Then
Return True
Else
Return False
End If
Else
Return True
End If
End Function
The problem I find is: if as in the code of the first method above I don't initialize index to any value, the second method will work perfectly, but I'll get a runtime error if I don't select any item on the listBox (and therefore index = -1). On the other hand, if I initialize index to 0 or to m_nameList.Length, then the program won't have any problems if I don't select any item on the ListBox and it will nicely display a warning message I wrote for that case, BUT the second method will behave in a weird way: it will find ALL the strings of the array full even if I only "write" the first one, or it will find ALL the strings empty if I "write" any string that it's not the first one (by writing I mean that I call for a method that puts my input as the text of the string selected). Any idea on why this happens and how I can fix it? Thank you so much.
I will get to the answer below, follow the logic, lets look at your first method. You can refactor it to
Public Function IsReserved(index As Integer) As Boolean
Return Not String.IsNullOrEmpty(m_nameList(index))
End Function
Everything else is called "noise". But as Fabio pointed, what if index is bad? Lets add useful code
Public Function IsReserved(index As Integer) As Boolean
If index < 0 OrElse index > m_nameList.Length - 1 Then
Throw New ArgumentOutOfRangeException(". . . . .")
End If
Return Not String.IsNullOrEmpty(m_nameList(index))
End Function
If you get this exception, your program has a bug - and you know to fix the consumer of this function.
And now lets get to your message
second method will behave in a weird way: it will find ALL the strings of the array full even if I only "write" the first one, or it will find ALL the strings empty if I "write" any string
Most likely reason why it happens - simply because you create condition when you send same value over and over again. Therefore result is same. And this is most likely a bug
If (reserved) Then
. . . . . .
Else
Return True ' <---- BUG ALLERT
End If
And two more things, your second method can shrink down to 1 line too.
Private Function CheckIfSeatIsAlreadyReserved() As Boolean
Return m_seatMngr.IsReserved(lstResults.SelectedIndex) AndAlso _
DialogResult.Yes = MessageBox.Show("The seat is already reserved. Would you like to continue?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
End Function
And, if you do some sort of reservation, array is not really good idea. How about Dictionary(Of String, Boolean)? Your theater or airplane has bunch of seats and you pre-load your dictionary with those
d.Add("1A", False)
d.Add("1B", False)
d.Add("1C", False)
d.Add("1D", False)
d.Add("1E", False)
d.Add("1F", False)
This is your whole airplane row. Now you can do much more with it
I am using SQL server and in my program I am currently using a count like this:
Function NumberOfApplications(inCandidateID As Integer) As Integer
Dim iCount As Integer = 0
Dim oApplication As DataModels.Application
For Each oApplication In Me.Applications
If oApplication.CandidateID = inCandidateID Then
iCount = iCount + 1
End If
Next
Return iCount
End Function
I have created my entities and bought in the correct Imports but I need to know how to perform a count using LINQ and VB. As you can see above I am using the Candidate ID to count. Any help would be appreciated. Thank you.
As #Pleun mentions, you can use the Count method, but the method itself has a predicate option, so you can use:
return Me.Applications.Count(app => app.CandidateID == inCandidateID);
In C#. I always have to think hard what that would look like in VB, this is what my brain tells me. The compiler migt disagree though ;):
Return Me.Applications.Count(Function(app) app.CandidateID = inCandidateID)
If you prefer the sql-like syntax, you could also do this:
Function NumberOfApplications(inCandidateID As Integer) As Integer
Return (From app As DataModels.Application In Me.Applications
Where app.CandidateID = inCandidateID).Count
End Function
I'm new to F# and trying to dive in first and do a more formal introduction later. I have the following code:
type Person =
{
Id: int
Name: string
}
let GetPeople() =
//seq {
use conn = new SQLiteConnection(connectionString)
use cmd = new SQLiteCommand(sql, conn)
cmd.CommandType <- CommandType.Text
conn.Open()
use reader = cmd.ExecuteReader()
let mutable x = {Id = 1; Name = "Mary"; }
while reader.Read() do
let y = 0
// breakpoint here
x <- {
Id = unbox<int>(reader.["id"])
Name = unbox<string>(reader.["name"])
}
x
//}
let y = GetPeople()
I plan to replace the loop body with a yield statement and clean up the code. But right now I'm just trying to make sure the data access works by debugging the code and looking at the datareader. Currently I'm getting a System.InvalidCastException. When I put a breakpoint at the point indicated by the commented line above, and then type in the immediate windows reader["name"] I get a valid value from the database so I know it's connecting to the db ok. However if I try to put reader["name"] (as opposed to reader.["name"]) in the source file I get "This value is not a function and cannot be applied" message.
Why can I use reader["name"] in the immediate window but not in my fsharp code? How can I use string indexing with the reader?
Update
Following Jack P.'s advice I split out the code into separate lines and now I see where the error occurs:
let id = reader.["id"]
let id_unboxed = unbox id // <--- error on this line
id has the type object {long} according to the debugger.
Jack already answered the question regarding different syntax for indexing in F# and in the immediate window or watches, so I'll skip that.
In my experience, the most common reason for getting System.InvalidCastException when reading data from a database is that the value returned by reader.["xyz"] is actually DbNull.Value instead of an actual string or integer. Casting DbNull.Value to integer or string will fail (because it is a special value), so if you're working with nullable columns, you need to check this explicitly:
let name = reader.["name"]
let name_unboxed : string =
if name = DbNull.Value then null else unbox name
You can make the code nicer by defining the ? operator which allows you to write reader?name to perform the lookup. If you're dealing with nulls you can also use reader?name defaultValue with the following definition:
let (?) (reader:IDataReader) (name:string) (def:'R) : 'R =
let v = reader.[name]
if Object.Equals(v, DBNull.Value) then def
else unbox v
The code then becomes:
let name = reader?name null
let id = reader?id -1
This should also simplify debugging as you can step into the implementation of ? and see what is going on.
You can use reader["name"] in the immediate window because the immediate window uses C# syntax, not F# syntax.
One thing to note: since F# is much more concise than C#, there can be a lot going on within a single line. In other words, setting a breakpoint on the line may not help you narrow down the problem. In those cases, I normally "expand" the expression into multiple let-bindings on multiple lines; doing this makes it easier to step through the expression and find the cause of the problem (at which point, you can just make the change to your original one-liner).
What happens if you pull the item accesses and unbox calls out into their own let-bindings? For example:
while reader.Read() do
let y = 0
// breakpoint here
let id = reader.["id"]
let id_unboxed : int = unbox id
let name = reader.["name"]
let name_unboxed : string = unbox name
x <- { Id = id_unboxed; Name = name_unboxed; }
x
private Incident incident = null;
incident = (Incident)(rdc.Incidents.Where(i => i.ID == ID));
I get the following exception:
Unable to cast object of type 'System.Data.Linq.DataQuery`1[WPF.Incident]' to type 'WPF.Incident'.
I need an instance of Incident to use it like this:
IList listInjury = ((IListSource)incident.Incident_Injuries.OrderBy(m => m.Employee.LastName)).GetList();
Try:
incident = rdc.Incidents.First(i => i.ID == ID);
The Where method can return multiple results (probably not in your specific case, but in the general case it could), so you'll need to get the first (and presumably only) result with First method like Mehrdad described
The code
(Incident)(rdc.Incidents.Where(i =>i.ID == ID))
returns a sequence, IEnumerable<Incident> and you are trying to cast that to type of Incident.That's why you're getting InvalidCastException because those types are not compatible. As Mehrdad suggested, you can use First. However, First would throw an exception if the sequence does not contain any elements. This may or may not be desirable. If exception is not desirable, you can call DefaultOrEmpty which would return the default value for that type if the sequence contains no elements. If Incident is a reference type, the default value would be null and you should add a null check in your code and handle that case appropriately.