Wednesday, March 9, 2011

Find Specific String or Char Count occur in a string

For getting The String count I have created three methods might be this will helpful to you.

in SQl server, we dont have any function which can return the number of words in a perticular string .but from small tricks we can do that . now Consider here we can separating the character on space basis

example :


DECLARE @String VARCHAR(4000)

SET @String = 'WelCome to D Murli Blog Spot.'

SELECT LEN(@String) - LEN(REPLACE(@String, ',', '')) + 1

Above query will return value 6 , but if the words are separate by more than one space then it will aslo count that space. but its wrong as per the answer.
in that case Create one function which can keep multiple spaces as a single space and return proper result

Below is a Function which can remove
white space and all and retrun peoper result.

CREATE FUNCTION [dbo].[WordCount] ( @inStr VARCHAR(4000) )

RETURNS INT

AS

BEGIN

DECLARE @Index INT

DECLARE @Char CHAR(1)

DECLARE @PrevChar CHAR(1)

DECLARE @WordCount INT

SET @Index = 1

SET @WordCount = 0

WHILE @Index <= LEN(@InStr)

BEGIN

SET @Char = SUBSTRING(@InStr, @Index, 1)

SET @PrevChar = CASE WHEN @Index = 1 THEN ' '

ELSE SUBSTRING(@InStr, @Index - 1, 1)

END

IF @PrevChar = ' ' AND @Char != ' '

SET @WordCount = @WordCount + 1

SET @Index = @Index + 1

END

RETURN @WordCount

END

GO

This is third method that will first split the string and count the string bases on loop

DECLARE @NextString NVARCHAR(40)

DECLARE @Pos INT

DECLARE @NextPos INT

DECLARE @String NVARCHAR(40)

DECLARE @Delimiter NVARCHAR(40)

DECLARE @Counter int

Set @Counter = 0

SET @String ='18,21,22'

SET @Delimiter = ','

SET @String = @String + @Delimiter

SET @Pos = charindex(@Delimiter,@String)

WHILE (@pos <> 0)

BEGIN

SET @NextString = substring(@String,1,@Pos - 1)

--SELECT @NextString -- Show Results

SET @String = substring(@String,@pos+1,len(@String))

SET @pos = charindex(@Delimiter,@String)

Set @Counter = @Counter + 1

END

Tuesday, March 8, 2011

Get Reverse String in XSL


<xsl:template name="reverse">
<xsl:param name="theString"/>
<xsl:variable name="thisLength" select="string-length($theString)"/>
<xsl:choose>
<xsl:when test="$thisLength = 1">
<xsl:value-of select="$theString"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="restReverse">
<xsl:call-template name="reverse">
<xsl:with-param name="theString"
select="substring($theString, 1, $thisLength -1)"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of
select="concat(substring($theString,$thisLength, 1) ,$restReverse)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<!--*****************Start Get File Name from URL *****************-->
<xsl:template name="GetFileName">
<xsl:param name="inputString"/>
<xsl:variable name="delimiter">/</xsl:variable>
<xsl:variable name="xxx">
<xsl:call-template name="reverse">
<xsl:with-param name="theString" select="$inputString" />
</xsl:call-template>
</xsl:variable>

<xsl:variable name="zzz">
<xsl:call-template name="reverse">
<xsl:with-param name="theString" select="substring-before($xxx,$delimiter)" />
</xsl:call-template>
</xsl:variable>

<xsl:value-of select="substring-before($zzz,'.')"/>
</xsl:template>
<!--*****************End Get File Name from URL *****************-->

Tuesday, March 1, 2011

Calling Parent Page method from Child Usercontrol using Reflection in ASP.Net

Parent Page Method
It is very important that the parent page method is declared as public otherwise the child user control will not be able to call the method. Below is the method that I want to call. This function simply accepts a string variable message and displays it on the screen
C#
public void DisplayMessage(string message)
{
Response.Write(message);
}
VB.Net
Public Sub DisplayMessage(ByVal message As String)
Response.Write(message)
End Sub
Child User Control Method
In the user control I have placed a textbox and a button. On the click event of the Button I am calling the Parent Page method we discussed above using Reflection and passing the value of the textbox to the method and then the method is invoked and the message is displayed on the screen.
C#
protected void btnSend_Click(object sender, EventArgs e)
{
this.Page.GetType().InvokeMember("DisplayMessage", System.Reflection.BindingFlags.InvokeMethod, null, this.Page, new object[] { txtMessage.Text });
}
VB.Net
Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As EventArgs)
Me.Page.GetType.InvokeMember("DisplayMessage", System.Reflection.BindingFlags.InvokeMethod, Nothing, Me.Page, New Object() {txtMessage.Text})
End Sub
Here txtMessege.Text Represent any of the existing control
So you can see how easily we can call a method in the parent page using the user control placed on that page.