"If at first you don't succeed; call it version 1.0" :-Unknown

Pages

Monday, September 19, 2011

Retrieve custom message if column is null in query

Hi *.*,
 Retrieve custom message if column value is null using query. Oracle, Mysql MSSQL all are providing different keyword for this purpose

NVL() function For Oracle
This function is used to replace NULL value with another value.
Table Sales_Data
store_name
Sales
Store A
300
Store B
NULL
Store C
150







  • SELECT  NVL(Sales,’No data Avilable’) FROM Sales_Data
  • SELECT SUM(NVL(Sales,100)) FROM Sales_Data;
2nd query will returns 550. This is because NULL has been replaced by 100 via the ISNULL function, hence the sum of the 3 rows is 300 + 100 + 150 = 550. 

 ISNULL() function for SQL Server
is used to replace NULL value with another value.

SELECT ISNULL(Sales,’No data avilable’)) FROM Sales_Data;
 IFNULL Function for MySQL
This function takes two arguments. If the first argument is not NULL, the function returns the first argument. Otherwise, the second argument is returned. This function is commonly used to replace NULL value with another value.

SELECT IFNULL(Sales,’No data avilable’) FROM Sales_Data;


Have a nice day... 'N happy Coding :)

Friday, September 16, 2011

Binding or display image having zoom effect from folder

Hi *.*,
As a continuation of my previous post. Now i'm binding images from folder.

Code 
    private class ImageFileInfo
    {
        public string FileName { get; set; }
    }

    public void limage()
    {
        string imgFolder = HttpContext.Current.Request.PhysicalApplicationPath + @"img\";
        string[] fileEntries = Directory.GetFiles(imgFolder);
        List<ImageFileInfo> mylist = new List<ImageFileInfo>();
        foreach (string fName in fileEntries)
        {
            string d = System.IO.Path.GetFileName(fName);
            mylist.Add(new ImageFileInfo { FileName = d });
        }
        dtagrid.DataSource = mylist;
        dtagrid.DataBind();
    }
 

On HTML
 
                <asp:DataList ID="dtagrid" runat="server" RepeatColumns="5" RepeatDirection="Horizontal">
                    <ItemTemplate>
                        <li><a href='img/<%# Eval("FileName") %>'>
                            <img src='img/<%# Eval("FileName") %>' alt="" /></a></li>
                    </ItemTemplate>
                </asp:DataList>

 U can download a demo from here
 Stay Tune...
Have a nice day... 'N happy Coding :)

Monday, September 12, 2011

Nested grid and findControl over child grid

Hi *.*,
Nested grid and findControl over child grid
Over here i have two gridview (GridView1,GridView2) and on 2nd grid  a linkbutton, onclick event i need to extract particular row data. that my need here its go.
 On HTML


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">
            <Columns>
                <asp:TemplateField HeaderText="Project">
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("PkID") %>'></asp:Label>
                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("StrName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Sub Task">
                    <ItemTemplate>
                        <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView2_RowCommand">
                            <Columns>
                                <asp:TemplateField HeaderText="Task Name">
                                    <ItemTemplate>
                                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("name") %>'></asp:Label>
                                        <asp:Label ID="Label4" runat="server" Text='<%# Bind("pk") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Task Status">
                                    <ItemTemplate>
                                        <asp:DropDownList ID="DropDownList1" runat="server">
                                            <asp:ListItem>Open</asp:ListItem>
                                            <asp:ListItem>Close</asp:ListItem>
                                            <asp:ListItem>Finish</asp:ListItem>
                                        </asp:DropDownList>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField>
                                    <ItemTemplate>
                                        <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Select" CommandArgument="<%#((GridViewRow) Container).RowIndex %>">Update</asp:LinkButton>
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                            <HeaderStyle BackColor="#CCCCFF" />
                        </asp:GridView>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <HeaderStyle BackColor="Maroon" ForeColor="#FFCC00" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>

 
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            GridView1.DataSource = d("SELECT PkID, StrName, DateTimeStamp FROM T2");
            GridView1.DataBind();
        }
    }

    public SqlDataReader d(string _Query)
    {
        SqlCommand cmd = new SqlCommand(_Query, con());
         return cmd.ExecuteReader();

    }

    public SqlConnection  con()
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionDemoString"].ToString());
        con.Open();
        return con;
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)// Bind nested grid view with parent grid view
            {
                string _name = ((Label)e.Row.FindControl("Label3")).Text;
                GridView griedview2 = (GridView)e.Row.FindControl("GridView2");
                griedview2.DataSource = d("SELECT     pk, name, DrpDwn FROM         TBNAME where DrpDwn='" + _name + "'");
                griedview2.DataBind();
            }
        }
        catch (Exception)
        {
           
            throw;
        }
    }
    protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        GridView GridView2 = (GridView)sender;  //find nested grid view
        GridViewRow chrow = (GridViewRow)GridView2.Parent.Parent; // parent grid's row for find any control from parent grid view
        if (e.CommandName == "Select")
        {
            int index = Convert.ToInt32(e.CommandArgument); //use command argument for identify row with nested grid view

            int pid = Convert.ToInt32(((Label)GridView2.Rows[index].FindControl("Label4")).Text); // id from labe4 of nested grid view
            DropDownList childrop = ((DropDownList)GridView2.Rows[index].FindControl("DropDownList1")); // get dropdownlist box of nested grid view
            string status = childrop.SelectedItem.Text;
        }

Hope u guys got it....If any trouble message me. :)
Stay Tune...
Have a nice day... 'N happy Coding :)