 |
| ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

April 17th, 2011, 04:44 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi Phil,
Yes, that should work without a problem. Something like this should work:
Code:
myEntity.PropertyOfTypeGuid = null;
In order for this to work, the PropertyOfTypeGuid column needs to be marked as nullable in the database before you add its table to the EF diagram.
Hope this helps,
Imar
BTW: while related to updates, I think this question would have been better off in a new thread. Makes it easier for everyone to find relevant stuff.
|
|

April 17th, 2011, 02:11 PM
|
|
Authorized User
|
|
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
|
|
LINQ Update
Hi Imar
I did originally think that it was that simple but when I tried setting the value as 'null' as per your example Visual Studio code behind page throws an exception saying it is not allowed. The SQL Server database table for that particular field does allow NULL values.
What I ended up doing is
Code:
reservationStatus.ReservationID = (GUID.Empty)
From what I've been able to find out about the GUID.Empty option from Google searches this should provide me with the same functionality as changing the GUID to NULL, which like I say is not possible through Visual Studio.
|
|

April 17th, 2011, 02:29 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
I just tried it in my own code and it works fine. Did you set the column as nullable before you added the table to the diagram? Otherwise, the EF diagram is not aware of this. You can check the property in the EF designer and look at the properties grid. What do you see for the nullable behavior?
Guid.Empty is sort of null, but not really. It looks like 000000-000000-000000-000000-00000 in the database.
Cheers,
Imar
|
|

April 17th, 2011, 02:54 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Oh, BTW, if you're using VB you need to set it to Nothing instead:
reservationStatus.ReservationID = Nothing
Hope this helps,
Imar
|
|

April 17th, 2011, 03:04 PM
|
|
Authorized User
|
|
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
|
|
LINQ Null Update
Hi Imar
Yeah the 'Nothing' function worked as you suggested. The majority of web sources I'd found suggested the 'Null' function I didn't even think that the NULL option was only available for C# I just took it for granted that it would work with VB.
Thank you once again for the suggestion.
Phil
|
|

April 30th, 2011, 07:42 AM
|
|
Authorized User
|
|
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
|
|
LINQ to Entities Select Query
Hi
Does anyone know how to assign the returned values of a LINQ to Entities Select query to individual variables?
Code:
Using myLiteratureCatalogue As New Model1.Literature_Cataloguing_SystemEntities
Dim getReservationStatus = From publication In myLiteratureCatalogue.Publications
Where publication.PublicationID = (PublicationID)
Select publication.ReservationID, publication.StatusID
ReservationID =
StatusID =
End Using
|
|

April 30th, 2011, 07:49 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi Phil,
You can loop over the result set and acces the properties if you have more than one item. If you know your query returns a single result, you can use Single() or SingleOrDefault at the end to create a single item:
Code:
Dim getReservationStatus = (From publication In myLiteratureCatalogue.Publications
Where publication.PublicationID = (PublicationID)
Select publication.ReservationID, publication.StatusID).Single()
SingleOrDefault returns Nothing when no item can be found, while Single() will throw an exception if the list is empty. That can be a good thing to track bugs if you think your query must always return a value.
Cheers,
Imar
|
|

April 30th, 2011, 08:04 AM
|
|
Authorized User
|
|
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
|
|
LINQ to Entities Select Statement
Hi Imar
Thank you for the quick response. My code should always return two values the ReservationID (GUID) and the StatusID(INT32) for the specific PublicationID (INT32) provided from a stored variable. When the code executes and brings back the results I want it to store the returns in additional stored variables so they can be accessed by other sub procedures within the same class
Code:
Using myLiteratureCatalogue As New Model1.Literature_Cataloguing_SystemEntities
Dim getReservationStatus = From publication In myLiteratureCatalogue.Publications
Where publication.PublicationID = (PublicationID)
Select publication.ReservationID, publication.StatusID
I did try it with SQL but for some reason SQL Select query is not bringing back any results
Code:
Using myConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("LiteratureCataloguingConnectionString").ConnectionString)
Const SQL As String = "SELECT ReservationID, StatusID FROM Publication WHERE PublicationID = @PublicationID"
Dim myCommand As New SqlCommand(SQL, myConnection)
myCommand.Parameters.AddWithValue("@PublicationID", PublicationID)
myConnection.Open()
Dim myReader As SqlDataReader = myCommand.ExecuteReader
If myReader.Read Then
ReservationID = myReader.GetGuid(0)
StatusID = myReader.GetInt32(1)
End If
myReader.Close()
myConnection.Close()
End Using
|
|

April 30th, 2011, 09:25 AM
|
|
Authorized User
|
|
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
|
|
LINQ to Entities Select Statement
Does anyone have an example of how you loop through a LINQ to Entities resultset to store each result as a different variable?
|
|

April 30th, 2011, 10:00 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi Phil,
I think you need to define what you mean with " to store each result as a different variable".
Consider this:
Code:
Dim getReservationStatus = From publication In myLiteratureCatalogue.Publications
Where publication.PublicationID = (PublicationID)
Select publication.ReservationID, publication.StatusID
What is getReservationStatus? It retuns an IEnumerable of some anonymous type with properties called ReservationID and StatusID.
Whether getReservationStatus returns one or more items depends on the query. If it retuns a single instance (with two properties) Single() should work. You should then be able to access getReservationStatus.ReservationID and getReservationStatus.StatusID
If, however, the query returns multiple objects (each with the two properties), you need a For Each loop over getReservationStatus and within the loop access the two properties.
Does this make sense?
Imar
|
|
 |