Wednesday, August 24, 2011

Row/Item Count in Repeater



Using below code you can get Repeater's Row/Item Count:

in below code "Container.ItemIndex" is used to get current row number and
"(string[])rpttags.DataSource).Length" is used to get total row/item count.
in above line my datasource for repeater was a string array so i used string[] as datasourcetype and used length to get its count in same way you can get for all other types

eg:-
<%# (Container.ItemIndex < ((yourDataSourceDataType)rpttags.DataSource).Length  - 1?",":"") %>

Compiled By: Rajesh Rolen

Read More

Monday, August 22, 2011

HTML Encode/Decode using JQuery

using below code you can encode/decode html tags using jquery

function htmlEncode(value){
  return $('
').text(value).html(); } function htmlDecode(value){ return $('
').html(value).text(); }

Compiled By: Rajesh Rolen

Read More

Friday, August 12, 2011

SQL Server parameter sniffing



While working on a project which has huge database interaction and i was using stored procedure to get good performance. But even though i was using stored procedure the performance was not so good all the time. I mean it was not consistent, for few queries on same stored procedure it was so good and some times it was going worst.

I have done lots of google for it and finally i got the solution.. The easiest solution which you can apply to any existing stored procedure without doing any major changes in it..

Its mostly helpful in situation where you have a stored procedure and the amount of data returned is varying wildly depending on what parameters are passed in.

Some time you will see that the performance of stored procedure change dramatically depending on how much data is being returned.

Then you could be a victim of SQL Server Parameter sniffing.
[YOU ARE AT RIGHT PLACE TO GET SOLUTION]


Normally we writes procedure like:
CREATE PROCEDURE GetUserData
   @UserName nvarchar(20)
    AS
    BEGIN
        SELECT DisplayName, FirstName, LastName 
        FROM dbo.User
        WHERE UserName = @UserName
    END

Now to get good speed constantly, just do some minor changes in it:
CREATE PROCEDURE GetUserData
        @UserName nvarchar(20)
    AS
    BEGIN
        DECLARE @myUserName nvarchar(20)
         SET @myUserName = @UserName
     
        SELECT DisplayName, FirstName, LastName 
        FROM dbo.User
       WHERE UserName = @myUserName
   END

I have done some minor changes in it.
that is, i have just declared local variables and assigned the values of parameters in it and then used those variables in queries instead of using parameters directly.

Now You must be thinking that how come it affect the performance.
Let me explain:

Here is the Microsoft definition:

“Parameter sniffing” refers to a process whereby SQL Server’s execution environment “sniffs” the current parameter values during compilation or recompilation, and passes it along to the query optimizer so that they can be used to generate potentially faster query execution plans. The word “current” refers to the parameter values present in the statement call that caused a compilation or a recompilation.


So For some strange reason, when you pass the parameters (@UserName in this case) to a local variable (@myUserName) SQL Server will no longer use the value of the parameter (@UserName) to influence the query plan and you will get the performance constantly. :)

Compiled By: Rajesh Rolen

Read More

Thursday, August 11, 2011

How to Query for single value for multiple column in fulltext search




Search a value in multiple column using fulltext search:
select * from productmaster where FREETEXT((productName,productDetails),'Search this text')

Compiled By: Rajesh Rolen

Read More

show multiple jquery scroller in single page



To show multiple jquery scroller in single page just add an extra div on outer side or every scroller. means place every scroller in seperate div and its done..


Compiled By: Rajesh Rolen

Read More

Wednesday, August 10, 2011

What is difference between Dictionary and Hashtable



The fact is that dictionary is a hashtable.
The main difference is that dictionary is a hashtable where as hashtable is not.
That means you get type safety with Dictionary, because you can't insert any random object into it, and you don't have to cast the values you take out.
its almost similar what difference between array list and generic list.

Compiled By: Rajesh Rolen

Read More

When caching is enabled for the XmlDataSource that is not in the page's control tree it requires a UniqueID that is unique throughout the application.

XmlDataSource class internally calls a private method called CreateCacheKey. Now, if you are using XmlDataSource without an ID, after upgrading the solution to ASP.NET 3.5, this might throw an exception - "When caching is enabled for the XmlDataSource that is not in the pages control tree it requires a UniqueID that is unique throughout the application." This is due to the absence of the UniqueID (which is read-only, but my experiment shows that setting the ID handles the same), which is used as part of the caching key, without which all instances would try to use same caching key. Setting a distinct ID solves this problem and the problem goes away

eg:


XmlDataSource XmlSrc = new XmlDataSource();
XmlSrc.ID = "someid";



Compiled By: Rajesh Rolen

Read More

Tuesday, August 9, 2011

Query Xml File with Namespace using XPath



We all know how to get value from xml file in .net but we face issue when there is a namespace with that xml file.

When a xml file contains a namespace then you will have to do some extra efforts to retrieve its value.

Lets say my xml is like:



Videos
119150

207463
somepath
sometitle
somelink
...

http://someurl/standard-and-poors-india-connection/207463

Tue, 09 Aug 2011 6:26:48 IST
1312894608




Now i wants to access value of "url" attribute of "media:thumbnail" tag, so to get it you will have to do :

Dim path As String = "http://api.flickr.com/services/feeds/photoset.gne?set=72157604607980332&nsid=51374889@N00&lang=en-us&format=rss_200_enc"
        Dim xmldoc As New XmlDocument()
        xmldoc.Load(path)
        Dim xnm As New XmlNamespaceManager(xmldoc.NameTable)
        xnm.AddNamespace("media", "http://search.yahoo.com/mrss/")
        Dim nList As XmlNodeList = xmldoc.SelectNodes("//item/media:thumbnail/@url", xnm)

        For Each xNode As XmlNode In nList
            ListBox1.Items.Add(xNode.InnerText)
            TextBox1.Text += TextBox1.Text + xNode.InnerText + vbNewLine
        Next


Now comes to original problem for which i have done google for 2-3 hours and then finally i got the answer.

Problem: I have got a repeater and i wants to show this xml values in it. now 1 option is that you can convert it to a datatable and easily use it.. but it will be extra process to you application. so now we wants to show xml directly to the repeater... to do so see below code:

In code behind file:
XmlDataSource xds = new XmlDataSource();
xds.XPath = "rss/channel/item";
xds.DataFile = sourceXml;// you can write xml path here.
myrpt.DataSource = xds;
myrpt.DataBind();

Now in .aspx file write below code:
< asp:Repeater ID="myrpt" runat="server" >
                < ItemTemplate >
                    < li >
                        < div class="multi_div" >
                            < img src='< %#XPath("*[local-name()='thumbnail' and namespace-uri()='http://search.yahoo.com/mrss/']/@url")% >' class="imgbdr" / >< div class="multi_blkstrip" >
                                < p class="float_l" >
                                    < img src="images/video_icon.gif" / >< /p >
                                < p class="float_r" >
                                    < %#XPath("title")% >< /p >
                            < /div >
                        < /div >
                        < p class="vidcap" >
                            < a href="#" class="fn fl" >< %#XPath("title")% >< /a >< /p >
                    < /li >
                < /ItemTemplate >
            < /asp:Repeater >

so this is the main line (solution):

< %#XPath("*[local-name()='thumbnail' and namespace-uri()='http://search.yahoo.com/mrss/']/@url")% >'

Compiled By: Rajesh Rolen

Read More

Wednesday, August 3, 2011

Handling null values with the Linq Aggregate methods



You can use it like:

var value = (from t in _table
             where t.Id == id
                && t.Time >= intervalStartTime
                && t.Time <= intervalEndTime
             select (int?)t.Value).Average()

Compiled By: Rajesh Rolen

Read More

For translation to SQL, the Math.Round method needs a MidpointRounding parameter. Use 'AwayFromZero' to specify the SQL function ROUND.



Whenever you will try to use Math.Round with LINQ, that time you will face this error:

For translation to SQL, the Math.Round method needs a MidpointRounding parameter. Use 'AwayFromZero' to specify the SQL function ROUND.


To overcome this problem. just do as its saying..
avgRate = Math.Round( (double) rmpd.Average(a => a.reate),1, MidpointRounding.AwayFromZero )

Compiled By: Rajesh Rolen

Read More

Tuesday, August 2, 2011

Find all possible combinations of a string



Using below function we can find all possible combinations of any string.

  static void Combination(string left, string right)
        {
            if (right.Length == 1)
            {
                Console.Write(left);
                Console.Write(right);
                Console.WriteLine();
                return;
            }
            else
            {
                for (var index = 0; index < right.Length; index++)
                {
                    Combination(left + right[index], right.Remove(index, 1));
                }
            }
        }

you can call above function like:

 Combination("", Console.ReadLine());

Compiled By: Rajesh Rolen

Read More

Monday, August 1, 2011

Convert Integers to Binary, Octal or Hexadecimal

Console.WriteLine(Convert.ToString(value, 2));      // Outputs "10101010"
Console.WriteLine(Convert.ToString(value, 8));      // Outputs "271"
Console.WriteLine(Convert.ToString(value, 16));     // Outputs "b9"

Compiled By: Rajesh Rolen

Read More
Powered By Blogger · Designed By Seo Blogger Templates