 |
BOOK: Beginning ASP.NET 4 : in C# and VB
 | This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211 |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB 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
|
|
|
|
|

August 3rd, 2012, 09:14 AM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
inserting records in a database
In chapter 14 of the book, users were able to choose a genre from the dropdown list and it was associated with the review they write, however I want people to be able to insert any genre they know through a text box, BUT that can cause duplicates of the same genre if few people insert the same genre name, so I want a way to first check if the genre is available, if the answer is yes then directly use the GenreId and if not then Add the genre for future users.
How can I go about it??
|
|

August 3rd, 2012, 09:17 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
You can query the EF for the genre. E.g. something like:
var myGenre = _context.Genres.FirstOrDefault(x => x.Name == genre.Text);
Then check the myGenre variable. If its null, create a new Genre, add it to EF and safe it.
After that, myGenre should be a valid genre whose ID you can use to associate the review with.
Hope this helps,
Imar
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|

August 3rd, 2012, 09:40 AM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
but is there any easier way to do it like using the sqlDataSource or anything simpler?? 
|
|

August 3rd, 2012, 09:47 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Nope; they don't enable you to check first, and then create.
But is this so complex? It should only take a couple of lines of code....
Imar
|
|

August 3rd, 2012, 09:54 AM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
I know, but for me as a beginner I found that linq is a little bit hard to grasp from the first time, so I think I need more time to fully understand it.
I would really appreciate if you can give me the code snippet that I can copy and paste directly.
Thanks
|
|

August 3rd, 2012, 10:10 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
That's a bit too much for me to do over a forum post. Why don't you give it a try, post your result and I'll help you complete it? I would start with the "Hand Coding" example in the LINQ chapter if I were you....
Cheers,
Imar
|
|

August 4th, 2012, 12:27 AM
|
|
Authorized User
|
|
Join Date: Jul 2012
Posts: 21
Thanks: 1
Thanked 2 Times in 2 Posts
|
|
I'm in complete agreement with Imar here, jasonrock. Why would you want to make it more difficult for a user? I've built many professional ASP.NET applications. From experience, I can tell you that nothing would frustrate a user more than to type in a genre only to be told the genre already exists.
My suggestion is you provide them with a dropdown (in a separate table), and if it doesn't exist then you could provide them with the option to add one. Keep in mind that making applications simple and fun to use is more important than just about any code you'll ever write.
|
|

August 4th, 2012, 05:12 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
>> I'm in complete agreement with Imar here, jasonrock. Why would you want to make it more difficult for a user?
That's not entirely what I mean. Having the ability to pick an existing or create a new one at the same UI could be very useful. For an example, take a look here: http://www.asp.net/ajaxLibrary/AjaxC.../ComboBox.aspx (and set the DropDownStyle to Simple to have insert and select behavior.
With "complex" I was referring to the actual implementation using LINQ which shouldn't be too hard if you've set up most of the UI already.
Cheers,
Imar
|
|

August 4th, 2012, 11:29 AM
|
|
Authorized User
|
|
Join Date: Jul 2012
Posts: 21
Thanks: 1
Thanked 2 Times in 2 Posts
|
|
Sorry, Imar. I wasn't trying to imply that's what you meant. *embarrassed grin here* I just inadvertantly mixed your opinion with my own.
To clarify, I simply meant I agreed with you that the implementation via LINQ wasn't difficult at all. The remainder was my attempt to help jasonrock understand the importance of a good user interface.
|
|
 |
|