Wednesday, 31 July 2013

Split Comma separated string without using cursor or while condition in sql Or Split comma separated string using XQuery

Split Comma separated string without using cursor or while condition in sql.
Split comma separated string using XQuery

Using below Query we can split comma separated string 

DECLARE @CommaSepString VARCHAR(max)

SET @CommaSepString='IND,UK,USA,AST,IND,PAK';



SELECT SplitCountry FROM (SELECT CAST('<A>' +Replace(@CommaSepString,',','</A><A>')+'</A>' AS XML) AS Strings
      )F1
      CROSS APPLY
      (
       SELECT Country.item.value('.','varchar(50)') AS SplitCountry
       FROM F1.Strings.nodes('A') AS Country(item)

      )F2


The out put looks like as below.



If you want to create reversely that mean table rows values to comma separated string look in below link.


Create Comma separated string from table rows value without using looping in SQL Or Create comma separated string from table rows values using XQuery


Create comma separated string from table rows value without using looping in SQL.


Or Create comma separated string from table rows values using XQuery.

Using below query we can achieve it.



In above example created one temporary table #Test. In the #Test temp table inserted some string values.

Using the below query you can get comma separated string.

SELECT STUFF(( SELECT DISTINCT  ',' + DataVal
                FROM #Test
                FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '') AS [Comma Separated String];


If you want to do reversely that mean to split comma separated value see in below link..

http://mastermindsdotnet.blogspot.in/2013/07/split-comma-separated-string-without.html

Wednesday, 3 April 2013

How to get same node values from the xml file using XQuery?


If we have xml file like
<Countries>
      <Country>India</Country>
      <Country>USA</Country>
      <Country>UK</Country>
      <Country>China</Country>
      <Country>Shrilanka</Country> 
</Countries>

If we want to get all countries, we can write query like following way..
SELECT
CAST(xmlFile.Country.query('data(.)') AS varchar(50)) AS [Country]
FROM @xml.nodes('/Countries/Country') AS xmlFile(Country)


Where @xml variable declare as XML and assign the xmlfile.
Full Example is as below.

DECLARE @xml XML

SET @xml='<Countries>
      <Country>India</Country>
      <Country>USA</Country>
      <Country>UK</Country>
      <Country>China</Country>
      <Country>Shrilanka</Country> 
</Countries>'

SELECT
CAST(xmlFile.Country.query('data(.)') AS varchar(50)) AS [Country]
FROM @xml.nodes('/Countries/Country') AS xmlFile(Country)

Wednesday, 27 March 2013

How to set maximum length to textarea in html


 Set maximum length to textarea in html
<textarea rows="3" cols="10" onkeypress="if (this.value.length > 10) { return false; }" ></textarea>

How to set maximum length to asp.net multiline textbox?


Set maximum length of a multiline property textbox.

<asp:TextBox ID="txt" runat="server" TextMode="MultiLine" Height="60" onkeypress="if (this.value.length > 10) { return false; }"></asp:TextBox>

Tuesday, 26 March 2013

Find the xml node value without knowing the position of an xml node from xml file using SQL query.


To find the xml node value without knowing the position of an xml node from xml file using SQL query.


Given 1 sample xml file which consist MyNode. Here I want to get the value of MyNode which may be placed in different position.
DECLARE @xml XML

SET @xml='<RootNode>
<SubNodes>
      <Node1>Node 1 </Node1>
      <Node2>Node 2 </Node2>
      <Node3>
            <MyNode> My node value </MyNode>
       </Node3>
 </SubNodes>
</RootNode>'

SELECT Nod.value('local-name(.)', 'varchar(50)') AS 'NodeName',
         Nod.value('.', 'varchar(50)') AS 'Node Value'
         FROM @xml.nodes('//*') AS xmlFile(Nod) WHERE Nod.value('local-name(.)', 'varchar(50)')='MyNode'

The output look like



Saturday, 23 March 2013

Creating Movable Controls in a WPF Application.


Create Canvas control in WPF window

  <Canvas x:Name="MyCanvas" Background="LightBlue" >          
           
  </Canvas> 


Add controls whatever you wish
I am taking ellipse control the XAML syntax is as follows

<Ellipse Fill="Red" Height="50" Width="100" Canvas.Top="100" Canvas.Left="50" >
            <Ellipse.RenderTransform>
                <TranslateTransform/>
            </Ellipse.RenderTransform>
</Ellipse>


Create MouseLeftButtonDown event for ellipse control
The XAML syntax looks like as below.

<Ellipse Fill="Red" Height="50" Width="100" Canvas.Top="100" Canvas.Left="50" MouseLeftButtonDown="Ellipse_MouseLeftButtonDown">
            <Ellipse.RenderTransform>
                <TranslateTransform/>
            </Ellipse.RenderTransform>
</Ellipse>

Same way you can add another controls. I am adding Rectangle control

<Rectangle Height="100" Width="100" Canvas.Left="243" Canvas.Top="62" Fill="Gray" MouseLeftButtonDown="Rectangle_MouseLeftButtonDown">
            <Rectangle.RenderTransform>
                <TranslateTransform/>
            </Rectangle.RenderTransform>
</Rectangle>


Now create MouseDown, MouseMove, MouseUp events to Canvas Control.

The line looks like

<Canvas x:Name="MyCanvas" Background="LightBlue" MouseDown="Canvas_MouseDown" MouseMove="Canvas_MouseMove" MouseUp="Canvas_MouseUp" >


The design look like



Now going towards to add the code.

Create global variable

double positionX = 0;
double positionY = 0;       
UIElement UIElementHolder = null;

Write the following codes in Code file.
private void Canvas_MouseDown(object sender, MouseButtonEventArgs e)
{
               positionX = Mouse.GetPosition((UIElement)sender).X;
               positionY = Mouse.GetPosition((UIElement)sender).Y;          
}

private void Canvas_MouseMove(object sender, MouseEventArgs e)
       {
            // checking draging is holding
            if (e.LeftButton == MouseButtonState.Pressed && UIElementHolder != null)
            {
           
                //Capturing the mouse position
                double newX = Mouse.GetPosition(MyCanvas).X;
                double newY = Mouse.GetPosition(MyCanvas).Y;

                //resetting the holded element position

                //control rendertransfer object created for moving in runtime using translateTransfrom element
                Transform objUIElement = UIElementHolder.RenderTransform;

                //getting offset values of control
                double offsetX = objUIElement.Value.OffsetX;
                double offsetY = objUIElement.Value.OffsetY;

                //changing translateTransfrom
                objUIElement.SetValue(TranslateTransform.XProperty, newX+offsetX-positionX);
                objUIElement.SetValue(TranslateTransform.YProperty, newY+offsetY-positionY);             

                // Update position to global variables
                positionX= newX;
                positionY = newY;
             
            }
        }

        private void Canvas_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (UIElementHolder != null)
            {
                //Clearing holding UIElement              
                UIElementHolder = null;
            }

        }

        private void Ellipse_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //Assigning related UIElement to UIElementHolder Object
            UIElementHolder = (UIElement)sender;
        }

        private void Rectangle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //Assigning related UIElement to UIElementHolder Object
            UIElementHolder = (UIElement)sender;
        }


Now run the application move the controls.. 

Friday, 25 January 2013

To count numbers of specified values in an array using LINQ.


To count numbers of specified values in an array using LINQ. 

string[] str = { "AA", "DD", "CC", "DD", "EE", "FF", "GG", "AA",                     "KK", "DD" };
            
int nCount = str.Count(m => m.Equals("DD"));


Tuesday, 8 January 2013

Create simple WPF application


Create simple wpf application

Open Visual studio 2010
Click on File menu.. Click on new. Click on Project.. you will get new project pop up window.


Click on Visual C#--> Windows.
Then select WPF Application in side content.



You can also able to change the .Net Framework version.  On the top dropdown list you can see the .Net Framework versions.
To change the project name type what name you want to give.
If you want to change the location click on browser and choose where you want to create the project.
Then click on ok.

You will get WPF application. The screen is look like as below.



In the above screen we can see two windows those are Design and XAML editor window.

Controls are available in toolbox.
Drag button control from toolbox to window.




In XAML Editor Window you can change the properties of controls
In the below screen I change the window Title property as My First Window and Button Content property to Click Me



Double click on Click Me Button, Button Click Event is generated in code file.

The changes in the XAML Editor window you can see the below image.




In code file write the following code to show message.
MessageBox.Show("Welcome to WPF..", "Info", MessageBoxButton.OK);

See image below

















Run the program and in window click on click button you will get message box dialog.  The window is look like below screen.







how to create round edge window in WPF Application?




Following XAML to make round edge window in WPF.



Required windows properties set to 


        WindowStyle="None"
        AllowsTransparency="True"
        Background="Transparent"


Then add border control to in windows content
Border syntax is as follows
<Border Name="windowBorder" BorderThickness="2" BorderBrush="DarkBlue" CornerRadius="20" Background="LightBlue">

  </Border>

If you want to design inside the window add grid control or use other layout control to design what you want to wish.

The all code is shown as below


<Window x:Class="NoneBorder.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Withought Border"
        Height="350" Width="525"
        WindowStyle="None"
        AllowsTransparency="True"
        Background="Transparent">
    <Border Name="windowBorder" BorderThickness="2" BorderBrush="DarkBlue" CornerRadius="20" Background="LightBlue">
        <Grid>
            <Button Content="Close" Height="23" HorizontalAlignment="Right" Margin="0,53,121,0" Name="btnClose" VerticalAlignment="Top" Width="75" Click="btnClose_Click" />
        </Grid>  
    </Border>
</Window>



The window is looks like as follows.







Wednesday, 2 January 2013

Script for to find the identity column in a table



SELECT name AS HasIdentity
FROM syscolumns
WHERE OBJECT_NAME(id) = 'table_Name'
AND COLUMNPROPERTY(id, name, 'IsIdentity') = 1