473,811 Members | 3,151 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple LINQ problem

Hi all

Having a mental block with linq!
I have a simple aspx page with a button and a label. When the button is
clicked I want the label to display the result of a simple query:
i.e. Select name from MyTable where id = 1
This should return ‘peter’ - simplicity using sql

I'm trying to do this with linq and getting nowhere (converting to string
error)
here's what I've got:

Button click event:

Dim db as New myTableDataCont ext
Dim name = From n In db.myTable Where n.Id = 1 Select n

Label1.text = name

Where am I going wrong?
Any pointers or tutorials would be great (vb.net)

Sep 25 '08 #1
3 942
James Page wrote:
Hi all

Having a mental block with linq!
I have a simple aspx page with a button and a label. When the button is
clicked I want the label to display the result of a simple query:
i.e. Select name from MyTable where id = 1
This should return ‘peter’ - simplicity using sql

I'm trying to do this with linq and getting nowhere (converting to string
error)
here's what I've got:

Button click event:

Dim db as New myTableDataCont ext
Dim name = From n In db.myTable Where n.Id = 1 Select n

Label1.text = name

Where am I going wrong?
Any pointers or tutorials would be great (vb.net)
James,

Your query will return a collection of strings. The query has no way
to know that only one string will be returned. So when you attempt to
assign "name" to the text property it does not know what to do.

If Id is the key of the table then change your code to the following
(just typed in so there may be problems but you will get the point)
Dim db as New myTableDataCont ext
Dim name as string = (From n In db.myTable Where n.Id = 1 Select n).single

Label1.text = name

LS
Sep 25 '08 #2
James Page wrote:
I have a simple aspx page with a button and a label. When the button is
clicked I want the label to display the result of a simple query:
i.e. Select name from MyTable where id = 1
This should return ‘peter’ - simplicity using sql

I'm trying to do this with linq and getting nowhere (converting to string
error)
here's what I've got:

Button click event:

Dim db as New myTableDataCont ext
Dim name = From n In db.myTable Where n.Id = 1 Select n
Dim name As String = (From row In db.myTable Where row.Id = 1 Select
row.Name).First OrDefault()
Label1.text = name


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Sep 25 '08 #3
Thanks guys.

Do you have any links to some tutorials for LINQ other than the ones on
asp.net or MSDN?
Sep 25 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1754
by: David Veeneman | last post by:
I've been hearing a lot about LINQ in connection with Orcas, the next release of VS.NET. Micorosoft touts LINQ as the Next Big Breakthrough, but it looks to me like further muddying of application layering. I use an object-oriented development approach, and I don't see how LINQ can be integrated. Am I missing something? Are there any good articles on using LINQ within OO development? Or is that, like 'army intelligence', an oxymoron?...
4
549
by: BeSharp | last post by:
I recently stumbled across a pretty interesting LINQ to SQL question and wonder, whether anybody might have an answer. (I'm doing quite some increasing LINQ evangelism down here in Germany.). Assume I want to select rows from a database and check whether a specific column contains keywords from a list of keywords. The following works just fine: List<stringsearchTerms = new List<string>() { "Maria", "Pedro" };
22
10389
by: paululvinius | last post by:
Hi! Testing som Linq-expressions and tried to measure performance and compare it to pre-Linq programming. The folloing two methods are functional equal but the non-Linq one is twice as fast. public List<ConferenceRoomOldWay(int minimumSeatingCapacity) {
6
1556
by: Dmitry Perets | last post by:
Hello, I am trying to work with MS SQL Server 7 from the release version of Visual Studio 2008 + LINQ to SQL. And the problem is that the LINQ to SQL designer doesn't accept my tables saying that my connection provider is unsupported. Then I found out that LINQ to SQL officially supports only .NET Provider for SQL Server. The problem is that SQL Server 7 can't work with this provider, so I have to use the .NET Provider for OLE DB. Well,...
3
2907
by: Leo Seccia | last post by:
Hello everyone, I have a c# project with a sql server database. I have a number of lookup tables in my database which I successfully managed to import into my LINQ dataclasses. eg. Table: tlkpColor (PK) tlkpColorID
9
2506
by: =?Utf-8?B?cmF1bGF2aQ==?= | last post by:
Hi all: after reading different places/sites about linq... I ran into these questions: 1. What framework do we need to run linq ? (does it depend on what version of visual studio we have?) how about vs2008? is it different name space or framework for linq xml or linq sql? ( 2. do we need to have references to what linq's dlls. or namespaces? system core? 3. what name spaces are needed?
4
1982
by: Jacek Jurkowski | last post by:
Why is it so slow? I really like that queries but using DataReader i have done my task's much more faster than ising LINQ...
4
2189
by: =?Utf-8?B?RXJpYyBGYWxza2Vu?= | last post by:
We’re storing our main entity in an insert only table which stores the history of past revisions, but we’re facing problems with storing this history as LINQ will only update the entity, and not reinsert it with a different revision number. Compounding the issue, we’ve also got an associated table storing properties for our entities which is not revisioned, but we still want changes to the children of our entity (additions, changes...
7
11604
by: shapper | last post by:
Hello, Is it possible to multiply all Prices in a List<Productby 1.1 using Linq? Product has a property named Price. Thanks, Miguel
0
10653
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10395
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10408
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10137
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7674
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6895
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5564
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3876
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3027
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.