Showing posts with label members. Show all posts
Showing posts with label members. Show all posts

Sunday, February 19, 2012

How to use a variable in a SQL statement…

Hi,

background

From a 'members' table, I get the first letter of the last name of the members and display in a dataList as buttons.

In the dataList control, the

<asp:button>
has the following:
CommandArgument='<%# DataBinder.Eval(...) %>' OnCommand="doSomething"

In the code behind page I have the following code:


Public Sub doSomething(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs)
Dim getLastName as string = e.CommandArgument.ToString()
lblBtn.Text = "clicked " + e.CommandArgument.ToString()


This code will do the job – in the label 'lblBtn' the text will show the 'value' that the button represents – i.e. if you click the button for 'B' the lblBtn will read 'clicked B'

The problem

Now that I know which button was clicked, I need to use the information in a SQL query to retrieve the relevant data.


Const strSQL2 As String = "select * from members where letter = 'getLastName'"

This query returns no records at all – no errors, just nothing. What I am doing wrong?

Kindly help.

Thanks,

Sami.a couple of questions:
1> Is the first letter of the last name stored in Members under field letter?
2> Is the following statement used in doSomething?


Const strSQL2 As String = "select * from members where letter = 'getLastName'"

If the answer to both questions are YES. Here is your correct strSQL2:


Const strSQL2 As String = "select * from members where letter = '" & getLastName & "'"
|||Thanks for your response – yes, you are right on both assumptions. I did what you suggested but got an error.

In VS the expression 'getLastName' got a blue underline and when I mouse over it I get a balloon message that says 'Constant expression is required'

When I 'view in browser' and click 'yes' for continuing with the preview and then click on one of the letter buttons I get an error page with the following message:

Incorrect syntax near '='

Any suggestions?

Thanks for your patience - I am very new at this

Sami.|||Sorry. My fault. Overlooked your statement. Please remove "Const " from your statement below.


Const strSQL2 As String = "select * from members where letter = 'getLastName'"


Change to this:


strSQL2 As String = "select * from members where letter = 'getLastName'"

Please let me know if there is still error.|||Wow - thank you so much...

It works great.

Sami.

How to use a parameter in Calculated Member

I have an MDX query that has two calculated members that use dates.

Currently these dates are "hardcoded" in the calculated member definition.

I want to change this to use input parameters, thereby allowing the user to set the dates. I will then deploy the .rdl file to the Report Server. I have found no examples of using parameters in calculated members, so I am wondering if this is a bad idea.

I have explored the "filter wizard" in Visual Studio, and I am only offered the opportunity to filter on the fields that appear in the report.

I have made several attempts to alter the statement within Visual Studio 2005 while having the MDX statement in "Design Mode", but as soon as I attempt to execute the statement (or change to another view, such as "Preview", Visual Studio tells me that the "query cannot be retrieved from the query builder" and then continues with the (older) most valid query, which does not have reference to the parameter. (This is a Reporting Services project).


Here is a statement with hard coded dates that works properly.


WITH

MEMBER [Measures].[Completed Work On Period Start] AS

(

[Date].[Year Month Date].[Date].&[2006-04-30T00:00:00],

[Measures].[Microsoft_VSTS_Scheduling_CompletedWork]

)

MEMBER [Measures].[Completed Work On Period End] AS

(

[Date].[Year Month Date].[Date].&[2006-05-06T00:00:00],

[Measures].[Microsoft_VSTS_Scheduling_CompletedWork]

)

MEMBER [Measures].[Completed_Work] AS

(

[Measures].[Completed Work On Period End] - [Measures].[Completed Work On Period Start]

)

SELECT

NON EMPTY

(

FILTER

(

NONEMPTYCROSSJOIN

(


[Assigned To].[Person].[Person],

[Work Item].[System_Id].[System_Id],

[Work Item].[System_Title].[System_Title],

[Work Item].[System_WorkItemType].[System_WorkItemType],

[Measures].[Current Work Item Count],

4

),

[Measures].[Completed_Work] > 0

)

)

ON ROWS,

NON EMPTY

{

[Measures].[Work Item Url],

[Measures].[Completed_Work]

}

ON COLUMNS

FROM

[Team System]

You need to be in the MDX view to hand author the query to do this.

- In the Query Designer, switch to MDX view

- Change the query to be a parameterised query, e.g. replacing the date with StrToSet(@.Date, CONSTRAINED)

- Select the "Query Parameters" option from the toolbar, to define the parameter. You must provide a default value, that will be used when previewing/preparing the query

|||

Following Paul's guidance, I was able to edit the query without VS flagging the query as invalid.

BTW: I used StrToMember instead of StrToSet, since my parameter was in the Members section. The results of the query are valid.

WITH

MEMBER [Measures].[Completed Work On Period Start] AS

(

(STRTOMember(@.prmStartDate)),

[Measures].[Microsoft_VSTS_Scheduling_CompletedWork]

)

MEMBER [Measures].[Completed Work On Period End] AS

(

(STRTOMember(@.prmEndDate)),

[Measures].[Microsoft_VSTS_Scheduling_CompletedWork]

)

MEMBER [Measures].[Completed_Work] AS

(

[Measures].[Completed Work On Period End] - [Measures].[Completed Work On Period Start]

)

SELECT

NON EMPTY

(

FILTER

(

NONEMPTYCROSSJOIN

(


[Assigned To].[Person].[Person],

[Work Item].[System_Id].[System_Id],

[Work Item].[System_Title].[System_Title],

[Work Item].[System_WorkItemType].[System_WorkItemType],

[Measures].[Current Work Item Count],

4

),

[Measures].[Completed_Work] > 0

)

)

ON ROWS,

NON EMPTY

{

[Measures].[Work Item Url],

[Measures].[Completed_Work]

}

ON COLUMNS

FROM

[Team System]