Showing posts with label web. Show all posts
Showing posts with label web. Show all posts

Monday, March 19, 2012

Group newbie: question on parsing a field value

I have a text field (called TreeNode) that contains node identifiers for a
dynamic tree on a web page. For example:

"1.1"
"1.2"
"1.2.1"
"1.2.2"
--
--
'1.2.10"
--
etc..

What I need to do is compare this field "value" to another value in a query.
(I'm using ASP and VBScript to create the statement). For example:

sql = "SELECT SomeField FROM MyTable WHERE TreeNode>=' " & MyNode & " ' "

The problem I run into is when the TreeNode value is say "1.2.10", and it's
being compared against "1.2.1" and "1.2.2". It should be greater than both
of these (in my implementation of this), but it actually falls between the
two, since it is a text comparison.

All I'm really interested in is the last value. But I can't use the RIGHT
function, because the last value may be one or more digits, and there could
be any number of levels (periods).

Is there an SQL function I could use that would strip away everything but
the text following the last period? I could then easily do the same in the
ASP script and compare integers. Something like:

sql = "SELECT SomeField FROM MyTable WHERE GetLastValueSQL(TreeNode) >= " &
GetLastValueASP(MyTreeNode)

This also needs to work in MS Access BTW :-)

Thanks in advance for any help!

Calan

AxMaster Guitar Software
www.jcsautomation.com
www.jcsautomation.com/music.asp
Music software and web design/hosting

"Reality exists only in the minds of the extremely deranged""Calan" <calan_svcREMOVE@.yaNOSPAMhoo.com> wrote in message news:<P3Jcc.643$FB1.182@.fe25.usenetserver.com>...
> I have a text field (called TreeNode) that contains node identifiers for a
> dynamic tree on a web page. For example:
> "1.1"
> "1.2"
> "1.2.1"
> "1.2.2"
> --
> --
> '1.2.10"
> --
> etc..
> What I need to do is compare this field "value" to another value in a query.
> (I'm using ASP and VBScript to create the statement). For example:
> sql = "SELECT SomeField FROM MyTable WHERE TreeNode>=' " & MyNode & " ' "
> The problem I run into is when the TreeNode value is say "1.2.10", and it's
> being compared against "1.2.1" and "1.2.2". It should be greater than both
> of these (in my implementation of this), but it actually falls between the
> two, since it is a text comparison.
> All I'm really interested in is the last value. But I can't use the RIGHT
> function, because the last value may be one or more digits, and there could
> be any number of levels (periods).
> Is there an SQL function I could use that would strip away everything but
> the text following the last period? I could then easily do the same in the
> ASP script and compare integers. Something like:
> sql = "SELECT SomeField FROM MyTable WHERE GetLastValueSQL(TreeNode) >= " &
> GetLastValueASP(MyTreeNode)
> This also needs to work in MS Access BTW :-)
> Thanks in advance for any help!
> Calan
> AxMaster Guitar Software
> www.jcsautomation.com
> www.jcsautomation.com/music.asp
> Music software and web design/hosting
> "Reality exists only in the minds of the extremely deranged"

This is one way:

declare @.node varchar(50)
set @.node = '1.2.1.10'

select substring(
@.node,
len(@.node) - charindex('.', reverse(@.node))+2,
charindex('.', reverse(@.node))
)

Or this may be easier to read:

select reverse(left(reverse(@.node), charindex('.', reverse(@.node))-1))

You could put this into a function (in SQL2000), but it would be
invoked once per row in queries, so using a stored procedure is
probably a better approach.

Simon|||> Is there an SQL function I could use that would strip away everything but
> the text following the last period? I could then easily do the same in the
> ASP script and compare integers. Something like:
> This also needs to work in MS Access BTW :-)

Hi,

I don't know anything about Access. You can use a table of numbers
trick to parse your "node" into individual nodes by using the period
as a delimiter. Erland has documented it in his site. In the code
below, I called my table of numbers TALLY which has one column ID with
values from 1,2,3,... to 8000.

declare @.node varchar (50)
set @.node='1.2.10'

SELECT
substring(phrase,s,(e-s-1)) as NODES
FROM
(
SELECT
id,
phrase,
charindex('.','.'+phrase+'.',id) as s,
charindex('.','.'+phrase+'.',id+1) as e
FROM tally,(select phrase=@.node) A
WHERE charindex('.','.'+phrase+'.',id) <
charindex('.','.'+phrase+'.',id+1)
) B

OUTPUTS:
NODES
----------------
1
2
10

Further modifying it to output the last NODE piece:
SELECT
substring(phrase,s,(e-s-1)) as NODES, identity(int,1,1) as i
INTO #T
FROM
(
SELECT
id,
phrase,
charindex('.','.'+phrase+'.',id) as s,
charindex('.','.'+phrase+'.',id+1) as e
FROM tally,(select phrase=@.node) A
WHERE charindex('.','.'+phrase+'.',id) <
charindex('.','.'+phrase+'.',id+1)
) B

SELECT NODES FROM #T WHERE i=(SELECT max(i) as i FROM #T)

OUTPUTS:
NODES
----------------
10

Sunday, February 19, 2012

Gridview & SQL data source

I have a few Stored procedures that return values, some need parameters passing and others don't.

Up to now to access that data in a Web App I have called this procedure using VB after seting up a data block etc.

However I notice that the Web Developer express edition has some tools that look like they should help. I have used the in-built tools to create a SQL data source linked to a details view. I then specified that this should connect to my database (sql2005 hosted) and then specified Custom SQL or Stored procedure.

From the drop down I can then select the SP that purely returns values. When I try to test this query I get an error to say that the query did not return any data tables.

Is there a way to get values returned from a SP in this way?

Regards

Clive

You can use events like (Updating, Updated, Selecting, Selected etc) to do your cutomization. For example, if your method returns an output parameter then:

Sub OnDSUpdatedHandler(ByVal sourceAs Object,ByVal eAs SqlDataSourceStatusEventArgs)If e.AffectedRows > 0Then' Perform any additional processing, such as setting a status label. Label1.Text = Request.LogonUserIdentity.Name & _" changed user information sucessfully!" dim outputvalue asString outputvalue = e.Command.Parameters("yourparamname").Value.ToString()Else Label1.Text ="No data updated!"End If End Sub'OnDSUpdatedHandler

As you can see, I haven't done any kind of error checking. Please ensure that your code does. If you need more help, reply to the post explaining the problem.

|||

I think my problem is earlier than this stage to be honest.

For instance I have a SQL 2005 Stored Procedure that returns values from a table I can run this in SQL server itself and it works fine.

When definign this SP as a datasource using the detailsview object in Visual Web Developer I get the error I mentioend above when runnign the test option while I am stil in the wizard. So it looks liek some problem with callign the SP, yet it works OK natively.

|||

DetailsView, FormView, Gridview etc controls are designed to show a record/records. So, if you are using a SqlDataSource that just returns a single value, you cannot bind that to abovementioned controls. Can you explain why do you need to have a Detailsview that shows only single value?

Whenever asking a question, please add as much info as possible. Otherwise, we have to guess other parts (as you can see from my previous post) which is obviously not what you are looking for. Provide more info about your problem/scenario.

|||

I have a stored procedure that takes no paramteres but returns some values which are calculated from a number of tables to returns some summary results. if I execute the SP from SQL manager I get 5 values returned which appear as output parameters.

I was hoping to be able to link these quickly and easily into a web page using a SQL data source linking to this SP as it's datasource.

I was hoping that the details view would then display the values returned.

There maybe an easier or better way to do this. I am a beginner with all this, and wanted to explore how to use a stored procedure as a datasource.

Regards

Clive

|||

If the stored procedure returns resultset (table with columns and rows) then it would show nicely in the DetailsView, GridView etc. I don't think these controls have builtin ability to look for output parameters and bind those values. I think you should do this programmatically.

Connect to db, run the storedproc, get the results. Then you can either manually populate the values in controls or you can create a collection of values and bind it to a dataaware control. If you need sample code, then reply to the post mentioning your preference.

Grid view-cant update or delete

I put a grid view on a web form ,when I run it -the SELECT, EDIT works

the UPDATE,DELETE makes an error although I use the sama data,I added the error :

Anyone can help?

Server Error in '/CrystalReportsWebSite1' Application.

The data types text and nvarchar are incompatible in the equal to operator.

Description:An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details:System.Data.SqlClient.SqlException: The data types text and nvarchar are incompatible in the equal to operator.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.


Stack Trace:

[SqlException (0x80131904): The data types text and nvarchar are incompatible in the equal to operator.] System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +95 System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +82 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +346 System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +3244 System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +186 System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +1121 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +334 System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) +407 System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +149 System.Web.UI.WebControls.SqlDataSourceView.ExecuteDbCommand(DbCommand command, DataSourceOperation operation) +493 System.Web.UI.WebControls.SqlDataSourceView.ExecuteUpdate(IDictionary keys, IDictionary values, IDictionary oldValues) +915 System.Web.UI.DataSourceView.Update(IDictionary keys, IDictionary values, IDictionary oldValues, DataSourceViewOperationCallback callback) +179 System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation) +1140

Hey,

What does those update/delete stored procedures look like? It seems like it may be an issue with the query.

|||I'm guessing he has a text field, and he told it to use optimistic concurrency or (CompareAllValues), which doesn't work with text fields.