Tuesday, March 20, 2012

Referencing sql in IF THEN ELSE Statement

The Background:

From page 1 a search is created and this value is converted to a querystring and given the name "id=" and passed to page 2. Page 2 then does a lookup in the sql 2000 db useing this querystring.

<

asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ ConnectionStrings:ListingDBConnectionString %>"SelectCommand="SELECT [ID], [CompName], [Package] FROM [CDetails] WHERE ([ID] = @.ID)"><SelectParameters><asp:QueryStringParameterName="ID"QueryStringField="id"Type="Int32"/></SelectParameters></asp:SqlDataSource>

The Problem:

What I then need to do is an IF THEN ELSE statement.

IF [Package in SQL1] = 4 Then

Package 4

IF [Package in SQL1] = 3 Then

Package 3

Only how do I reference the sql value in the if statement.

I have tried

<%IF Eval("Package") = 4 Then %> and with Bind - No good

Hi asiddle,

If you want to proceed IF then in SQL statement, I suggest you use CASE...WHEN...

You can first put the SelectCommand into a stored procedure, then use CASE...WHEN... to do the switch.

For more information, please check the following link:

http://msdn2.microsoft.com/en-us/library/ms181765.aspx

HTH. If this does not answer your question, please feel free to mark the post as Not Answered and reply. Thank you!

|||

Hello Kevin,

Many thanks for your reply. Unfortunately I cant make head nor tails of it. I followed your link to msdn but as usual all its actually done is confused the hell out of me. Is it possible you could drop an example?

Many thanks

|||

Hi asiddle,

I suggest you put the CASE...WHEN into a stored procedure, and take @.ID as parameter.

For example:

SELECT [ID], [CompName], [Package] FROM [CDetails] WHERE [ID] =
CASE
WHEN @.ID>1 Then XXXX
WHEN XXXX Then XXXX

|||

[Package] is an integer field ?

SELECT [ID], [CompName], 'Package ' + convert(varchar(10), [Package]) as [Package_Desc]

FROM [CDetails] WHERE ([ID] = @.ID

No comments:

Post a Comment