Sunday 20 January 2013

ToolTip in dropdown in asp.net using c#.


In this post I will explain how to show tooltip in dropdown in asp.net using c#.

Description:-

In dropdown when the user name or something is too long ,so that we cannot read or see it properly in dropdown we use tooltip to see the full name. When cursor is on that item in dropdown it will show the full name or path. It’s better to see the example.

First make a design page ,on this take one dropdownlist and bind with table .

Default.aspx:-
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
        <asp:DropDownList ID="DropDownList1" runat="server"
            ondatabound="DropDownList1_DataBound">
        </asp:DropDownList>
    </div>
    </form>
</body>
</html>

Code behind page:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class dropdown_ToolTipInDropDown : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDropDown();
        }
    }
    public void BindDropDown()
    {
        SqlConnection con = new SqlConnection(@"Data Source=SUSHIL-PC;Initial Catalog=modi;Integrated Security=True");
        SqlCommand com = new SqlCommand("Select EmpId , EmpName From EmpDetails",con);
        SqlDataAdapter da = new SqlDataAdapter(com);
        DataSet ds = new DataSet();
        con.Open();
        da.Fill(ds);
        con.Close();
        DropDownList1.DataTextField = "EmpName";
        DropDownList1.DataValueField = "EmpId";
        DropDownList1.DataSource = ds;
        DropDownList1.DataBind();
    }
    protected void DropDownList1_DataBound(object sender, EventArgs e)
    {
        DropDownList ddl = sender as DropDownList;
        if (ddl != null)
        {
            foreach (ListItem li in ddl.Items)
            {
                li.Attributes["title"]=li.Text;
            }
        }
    }
}

Output :-

Search with image like Google in Asp.Net using Jquery.


In this post I will explain how to search with image like google or facebook in asp.net using JQuery.

Description:-

For this we have take one textbox on design page and create database table in which some dummy data along with image. I have Jquery for search with image in this example. It’s better to be taking a practical look.

First create a table  in Sql server .

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[country_image](
     [country_name] [varchar](max) NOT NULL,
     [country_image] [varchar](max) NOT NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

After that create Search.ashx file (this is web handler file).

Search.ashx:-
<%@ WebHandler Language="C#" Class="Search" %>
using System;
using System.Data.SqlClient;
using System.Text;
using System.Web;

public class Search : IHttpHandler {
   
    public void ProcessRequest (HttpContext context) {
        //string searchText = context.Request.QueryString["s"];
        SqlConnection con = new SqlConnection("Data Source=SUSHIL-PC;Initial Catalog=modi;Integrated Security=true");

        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "select country_name,country_image from country_image";
        //cmd.Parameters.AddWithValue("@Search1", searchText);

        StringBuilder sb = new StringBuilder();
        cmd.Connection = con;
        con.Open();
        using (SqlDataReader dr = cmd.ExecuteReader())
        {
            while (dr.Read())
            {
                sb.Append(string.Format("{0},{1}{2}", dr["country_name"],dr[1], Environment.NewLine));
            }
        }
        con.Close();
        context.Response.Write(sb.ToString());
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}

Design a page like this :-

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
   
   <link href="Styles/jquery.autocomplete.css" rel="stylesheet" type="text/css" />
    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script src="Scripts/jquery.autocomplete.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#<%=txt_search.ClientID%>").autocomplete("Search.ashx", {
                width: 280,
                formatItem: function (data, i, n, value) {
                    return "<img style = 'width:25px;height:20px' src='" + value.split(",")[1] + "'/>" + value.split(",")[0];
                },
                formatResult: function (data, value) {
                    return value.split(",")[0];
                }
            });
        });
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:TextBox ID="txt_search" runat="server" Height="20px" Width="275px" Font-Names="roboto" ForeColor="Silver"></asp:TextBox>
    </div>
    </form>
</body>
</html>


OutPut :-

Wednesday 16 January 2013

Read Xml file in GridView in asp.net using c#.


In this post I will explain how to read XML file in GridView in asp.net using c#.

Description:-

I have written many post regarding gridView . In this post I will explain how to read a xml file in griview or how to bind xml data to gridview. For this first Create an xml file and put some dummy data in it.
Create a xml file name Employee.xml and insert some data in this file.

Employee.xml:-

<?xml version="1.0" encoding="utf-8" ?>
<employees>
  <employee>
    <EmpName>Sushil Kumar</EmpName>
    <EmailId>Sushilct25@gmail.com</EmailId>
    <Designation>Engg.</Designation>
    <PhoneNo>9998882345</PhoneNo>
  </employee>
  <employee>
    <EmpName>Prdeep Kumar</EmpName>
    <EmailId>pradeep@gmail.com</EmailId>
    <Designation>Manager</Designation>
    <PhoneNo>12344567890</PhoneNo>
  </employee>
  <employee>
    <EmpName>sushil Modi</EmpName>
    <EmailId>modi@gmail.com</EmailId>
    <Designation>Soft. Developer</Designation>
    <PhoneNo>224525426</PhoneNo>
  </employee>
  <employee>
    <EmpName>Rahul Ranjan</EmpName>
    <EmailId>ranjan@gmail.com</EmailId>
    <Designation>Engg.</Designation>
    <PhoneNo>1245225</PhoneNo>
  </employee>
  <employee>
    <EmpName>Chulbul Pandey</EmpName>
    <EmailId>pandey@gmail.com</EmailId>
    <Designation>Economist</Designation>
    <PhoneNo>235235353</PhoneNo>
  </employee>
</employees>

After that add a web page to the project and drag  a GidView on this page like this..

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Read Xml File In GridView</title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
        <asp:GridView ID="GridView1" runat="server" BackColor="#DEBA84"
            BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3"
            CellSpacing="2">
            <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
            <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
            <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
            <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#FFF1D4" />
            <SortedAscendingHeaderStyle BackColor="#B95C30" />
            <SortedDescendingCellStyle BackColor="#F1E5CE" />
            <SortedDescendingHeaderStyle BackColor="#93451F" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>

After that add the following namespace  in codebehind:-

using System.Data;
using System.Xml;

Code behind page:-
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridView();
        }
    }
    public void BindGridView()
    {
        XmlTextReader xmlReader = new XmlTextReader(Server.MapPath("Employee.xml"));
        DataSet ds = new DataSet();
        ds.ReadXml(xmlReader);
        xmlReader.Close();
        if (ds.Tables.Count != 0)
        {
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
        else
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            GridView1.DataSource = ds;
            GridView1.DataBind();
            int columnCount = GridView1.Rows[0].Cells.Count;
            GridView1.Rows[0].Cells.Clear();
            GridView1.Rows[0].Cells.Add(new TableCell());
            GridView1.Rows[0].Cells[0].ColumnSpan = columnCount;
            GridView1.Rows[0].Cells[0].Text = "No Record Found....";
        }
    }

Output  like this:-