CodeCharge Studio All ASP PHP Perl ColdFusion Java C#.NET VB.NET

Lookup Single Field Value in a Database

This example shows how to read a single value from a database. In this case, we have a Header page which contains a UserName Label that is used to display the user's name.

Method 1

Use the DLookup action.

  1. Add the DLookup action to the Before Show event of the page.
  2. Set the properties of the action.
    Note: The values of the Expression, Domain and Criteria properties should not be entered in double quotes.

Method 2

  1. Add the Before Show event to the page.
  2. Within the event, add the code below:

ASP

Function Header_BeforeShow(Sender) 

  Header.UserName.Value = CCDLookUp("user_name","users","user_id=" & DBConnection1.ToSQL(CCGetUserID(),ccsInteger), DBConnection1) 

End Function

PHP

function Header_BeforeShow(& $sender) { 
global $Header;
global $DBConnection1;

  $Header->UserName->SetValue(CCDLookUp("user_name","users","user_id=". $DBConnection1->ToSQL(CCGetUserID(), ccsInteger), $DBConnection1) );

}

Perl

sub Header_BeforeShow() { 

  $DBConnection1 = clsDBConnection1->new();
  $result = CCDLookUp("first_name", "users", "user_id=". $DBConnection1->ToSQL(CCGetUserID(), $ccsInteger), $DBConnection1);
  $Header->{UserName}->SetValue($result);

}

ColdFusion

<!--Header_BeforeShow --->

  <CFModule Template="CCDLookUp.cfm" table="Users" field="first_name" where="User_ID=1" Connection="Connection1">
  <CFSET fldUserName=CCDLookup>

VB.Net

'Header_BeforeShow

  UserName.Text = (New TextField("",Settings.Connection1DataAccessObject.ExecuteScalar("SELECT user_name FROM users WHERE user_id="&DBUtility.UserId))).GetFormattedValue("")

C#

//Header_BeforeShow

  UserName.Text = (new TextField("",Settings.Connection1DataAccessObject.ExecuteScalar("SELECT user_name FROM users WHERE user_id=" + DBUtility.UserId))).GetFormattedValue("");

Java

//Header_BeforeShow

   e.getPage().getControl("UserName").setValue( DBTools.dLookUp("User_Name","Users","User_ID="+Utils.getUserId(e.getPage()),"Connection1"));

See Also:

Before Show event, How to read multiple values from a database


On-line, printable versions and updates