.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Post New Web Links

Confusion on retriving single value from table

Posted By:      Posted Date: September 23, 2010    Points: 0   Category :ASP.Net

I am confused when pulling a single value from a table, and although I have a work-around, there would seem to be a more eloquent process that I am missing.

Say I have a table of friends, and I want to pull out a single integer value.  I would think that the following C# code in asp.net (with LINQ) would do the trick.

int i = from p in db.FRIENDS where p.name == "JohnSmith" select age;

If I run the above code though, I throw a cast exception.  Instead, I have to do the following:

var i = from p in db.FRIENDS where p.name == "JohnSmith" select age;
int a;

foreach(FRIENDS j in i)
    a = j.age 

Is there a better way of grabbing just a single record value when I know what the type?  Something that is cleaner to read?

View Complete Post

More Related Resource Links

merging multiple tables in a single dataset to single table


 i have a stored procedure which returns three tables to a dataset ..... now i need to merge all three tables to a single table from d same dataset 

like dataset1 has table1 table2 and table3 .... i want all the three tabels to be merged into dataset1 itself .... instead of three diffrent tables so that i can show all three table data in a single datagrid  as a compact data and combination of 3 tables from d single dataset.....

can some1 help me please.....

no of rows in single table

hi friends...i need one count...one sql table how many rows able to create or write in sql server 2005

Tricky SELECT query from a Single Table

Hi I have a 2 rows of data in a table as mentioned below   Table1             ElementID Month Year Planned Cost UnplannedCost PlannedExpense UnPlannedExpense 4 9 2010 NULL 40 NULL 20 4 9 2010 400 NULL 200 NULL  I need a SELECT query to get the output in a single row as ElementID Month Year Planned Cost UnplannedCost PlannedExpense UnPlannedExpense 4 9 2010 400 40 200 20 Could anybody help me in writing a query for this? Thanks

To Search a word in all the columns of a Single Table

Dear Friends, How can I search a word in all the columns of a table?Please advise.

How to Listen to MSMQ and Database table from a single WCF service

Hi, I have one WCF service which is listenibg to MSMQ. Once message is added to MSMQ, it will start processing the business logic. But my requirement is to make this WCF service to listen to one database table also. If any message is logged to this table or any message added to MSMQ, the WCF service should start processing the business logic. Could you please let me know if it's feasible using WCF service. It would be great if you can tell me the alternate approaches to implement this functionality. Ram.  .NET related discussions

update a single table in edmx file


I'm working on the big project who has edmx file with lots of table. I want to update a table from DB. But when I update the edmx file, it  also refresh the other tables from DB. How do I update single table?

How to take backup of single table in SQL server 2008 and SQL Server Management Studio Environment



I know how to take total database backup in SQL server 2008 and SQL Server Management Studio Environment but i failed to take single table backup in database.

It is possible to take single table backup in Oracle using PL/SQL Developer IDE.

Thanks in Advance..... 

It is possible to alter multiple columns within a single alter table statement?


It is possible to alter multiple columns within a single alter table statement?

I tried & searched not getting it.

Alter table au_de alter column m_user char(9),c_user char(9)

Msg 102, Level 15, State 1, Line 1

Incorrect syntax near ','.


Alter table au_de alter column m_user char(9),alter column c_user char(9)
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ','.

Transforming a multi-row notes table into a single-row notes table


Hi guys,

I'm new to SSIS and trying to design a package that will take a multi-row notes table and concatenate it into a single-row notes table. For example, the source fields may include Customer#, Note#, NoteText, Sequence#. In the current format, a long note may be stored in 2, 3, 4 rows. My SSIS package somehow needs to loop through each row, ordered by Sequence#, and concatenate the NoteText field into a single output field. For example:


Customer# Note# NoteText      Sequence#
--------- ----- --------      ---------
1234   1   Example of a multi 1
1234   1   line note.     2
5555   2   Single line note.  3
6666   3   Example of a multi 4
6666   3   line note that spans5
6666   3   three rows.     6


Needs to output the following:


Customer# Note# NoteText 
--------- ----- --------   
1234   1   Example of a multi line note.
5555   2   Single line note.
6666   3   Example of a multi line note that spans three rows. 


Does anyone know what the best method is to accomplish this? I tried using a Script Task and writing it in VB.NET but I wasn't able to figure out how to access multiple rows of data at once. I don't have much experience writing T-SQL Stored Procedures, but I could learn quickly if this is the best method.

Any suggestions?

using table variable in single quote



I searched over internet but i couldn't find. How can i use table variable in single quote here some of code that i can do and i can't

for example if i can write like this it will work

declare @table table ([name] varchar(20))
insert into @table values('ahmet')
select * from @table
but if i can write like this does not work
exec('select * from ['+@table+']')
I can use any variable for table name like
declare @tableName varchar(20)
set @tableName = 'sampleTable'
exec('select * from ['+@tableName+

cascading drop down lists from single table


my one table consists of fields (id, flying from, flying to). thus i have one drop down for flying from and another drop down for flying to. i'm hoping to cascade them so 2nd drop down values are dependant on the values from 1st drop down. is it possible to do this using a single table? or must i use two tables and link the id's from both? and do i write anyting in the SelectedIndexChange event? thanks...

the sqldatasource code looks like this:

Flying From:

        <asp:DropDownList ID="ddlFlyingFrom" runat="server" DataSourceID="SqlDataSource1"
            DataTextField="FlyingFrom" DataValueField="Id" AutoPostBack="True" OnSelectedIndexChanged="ddlFlyingFrom_SelectedIndexChanged">

        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TestConnectionString %>"
            SelectCommand="SELECT [Id], [FlyingFrom] FROM [Flights]"></asp:SqlDataSource>

Problem filling labels with table data outside datalist using single call to db


On a single page, I'd like to fill 2 hyperlink controls as well as a datalist control with table data using one call to the database. The page looks like this:

<div id="PageContent">    
      Please select the district you wish to view below.</p>
      <asp:DropDownList ID="ddlDistrict" runat="server" CssClass="textbox" 
        DataTextField="DistrictName" DataValueField="DistrictId" AutoPostBack="True" 
    <p><asp:label ID="lblDistrictAbbrev" runat="server" Text="District Abbreviation: " />
      <asp:hyperlink ID="hyperlinkDistrictName" runat="server" 
        Text='' NavigateUrl="~/Contact/NationalCouncil.aspx" 
        Font-Bold="True" />
    <p><asp:label ID="lblDistrictCouncillor" runat="server" Text="District Councillor: " />
      <asp:hyperlink ID="hyperlinkCouncillor" runat="server" 
        Text='' NavigateUrl="~/Contact/NationalCouncil.a

How can I check (true/false) if every single entry in table exists in other table?



I have 2 tables:

- Salesmen with Pk.SalesmenID
- Appointments with Fk.SalesmenID and busy(bit)

How can I check if every salesmen has at least one appointment

Am I on the right way with statement below, though I got syntax problems

(SELECT COUNT(distinct SalesmanID)FROM Appointments)
(SELECT COUNT(SalesmanID) FROM Salesmen)

How can we reindex single table in s sql server 2008?



I have a requirment truncate and reindex the single table,how can we reindex a table?

If we truncate the table,is it necessary to reindex the table(if busines needs).

What is the code for reindex the table in sqlserver2008?




How to bind data from linq to html table without a single line of loop.




This is the above code i have worked using querysing and displaying the listitem with in  a simple html table. Here I am using foreach statement to bind items. How to bind the data to a simple html table using SharePoint linq without  a single line of loop.

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Linq;
using Microsoft.SharePoint.Linq;

namespace QuerystringProgramWithoutLoop.QuerystringProgramWithoutLoop
    public class QuerystringProgramWithoutLoop : WebPart
        string strTitle=string.Empty;
        string strStatus=string.Empty;
        protected override void Render(HtmlTextWriter writer)

is ms sql table i have more then Two Lakhs Record in single table how to get fast



I have two table called Invoice_Register and Invoice_Description..

In Invoice_Description table i have some more then two lakhs record, as per ms sql how much time to take to execute this...? coz im getting very late.. note:- I used Store Procedure also..

So let me know the best way in which i load all the record within some 15 sec.. 

Want to show all rows from one table but only those relating to a single week in the other

This must be a simple one and I am just having a block
I have two tables, Personnel and working details.
I want to show all staff working in a single building but only the working details for a specific week
I have added a join which should list all staff but won't because of the date filter in the working detail table it returns only an exact match, I have added ' or is null' for the date filters but just returns all staff from all buildings
SELECT        [8977].cTblPersonnel.Firstn, [8977].cTblPersonnel.Surname, [8977].cTblWagesFcst.StartDate
FROM            [8977].cTblWagesFcst RIGHT OUTER JOIN
                         [8977].cTblPersonnel ON [8977].cTblWagesFcst.PersonnelID = [8977].cTblPersonnel.PersonnelID
WHERE        ([8977].cTblPersonnel.UnitID =544) 
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend