Sunday, January 26, 2014

different way to create a delegate


In my previous post i explained, how to Search string pattern using sql on SQL i had explained how to Add Image in between Rows of a GridView using C#.


Below steps mention how to create delegate is four process:-

1. Declare a delegate.

2. Create an object reference.
3. Point the reference to the method.
4. Invoke the method via the delegate.



Refer more details  on How To Create Custom Event Using Delegate In C#

Thursday, December 12, 2013

list table name and schema using sql



Just a hours ago, i was looking for code which will list-out all the tables in database with its schema name. I found so many way to do this , but here i found simple and easy way to implement it.

SELECT '['+SCHEMA_NAME(schema_id)+'].['+name+']'
 AS ListofTables_withSchema
FROM sys.tables

This will returns all the List of table along with the schema names.
 
 
 
 
 Happy coding...

Saturday, December 7, 2013

Gridview header sorting using C#

In my previous post i explained, how to Search string pattern using sql on SQL i had explained how to Add Image in between Rows of a GridView using C#.
 
Now in this article i will explain one of the useful feature i.e Gridview Sorting.
you need to set the AllowSorting property as a True. and SortExpression Property of columns to the specific field name from the database.
Lets look at the below sample gridview code.
 
<asp:GridView ID="gvDetails" runat="server"  onsorting="gvDetails_Sorting" AllowSorting="True">
<Columns>
<asp:TemplateField HeaderText="Your Name" SortExpression="FirstName">
<ItemTemplate>
<%#Eval("YourName")%>'/>
</ItemTemplate>
</asp:TemplateField>
</asp:GridView>

Now for sorting you need to create one public property which store the value of direction in the viewstate and base on that select query get fired.


public GetSortDir direction
{
  get
  {
    if (ViewState["SortingDir"] == null)
        {
            ViewState["SortingDir"] = GetSortDir.Ascending;
         }
         return (GetSortDir)ViewState["SortingDir"];
   }
   set
   {
       ViewState["SortingDir"] = value;
   }
}

Now check the gridview directon and base on viewstate it will get set the new direction.

    protected void gvDetails_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sortDirection = string.Empty;
        if (direction == sortDirection.Ascending)
        {
            direction = sortDirection.Descending;
            sortDirection = "Desc";
        }
        else
        {
            direction = sortDirection.Ascending;
            sortDirection = "Asc";
        }
       
        DataView sortedView = new DataView(BindGridView());
        sortedView.Sort = e.SortExpression + " " + sortDirection;
        gvDetails.DataSource = sortedView;
        gvDetails.DataBind();
    }


Here BindGridView() is the function which returns the datatable & it contain the query result.
that's it now check the gridview and click on the header and see the sorting effects get added in your gridview.
 
 
 

Saturday, November 23, 2013

Search string pattern using sql


In my previous post i explained, how to Parsing delimited string using sql using  SQL and asp net
Now in this article i will explain how to Search the string pattern on entire string using the sql. some time we wanted to search the specific string along with the position then we can think of this way to retrieve the string.

Below i have created some sample query which will retrieve the string on specific position 


create table #TempSQL(csvtext varchar(2000) not null)
insert #
TempSQL select 'tempa,tempb,tempc,tempd,tempe,tempf,tempg' union select 'temp1,temp,temp3,temp4,temp5,temp6'

select
       dbo.fnGetCsvPart(csvtext,0,default) as pos0
       ,dbo.fnGetCsvPart(csvtext,2,default) as pos2
       ,dbo.fnGetCsvPart(csvtext,2,1) as Entire_string
from #
TempSQL
 

Here is the generated output from above query.

pos0                       pos2                          Entire_String
------------------ ------------------------------ ------------------------------
temp1                        temp3                   temp3,temp4,temp5,temp6
tempa                        tempc                   tempc,tempd,tempe,tempf,tempg 


Now create this function


create  function dbo.fnGetCsvPart(@csvtext varchar(2000),@indexPos tinyint, @lastPos bit = 0)
returns varchar(5000)
as

begin
   declare @ivar int; set @ivar = 0
   while 1 = 1
       begin
           if @indexPos = 0
             begin
                 if @lastPos = 1 or charindex('_',@csvtext,@ivar +1) = 0
                       return substring(@csvtext,@ivar +1,len(@csvtext)-@ivar +1)
                     else
                       return substring(@csvtext,@ivar +1,charindex('_',@csvtext,@ivar +1)-@ivar -1)
            end
          select @indexPos = @indexPos-1, @ivar = charindex('_',@csvtext,@ivar +1)
          if @ivar = 0 break
   end
 return null
end
GO


You may call that directly on your query. check below  example.

select Isnull(dbo.fnGetCsvPart(Document_Name,2,default),'') as [Request]
 

Wednesday, December 26, 2012

Parsing a delimited string in SQL

 Most of the time we required the delimited strings to be added in database. today i come up with some simple solution. which will read the string and specified delimited character and base on that it will divide the string and split it across.

CREATE FUNCTION ParseValues
(@String varchar(8000), @Delimiter varchar(10) )
RETURNS @RESULTS TABLE (ID int identity(1,1), Val varchar(50))
AS
BEGIN
DECLARE @Value varchar(100)
WHILE @String is not null
BEGIN
SELECT @Value=CASE WHEN PATINDEX('%'+@Delimiter+'%',@String) >0 THEN LEFT(@String,PATINDEX('%'+@Delimiter+'%',@String)-1) ELSE @String END, @String=CASE WHEN PATINDEX('%'+@Delimiter+'%',@String) >0 THEN SUBSTRING(@String,PATINDEX('%'+@Delimiter+'%',@String)+LEN(@Delimiter),LEN(@String)) ELSE NULL END
INSERT INTO @RESULTS (Val)
SELECT @Value
END
RETURN
END

You can call this function in your query and it will return the result.(as show in img)

select * from dbo.ParseValues('This;is;a;delimited;string;value',';')


and here is your result..

   
 Hope this will helps you, Please put your comments or dought to help others.
 

Monday, June 18, 2012

Jquery copy to clipboard

Hi, after long time right? yea... 

anyways after long time i am decided to get back again on blogger :) and make this blogger active and helpful to others.

today i come up with some tricks on javascript or on jquery . its a Copy to clipboard.
In IE we have direct window option to make the copy to clipboard code event , but if you try to do same with Firefox or other it wont work 

so i thought let add some trick to make the copy thing easy :).

Find my below code and put it in the head section


 
<script type="text/javascript" src="http://code.jquery.com/jquery-1.3.2.min.js"> </script>

 
<script type="text/javascript" src="js/jquery.zclip.js"> </script>

    <script type="text/javascript">
        $(document).ready(function() {
            $('a#copy-dynamic').zclip({
                path: 'js/ZeroClipboard.swf',
                copy: function() { return $('input#dynamic').val(); }
            });

        });
   
</script>

and insert this section on your body part.

 <div>
       
<br />
        <a href="#" id="copy-dynamic">Click here to copy the value of this input:</a>
        <input style="width: 300px; margin-left: 15px;" type="text" id="dynamic" value="Insert any text here." onfocus="if(this.value=='Insert any text here.'){this.value=''}" onblur="if(this.value==''){this.value='Insert any text here.'}" />
   
</div>

lets download the swf from GitHubs and put it in your js directory accordingly.

now simple run the application , type the text on the textbox and click on copy button it will copy it in your system just like (Ctrl + C).

Put your comments or suggestion to make this thread active and popular.

Tuesday, May 3, 2011

Jquery Auto Hide

Hi Guys today i come up with some cool example of JQuery. its a "Auto hide button or Div after some interval time "

if you seen in Gmail, when you send a mail or move the mail to any folder or if you do any operation you will see the confirm message and after some interval time it will hide automatically.
like this:

yes here is the source on same.


<html><head>
<script language="javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script language="javascript">
(function($){
$(document).ready(function() {
$("[AutoHide]").each(function() {
if (!isNaN($(this).attr("AutoHide"))) {
eval("setTimeout(function() {jQuery('#" + this.id + "').hide();}, " + parseInt($(this).attr('AutoHide')) * 1000 + ");");
}
});
});
})(jQuery);
</script>
</head>
<body>
<center>
<br><br>
<div id="div1Seconds" AutoHide="10" style="background: #ccc; border: solid 1px #333">
<input type="button" value="This Button will be hidden in 10 second.">
</div>
<br><br>
<div id="div3Seconds" AutoHide="15" style="background: #ccc; border: solid 1px #333">
<input type="button" value="This Button will be hidden in 15 second.">
</div>
</center>
</body>
</html>

Demo : (refresh page to view the demo)










Hope it will likes you.