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 15th, 2013, 06:50 AM
Authorized User
 
Join Date: May 2013
Posts: 12
Thanks: 3
Thanked 0 Times in 0 Posts
Default SQL access with data source control

Hello All,

for some time I am struggling with following: I want to access a SQl record and do (only) some arithmetic operation on it in a code behind page in c#.

I imagine I should use SQLDataSource but then what? Using DetailsView or FormView is not what I want since I don't want to show something.

Though everywhere it states that Linq/EF lets you " access data in your code" apparently it means through data bound controls and not in C#.

Is there a data bound control that not shows anything. Can anyone point me in the right direction for this?

Thanks in advance, all help is appreciated!

Erik
 
Old August 15th, 2013, 08:45 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

>> though everywhere it states that Linq/EF lets you " access data in your code" apparently it means through data bound controls and not in C#.

Nope, not true. Take a look at the LINQ chapters; you'll see many examples that don't use data controls. With LINQ you can query all kinds of data (sets, single objects, scalar values like a number or the count of records, and work with that.

What is it exactly you're trying to do? Can you describe your SQL table and LINQ model along with the expected actions against 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!
 
Old August 15th, 2013, 09:43 AM
Authorized User
 
Join Date: May 2013
Posts: 12
Thanks: 3
Thanked 0 Times in 0 Posts
Default Explanation on dbase table

In SQL I have a very straightforward table, With an Id , several text fields and one SQL field called

Distribution of type binary(936)

In a cs file I have defined:
Code:
public static class Guide
{
 // Definieer Locatie array
     static Boolean [,] _Loc = new Boolean[144,52];
     public static Boolean[,] Location { get { return _Loc; } set { _Loc = value; } }
I want, with a button, get Distribution out of dbase and map it onto Location. With another button I would like to return the result to the dbase.
I understand boolean array's can not be mapped directly so I converted the boolean to a Byte[936] array which has same length and should be mappable to the SQL binary.

However I can't get a good looking handle to do this SQL interaction and need some advice how to continu: With SQL, LinQ or EF. I will now first look at LinQ possibilities.
 
Old August 15th, 2013, 11: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

Have you tried generating an EDMX model based on the database table? Then you can do something like:

var location = db.Locations.First(x => x.Id == id);

and work with the location and its properties. (Note: just making up some code and name,s but I hope you get the idea).

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 15th, 2013, 12:38 PM
Authorized User
 
Join Date: May 2013
Posts: 12
Thanks: 3
Thanked 0 Times in 0 Posts
Default still not there

Thanks Imar,

On the route of edmx I try to make a test entity and have following in a button click:
Code:
 using (GuideEntities MyEntities = new GuideEntities())
        {
            var CR = from Guide in MyEntities.Guides
                     where Guide.Id == 1
                     select Guide.TestText;
            label1.text = CR;
        }
I get an conversion error for the label, if I do ToString() the query is not performed: How do I pickup the result of above select in a variable?

Best Regards,

Erik
 
Old August 15th, 2013, 01:43 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Erik,

(Nederlands?)

By default, a query returns an IEnumerable / IQueryable which is a collection, even though you query a single object. So this:

Code:
var CR = from Guide in MyEntities.Guides
                     where Guide.Id == 1
                     select Guide.TestText;
returns a collection that contains a single item. You can use Single, or SingleOrDefault if it's possible that the ID doesn't exist:

Code:
var CR = (from Guide in MyEntities.Guides
                     where Guide.Id == 1
                     select Guide.TestText).SingleOrDefault();
Then you can assign the text as follows:

Code:
if (CR != null)
{
  label1.Text = CR;
}
You can assign CR directly, as you're selecting its TestText property. The alternative is this:

Code:
var CR = (from Guide in MyEntities.Guides
                     where Guide.Id == 1
                     select Guide).SingleOrDefault();
Then you can assign the text as follows:

Code:
if (CR != null)
{
  label1.Text = CR.TestText;
}
There are many other ways to interact with LINQ and the underlying database, so you'll see different syntax as well.

I would recommend checking out the chapter on LINQ as well as getting a dedicated resource (book, online tutorial) on LINQ to learn more.

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 15th, 2013, 02:07 PM
Authorized User
 
Join Date: May 2013
Posts: 12
Thanks: 3
Thanked 0 Times in 0 Posts
Default Nederlands

Hallo Imar,

Ja Nederlands, en het voorbeeld werkt.

Is het boek : Professional Linq | Scott Klein Engels - Paperback | 2008 aan te bevelen? Ik zag dat het niet meer bij wrox te koop was (uitverkocht). Of weet je betere boeken?

Heel erg bedankt,

Erik
 
Old August 15th, 2013, 02:21 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Het is wat verouderd, maar veel kern concepten zijn nog wel hetzelfde.

Voor een "deep-dive" zou je ook het Entity Framework boek van Julia Lerman kunnen aanschaffen. Dat gaat echter flink de diepte in op het EF vlak.

Groeten,

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:
Erik Severiens (August 15th, 2013)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Object data source control Rambo BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 3 March 4th, 2009 04:34 PM
Best Data Source Control to Use in ASP.Net 2.0 srotondo ASP.NET 2.0 Basics 2 January 23rd, 2008 05:30 PM
Linked SQL table doesn't show up as control source halfnote5 Access 2 September 3rd, 2007 07:30 PM
Data source control for Create User zoltac007 BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 13 August 16th, 2006 02:34 PM





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