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

August 15th, 2013, 08:45 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
>> 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
|
|

August 15th, 2013, 09:43 AM
|
|
Authorized User
|
|
Join Date: May 2013
Posts: 12
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
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.
|
|

August 15th, 2013, 11:12 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

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

August 15th, 2013, 01:43 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

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

August 15th, 2013, 02:21 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|
 |
|