Wrox Programmer Forums
|
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
 
Old August 3rd, 2012, 09:14 AM
Registered User
 
Join Date: Aug 2012
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Default 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??
 
Old August 3rd, 2012, 09:17 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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
__________________
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!
The Following User Says Thank You to Imar For This Useful Post:
jasonrock (August 3rd, 2012)
 
Old August 3rd, 2012, 09:40 AM
Registered User
 
Join Date: Aug 2012
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Default

but is there any easier way to do it like using the sqlDataSource or anything simpler??
 
Old August 3rd, 2012, 09:47 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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
__________________
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!
 
Old August 3rd, 2012, 09:54 AM
Registered User
 
Join Date: Aug 2012
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Default

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
 
Old August 3rd, 2012, 10:10 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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
__________________
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!
 
Old August 4th, 2012, 12:27 AM
Authorized User
 
Join Date: Jul 2012
Posts: 21
Thanks: 1
Thanked 2 Times in 2 Posts
Default

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.
 
Old August 4th, 2012, 05:12 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

>> 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
__________________
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!
 
Old August 4th, 2012, 11:29 AM
Authorized User
 
Join Date: Jul 2012
Posts: 21
Thanks: 1
Thanked 2 Times in 2 Posts
Default

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.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Inserting D.B. records programatically ( entering the same value in all records ? ) liamfitz BOOK: Visual Basic 2008 Programmer's Reference ISBN: 978-0-470-18262-8 0 June 22nd, 2009 03:44 PM
Inserting Records Question? ersp ADO.NET 1 May 12th, 2004 09:55 AM
Problem In Inserting Records in database zaeem Classic ASP Databases 1 October 22nd, 2003 06:35 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.