Search This Blog

Sunday 22 September 2019

Add ToolTip to DropDownList Items in ASP.Net Using jQuery


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script>
        $(document).ready(function () {
            //Using for loop
            //$("select").click(function () {
            //    var s = this;
            //    for (i = 0; i < s.length; i++)
            //        s.options[i].title = s.options[i].text;
            //    if (s.selectedIndex > -1)
            //        s.onmousemove = function () { s.title = s.options[s.selectedIndex].text; };
            //});

            //OR

            //Using jquery each loop
            $("select").click(function () {
                var s = this;
                $("select option").each(function () {
                    $(this).attr("title", $(this).val());
                });
                if (s.selectedIndex > -1)
                    s.onmousemove = function () { s.title = s.options[s.selectedIndex].text; };
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:DropDownList ID="DropDownList1" runat="server">
                <asp:ListItem>Select State</asp:ListItem>
                <asp:ListItem>Andhra Pradesh</asp:ListItem>
                <asp:ListItem>Arunachal Pradesh</asp:ListItem>
                <asp:ListItem>Assam</asp:ListItem>
                <asp:ListItem>Bihar</asp:ListItem>
                <asp:ListItem>Chattisgarh </asp:ListItem>
                <asp:ListItem>Goa</asp:ListItem>
                <asp:ListItem>Gujarat</asp:ListItem>
                <asp:ListItem>Haryana</asp:ListItem>
                <asp:ListItem>Himachal Pradesh </asp:ListItem>
                <asp:ListItem>Jammu and Kashmir</asp:ListItem>
                <asp:ListItem>Jharkhand</asp:ListItem>
                <asp:ListItem>Karnataka</asp:ListItem>
                <asp:ListItem>Kerala </asp:ListItem>
                <asp:ListItem>Madhya Pradesh</asp:ListItem>
                <asp:ListItem>Maharashtra </asp:ListItem>
                <asp:ListItem>Manipur</asp:ListItem>
                <asp:ListItem>Meghalaya</asp:ListItem>
                <asp:ListItem>Mizoram</asp:ListItem>
                <asp:ListItem>Nagaland </asp:ListItem>
                <asp:ListItem>Odisha </asp:ListItem>
                <asp:ListItem>Punjab </asp:ListItem>
                <asp:ListItem>Rajasthan</asp:ListItem>
                <asp:ListItem>Sikkim </asp:ListItem>
                <asp:ListItem>Tamil Nadu  </asp:ListItem>
                <asp:ListItem>Telengana</asp:ListItem>
                <asp:ListItem>Tripura</asp:ListItem>
                <asp:ListItem>Uttar Pradesh </asp:ListItem>
            </asp:DropDownList>
        </div>
    </form>
</body>
</html>

Output


Alternatively You can use the following code in code behind page to show tooltip in Asp.net DropDownList Control.

Note:- Use this code after where DropDownList bind completed.

for (int i = 0; i < DropDownList1.Items.Count; i++)
{
      DropDownList1.Items[i].Attributes.Add("title", DropDownList1.Items[i].Text);
}

Saturday 14 September 2019

How to get sp_executesql result into a variable?

--Way 1:-
DECLARE @Query NVARCHAR(4000)
       ,@TodayDate NVARCHAR(1000)
SET @Query = N'SELECT @TodayDate = GETDATE()'
EXEC SP_EXECUTESQL @Query, N'@TodayDate NVARCHAR(1000) OUTPUT', @TodayDate = @TodayDate OUTPUT
SELECT @TodayDate AS TodayDate

--Way 2:-
CREATE TABLE #FruitName(Id INT IDENTITY,Name VARCHAR(100))
INSERT INTO #FruitName(Name) VALUES('Apple')
INSERT INTO #FruitName(Name) VALUES('Mango')
INSERT INTO #FruitName(Name) VALUES('Banana')
INSERT INTO #FruitName(Name) VALUES('Pineapple')

DECLARE  @Query NVARCHAR(4000)
        ,@Name NVARCHAR(1000)
        ,@Id NVARCHAR(1000)
SET @Id = '1'
SET @Query = N'SELECT @Name = Name FROM #FruitName WHERE Id = '+@Id+''
EXEC sp_executesql @Query, N'@Name NVARCHAR(1000) OUTPUT', @Name = @Name  OUTPUT
SELECT @Name AS Name

--Way 3:- Using Placeholder

DECLARE  @Query NVARCHAR(4000)
        ,@Name NVARCHAR(1000)
        ,@Id NVARCHAR(1000)
SET @Id = '1'
SET @Query = N'SELECT @Name = Name FROM #FruitName WHERE Id = @Id'
EXEC sp_executesql @Query, N'@Id NVARCHAR(1000), @Name NVARCHAR(100) OUTPUT', @ID = @ID, @Name = @Name OUTPUT
SELECT @Name AS Name

--Way 4:- Using Temp Table
DECLARE @Id NVARCHAR(1000)
SET @Id = '1'
CREATE TABLE #Name(Name VARCHAR(100))
INSERT INTO #Name EXEC('SELECT Name FROM #FruitName WHERE Id = '+@Id+'')
SELECT * FROM #Name
DROP TABLE #Name

--Way 5:- Using Table Variable
DECLARE @Id NVARCHAR(1000)
SET @Id = '1'
DECLARE @FruitName AS TABLE(Name VARCHAR(100))
INSERT INTO @FruitName EXEC('SELECT Name FROM #FruitName WHERE Id = '+@Id+'')
SELECT * FROM @FruitName

--Drop Temp Table
DROP TABLE #FruitName