Retrieve single value from Database, much like DLookup() in MS Access

I am exploring Silverlight (C#) and SQLServer as a next evolution for our current (slow) Access database. So far everything has been great, using DomainServices to retrieve the data I need. In our database we have a table (Supervisors) with Supervisor_ID, Supervisor_FirstName, Supervisor_LastName and many other fields.

What I want to do is recreate a function I use in my current database called EntityNameFirstLast(EntityID) which would take an integer. I could then retrieve the value of [Supervisor_FirstName] from [Supervisors] table where [Supervisor_ID] == EntityID using the following: FirstName = DLookup("[Supervisor_FirstName]", "Supervisors", "[Supervisor_ID] = EntityID

I would do the same for lastname and combine the strings returning one string with First and last name.

How can I get just a single value from my database through my DomainService (or any way for that matter)? I understand that IQueryable GetSupervisorByID(Int SupID) will return the entire row that I need, but how can I get a specific field from that row?

I am also aware that I can set the DomainDataSource in my XAML and then bind to the data I want, but I am curious if what I asked above is doable or not.


There are number of ways you can accomplish your requirement if what you need is a single value from MS-SQL server:

1.Use a Query to do the concatenation and then use its output in your code

Select Supervisor_FirstName + ' ' + Supervisor_LastName as Supervisor_FullName From Supervisors Where Supervisor_ID = EntityID

Now you can get the above query to execute through a SqlCommand and get the part thats interesting to you

private string GetSupervisorFullName(string entityID, string connectionString) {
    string query = "Select Supervisor_FirstName + ' ' + Supervisor_LastName as Supervisor_FullName From Supervisors Where Supervisor_ID = @EntityID";
    string supervisorFullname = "";

    using(SqlConnection con = new SqlConnection(connectionString)) {
        SqlCommand cmdSupervisorFullname = new SqlCommand();
        cmdSupervisorFullname.Connection = con;
        cmdSupervisorFullname.CommandText = query;
        cmdSupervisorFullname.CommandType = CommandType.Text;

        SqlParameter paraEntityID = new SqlParameter();
        paraEntityID.ParameterName = "@EntityID";
        paraEntityID.SqlDbType = SqlDbType.NVarChar;
        paraEntityID.Direction = ParameterDirection.Input;
        paraEntityID.Value = entityID;


        try {
            supervisorFullname = (String) cmdSupervisorFullname.ExecuteScalar();
        } catch(Exception ex) {

        return supervisorFullname;

2.Second way would be create a Scalar function in the SQL for your requirement and then access that function using the same kind of method as mentioned above.

Then finally you would take the return value from your method GetSupervisorFullName and populate any control value of your choice.

Please do note that there are again other methods of doing the same with LINQtoSQL or with any other ORM tools. The above 2 methods are the basic way of accomplishing them.

Hope that helps.

Need Your Help

ASP.NET MVC 2 System.Web.Mvc.dll conflict dll conflict

I have installed the ASP.NET MVC 2 RC and opened the default "example project", but get the following error:

Is there a CMS that will integrate alongside existing tools (maybe as a control inside a page) in .net webforms content-management-system

I'm looking for a lightweight solution that will let me spread out the responsibility for content amongst various departments. This sounds a lot like a CMS. However, initial impressions suggest tha...