Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > ASP.NET 4 General Discussion
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #11 (permalink)  
Old April 17th, 2011, 04:44 AM
Imar's Avatar
Wrox Author
Points: 72,038, Level: 100
Points: 72,038, Level: 100 Points: 72,038, Level: 100 Points: 72,038, Level: 100
Activity: 50%
Activity: 50% Activity: 50% Activity: 50%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,080
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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.
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #12 (permalink)  
Old April 17th, 2011, 02:11 PM
Authorized User
 
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
Default 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.
Reply With Quote
  #13 (permalink)  
Old April 17th, 2011, 02:29 PM
Imar's Avatar
Wrox Author
Points: 72,038, Level: 100
Points: 72,038, Level: 100 Points: 72,038, Level: 100 Points: 72,038, Level: 100
Activity: 50%
Activity: 50% Activity: 50% Activity: 50%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,080
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #14 (permalink)  
Old April 17th, 2011, 02:54 PM
Imar's Avatar
Wrox Author
Points: 72,038, Level: 100
Points: 72,038, Level: 100 Points: 72,038, Level: 100 Points: 72,038, Level: 100
Activity: 50%
Activity: 50% Activity: 50% Activity: 50%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,080
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Oh, BTW, if you're using VB you need to set it to Nothing instead:

reservationStatus.ReservationID = Nothing

Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #15 (permalink)  
Old April 17th, 2011, 03:04 PM
Authorized User
 
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
Default 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
Reply With Quote
  #16 (permalink)  
Old April 30th, 2011, 07:42 AM
Authorized User
 
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
Default 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
Reply With Quote
  #17 (permalink)  
Old April 30th, 2011, 07:49 AM
Imar's Avatar
Wrox Author
Points: 72,038, Level: 100
Points: 72,038, Level: 100 Points: 72,038, Level: 100 Points: 72,038, Level: 100
Activity: 50%
Activity: 50% Activity: 50% Activity: 50%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,080
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #18 (permalink)  
Old April 30th, 2011, 08:04 AM
Authorized User
 
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
Default 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
Reply With Quote
  #19 (permalink)  
Old April 30th, 2011, 09:25 AM
Authorized User
 
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
Default 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?
Reply With Quote
  #20 (permalink)  
Old April 30th, 2011, 10:00 AM
Imar's Avatar
Wrox Author
Points: 72,038, Level: 100
Points: 72,038, Level: 100 Points: 72,038, Level: 100 Points: 72,038, Level: 100
Activity: 50%
Activity: 50% Activity: 50% Activity: 50%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,080
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
update multiple columns in an update statement debbiecoates SQL Server 2000 1 August 17th, 2008 04:01 AM
UPDATE statement takwirira ASP.NET 2.0 Basics 7 March 7th, 2007 07:48 PM
insert and/or update statement carswelljr SQL Language 3 September 28th, 2006 10:03 AM
Update Statement some error thas123 ADO.NET 1 January 8th, 2006 10:30 AM



All times are GMT -4. The time now is 11:43 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.