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

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

Selecting a field based on a variable

Posted By:      Posted Date: September 23, 2010    Points: 0   Category :Sql Server

If I have a field name in @FieldName is there a way to do something like

SET @FieldName = 'MyField'

SELECT @FieldName FROM MyTable

where the above would yeild the same results as

SELECT MyField from MyTable

Thanks in advance.

View Complete Post

More Related Resource Links

How to take value of bound field of grid in a variable or in a textbox.


How to take value of bound field of grid in a variable or in a textbox.

 I m using this code in .aspx page for grid.

<asp:GridView ID="Grd_Mprocess" runat="server" AutoGenerateColumns="False"

                      Width="100%" AllowPaging="True"


                      AllowSorting="True" PageSize="5"  >

                 <RowStyle CssClass="odd" />


<asp:BoundField HeaderText="DOC_TYPE_PK" DataField="DOC_TYPE_PK"  Visible="true" >


  <asp:TemplateField HeaderText="DOC TYPE ID" Visible="true">  &nbs

Filtering/selecting rows based on date


I have a gridview, based on thsi datasource:


<asp:SqlDataSource ID="SqlDataSourceAllLoads" runat="server" 
        ConnectionString="<%$ ConnectionStrings:tplatz_dk_dbConnectionString %>"               
        SelectCommand="SELECT DISTINCT DATEADD(day, 0, DATEDIFF(day, 0, tblDelivered.delivered_Date)) AS delivered_Date, tblDelivered.delivered_LoadNo, tblDelivered.delivered_TrailerNo, ISNULL(tblDeliveredInfo.deliveryInfo_FirmaNavn, N'?') AS Expr1 FROM tblDelivered FULL OUTER JOIN tblDeliveredInfo ON tblDelivered.delivered_LoadNo = tblDeliveredInfo.deliveryInfo_LoadNr ORDER BY delivered_Date DESC">

Using a dropdownlist, I can filter (by changing datasource) the results based on customer names in a dropdownlist. This datasource, working perfectly, looks like this:


<asp:SqlDataSource ID="sqldataSourceKundeLoads" runat="server" 
        ConnectionString="<%$ ConnectionStrings:tplatz_dk_dbConnectionString %>" 
        SelectCommand="SELECT  DATEADD(day, 0, DATEDIFF(day, 0, tblDelivered.delivered_Date)) AS delivered_Date, tblDelivered.delivered_LoadNo, tblDelivered.delivered_TrailerNo, ISNULL(tblDeliveredInfo.deliveryInfo_FirmaNavn, N'?&#

Calculated Value based on a Checkbox field

Hi All:         This should be quite straightforward, but for some reason...         I implement a calculated value with this expression: =IF(fieldName="Yes",1,0) and the data type returned from this formula is a number with 0 decimal places...         Something particular about this, is that the fieldName field is a checkbox.         It doesn't work, I only get 0s for all the values (Yes or No).         I tryed again changing to =IF(fieldName="Yes","1","0") and data type returned string, but I get the same result...         Can you realize what I am doing wrong?... It's just that I see it pretty well...         Ok, thanks.Acaspita

changing font color on a data column field based on the contents

I am trying to change font color if the datafield has the string EMERGENCY. This is my original template field: <asp:TemplateField HeaderText="Type" SortExpression="DigType"> <EditItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Eval("DigType") %>' ></asp:Label> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Eval("DigType") %>' ></asp:Label> </ItemTemplate> </asp:TemplateField>   and this is what I thought would work: <EditItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Eval("DigType") %>' ForeColor='<%# IIf(Eval("DigType")="EMERGENCY","Black", "Red") %>'></asp:Label> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Eval("DigType") %>' ForeColor='<%# IIf(Eval(&quo

create multiple columsn based on a passed in variable

Hi, I am clueless as to how the following would be done. Would appreciate imputs: i would be passed in a StartDate and EndDate along with a Period(Week,Month,Quarter,Year).  this would be an INT which is calculated in the following way -- Suppose he selects StartDate = 2010-01-01, EndDate = 2010-02-28, and Period = Weeks ---> Which would mean my resultset would have a additional 9 columns (9 weeks using DateDiff(Week,@startdate,@enddate) The number of fixed columns always will be 4 Total # of records that would always come back is > 10 something like this CategoryTypeId(int),ParentCategory(uniqueidentifier),Id(uniqueidentifier),Name nvarchar(50),Wk1,Wk2.....Wk9 1,xxxx,xxxx,'Shopping',650,300,1275....1000   (i dont't need to total the wk,month,qurter or yearly.This would be taken care on the front end) Thanks!

Auto-populating a field based on SharePoint Contacts List

Hello! Hopefully a quick question for you gurus :) So, I have two relevant fields in my InfoPath 2007 form: A drop-down list called Contact Name, and a text field called Contact Phone Number.  Contact Name is populated through a SharePoint Contacts List, retrieving their Full Name from that list. What I'd love to have happen is, when the user selects a name from the Contact Name list, have the form auto-populate the Contact Phone Number based on the phone number of the selected name from the SharePoint Contacts List. I tried to set a rule in Contact Name to, when the field is Not Blank, populate the Contact Phone Number with the phone number from the Contacts List, but all that does is populate the field with the top-most phone number, not the one matching the selected name. Any way to do this more accurately, and preferably without custom code? (Client has an explicit requirement to not have any custom code on the form or its associated workflow)   Thank you so much!

Lists - wrong field is nameing variable

I have created a list of applications which are installable via SCCM and the list is also used to raise new application packaging requests. What I am finding is that the workflow picks up the application developer as the name that is used in workflows, for example, if I raise a request for Microsoft Office Project 2010, the workflow comes back with "Please review Microsoft". How can I persuade the list and the workflow that it is the application name field I want brought back and not the company name? Regards Mark

Dynamically display images based on a variable - JavaScript

I am trying to bring in a specific image based on a variable brought into the page with a querystring. I initially thought having an array with the image's URL in it would work, but I am having problems associating the array index with the variable. Any help with this would be greatly appreciated. Dave 

Display Value Based on ID Field in Datalist


This should be very simple but I guess my mind is even more simple.

I am trying to display a starting date from an access database based on the dateId field in a datalist.

On page 1 (using a master page) the customer selects a starting date from a dropdownlist. When the customer hits the submit button, they are transferred to page 2 where the selected starting date is displayed.

The database table is basically:

DateID Startingdate

1             1/1/2010

2             2/1/2010

3             3/1/2010

4             4/1/2010

5             5/1/2010

The dropdownlist from page 1 is:

<asp:DropDownList class="formtextbox" ID="DrpStartCov1" runat="server" 
                DataTextField="Date" DataValueField="DateID">

My select command is:
SelectCommand="SELECT [NextMonth], [DateID] FROM [List]"
As I mentioned

Excel 2007 Cubeset function needs to pull list based on part of field value (ie contains, wildcardin


I have a Cube with a field called 'Item Code'.  This field is six characters in length.  The first two characters differentiate what kind of product it is.  I am building my reports with cube functions.  I do not want to use a pivot table to retrieve the list by using the 'contains' or 'begins with' label filters.  Is there a way to use the cubeset function with left, mid, right functions?  I realize that it can be set up in the cube as a separate field, but I am not able to update the cube.  Below are my examples - 'All Products' and 'Product R3189S' work fine, but I need to retrieve 'Products starting with R3'.

=CUBESET("Financials Sales Cube", "[Product Dimension].[Item Code].[All Product Dimension].children", "All Products")

=CUBESET("Financials Sales Cube", "[Product Dimension].[Item Code].[All Product Dimension].[R3189S]", "Product R3189S")

= CUBESET("Financials Sales Cube", "[Product Dimension].[Item Code].[All Product Dimension].[R3****]", "Products Starting with R3")

Thank you in advance.


how to change the view of SharePoint List based on field?


hi all,

I have different sites in my sharepoint application. in every site i have one list, this list have one field called location. When i open this list in purticular site at the time users  will see only the items where the location filed is equal to the site name.

So please suggest me how we can achieve this?



TreeView node visually unselected after selecting tree node and then clicking soem other field



I am using .NET 3.5 (VS 2008).  I have a SplitContainer with a TreeView on one side and a form with various fields, tabs, etc on the other.  When I select one of the tree nodes, the node is highlighted.  The node loses its highlighting when the user clicks on one of the tabs or enters a field on the other side of the SplitContainer.  Although no highlighting is visible, the treeView.SelectNode() still returns the proper node, so it appears that the tree node is still selected, just not visually highlighted.  The problem is that without the highlighting to distinguish which node is selected, the user can easily get lost.  Is there a build-in mechanism to maintain some sort of highlighting on the selected node when the cursor is moved to another form.  For example, in Windows Explorer, if you select a tree node, the node is highlighted in a dark blue.  If you were to then place the cursor in the Address box, the selected node changes to have a beige color.  This still shows which node was selected.

Is there a built-in solution (ie some property to set on the tree view or nodes) that would help fix my issue or do I have to build my own solution to mimic something like what Windows Explorer does?  I wanted to avoid having to put event handlers on all tabs and form fields that would be used to &

QueryExtender Control - Variable field name to search against


FrownI have been alerted to the Query Extender control through previous posts, but I am still seeking the final part of my desired solution.

NB the following is written in conceptual English (programming specification) format, rather than actual asp code.

While I fully understand that I can use the QueryExtender control to pass a parameter from one web page to another, I do not see, and maybe this is not even possible, how I can pass a parameter of which field I want to select on.

Ideally I would like to be able to implement a query which is

SELECT tiles where 'Passed Variable Name' contains 'Passed Test Value'

such as would then generate, for example -

SELECT titles where 'Series Available' contains 'A'


SELECT titles where 'Authors' contains 'Wyndham'

but I just can't work out how I can implement the varying search field.

Is what I am seeking for possible? OR

Am I going to be forced to implement an approach such as the following?

SELECT Calling Web Page

CASE "Series" 

SELECT titles where 'Series Available' contains 'passed variable'

CASE "Authors"

Combine multiple xml elements into one column field based on subheading


I need to pull down data from a third party vendor's SQL view, where one of the columns is xml. I've never worked with xml before; unfortunately our vendor was not willing to change the format to what they were previously providing.

XML Example:

<ResponseDTO xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

Selecting record based on domain url and email comparison


I want to limit the selecting of data records to only the user who created the record, OR a users who's email address belongs to the domain of the record being edited.
So, if the producer has a value in the URL of "www.mydomain.com" and their email address in the Membership is "someone@mydomain.com" they will be able to see the record.

I have the following table:
Table -  Producer
Field: ProducerID
Field: ItemUserID
Field: URL

I have figured out how to select based on the ItemUserID (see code below), but I don't know how to compair the email address values of URL and the Membership.

protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
      if (Page.User.Identity.IsAuthenticated)
     //get id of user logged in
     e.Command.Parameters["@ItemUserID"].Value = Membership.GetUser().ProviderUserKey;

Can someone show me how to code my requirements?

selecting a value of one column based on the max value of another in a subquery


I would like to select the value of column c  that is based on the max of column b

select q.a,q.c

(select a,max(b) as mx,c

from  a table group by a,c)q

when I try to select the row I get multiple rows

I need to return one row that corresponds to the max in col b

InfoPath -Validate a Field Based on ANY change in a Secondary Field


I would like to require a change to one field if a secondary dropdown field is changed at all.  At Minimum I would like to change the formatting or color of the field in question.


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