Thursday, July 17, 2014

Nested Grid Through ADO.NET

In aspx put the grid in the given way-:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="gvParent" runat="server" AutoGenerateColumns="False" BackColor="White"
            BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1"
            GridLines="None" OnRowCommand="gvParent_RowCommand">
            <Columns>
                <asp:BoundField DataField="Grp_Id" HeaderText="ID" SortExpression="Grp_Id" />
                <asp:BoundField DataField="GrpNm" HeaderText="Name" SortExpression="GrpNm" />
                <asp:TemplateField HeaderText="Group">
                    <ItemTemplate>
                        <asp:Button ID="btnShowChild" runat="server" CommandArgument='<%# Bind("Grp_Id") %>'
                            CommandName="ShowChild" Text="+" />
                        <asp:Button ID="btnHideChild" runat="server" CommandArgument='<%# Bind("Grp_Id") %>'
                            CommandName="HideChild" Text="-" Visible="false" />
                        <asp:GridView ID="gvChild" runat="server" AutoGenerateColumns="False">
                            <Columns>
                                <asp:BoundField DataField="Acct_Id" HeaderText="AcctId" SortExpression="Acct_Id" />
                                <asp:BoundField DataField="PartyNm" HeaderText="Party Name" SortExpression="PartyNm" />
                                <asp:BoundField DataField="TempAdd" HeaderText="Address" SortExpression="TempAdd" />
                            </Columns>
                        </asp:GridView>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
            <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
            <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
            <RowStyle BackColor="#DEDFDE" ForeColor="Black" />
            <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#F1F1F1" />
            <SortedAscendingHeaderStyle BackColor="#594B9C" />
            <SortedDescendingCellStyle BackColor="#CAC9C9" />
            <SortedDescendingHeaderStyle BackColor="#33276A" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>

Code Behind File will be like this--:

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DbMandiConnectionString"].ConnectionString);
    string query = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindParentGrid();
        }
    }
    public void BindParentGrid()
    {
        query = "SELECT [Grp_Id] ,[GrpNm]  FROM [dbo].[tbl_Group]";
        SqlDataAdapter sdap = new SqlDataAdapter(query, con);
        DataSet ds = new DataSet();
        sdap.Fill(ds);

        gvParent.DataSource = ds;
        gvParent.DataBind();
        ds.Clear();
    }
    protected void gvParent_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "ShowChild")
        {
            query = "SELECT *  FROM [dbo].[tbl_AccountMaster] where [Grp_Id]=" + e.CommandArgument.ToString() + "";
            SqlDataAdapter sdap = new SqlDataAdapter(query, con);
            DataSet ds = new DataSet();
            sdap.Fill(ds);
            for (int i = 0; i < gvParent.Rows.Count; i++)
            {
                if (e.CommandArgument.ToString() == (gvParent.Rows[i].Cells[0].Text))
                {
                    ((GridView)gvParent.Rows[i].FindControl("gvChild")).DataSource = ds;
                    ((GridView)gvParent.Rows[i].FindControl("gvChild")).DataBind();
                    ((Button)gvParent.Rows[i].FindControl("btnShowChild")).Visible = false;
                    ((Button)gvParent.Rows[i].FindControl("btnHideChild")).Visible = true;
                }
            }
        }
        if (e.CommandName == "HideChild")
        {

            for (int i = 0; i < gvParent.Rows.Count; i++)
            {
                if (e.CommandArgument.ToString() == (gvParent.Rows[i].Cells[0].Text))
                {
                    ((GridView)gvParent.Rows[i].FindControl("gvChild")).DataSource = null;
                    ((GridView)gvParent.Rows[i].FindControl("gvChild")).DataBind();
                    ((Button)gvParent.Rows[i].FindControl("btnShowChild")).Visible = true;
                    ((Button)gvParent.Rows[i].FindControl("btnHideChild")).Visible = false;
                }
            }
        }
    }
}


Put new line character in select statement

Several times we need to show the values in new line which is fetched thorugh select statement and put them in a label or textbox. Then there is a function CHAR() in sql server through which we can get our desired result.
Control character
Value
Tab
char(9)
Line feed
char(10)
Carriage return
char(13)
Example-:

select profile + CHAR(13)+CHAR(10) + address +CHAR(13)+CHAR(10)+phone, export_ref from tbl_profile

Case Statement in Sql Server

We can use CASE expression in the SELECT list, WHERE clauses, HAVING clauses, IN lists, DELETE and UPDATE statements, and even inside of built-in functions.
A Simple Example : 

select case due_amt when 0 then 'Paid' else due_amt end as FeeStatus from dbo.tbl_feeinfo

Insert Data From One Table to Another Table

Before Inserting Data into table we have to consider if table is created or not.If Table is created then we use INSERT INTO SELECT method and in another case SELECT INTO TABLE.

INSERT INTO SELECT  :

INSERT INTO [dbo].[Rpt_WeeklySales]
([Description],[Value],[Week_No],[Year],[fkPartyId])
SELECT B.Description, A.Order_Val, A.Week_No,A.Year,A.fkParty_Id
FROM dbo.Trans_Order as A INNER JOIN dbo.SA_TRANS_ORDER_DETAIL as B
ON A.Order_Id = B.Order_Id

SELECT INTO TABLE :
SELECT B.Description, A.Order_Val as Value, A.Week_No,A.Year,A.fkParty_Id
INTO TestTable
FROM dbo.Trans_Order as A INNER JOIN dbo.SA_TRANS_ORDER_DETAIL as B
ON A.Order_Id = B.Order_Id


Difference between DBMS & RDBMS


What is the difference between DBMS and RDBMS?
1) Data Base Management System is a process of managingdata for efficient retrieval & storage of data.Ex: sysbase , FoxproThe database which is used by relations(tables) toacquire information retrival are known as RDBMSEX: SQL, ORACLE,MY-SQL SERVER2) In DBMS,client server concept is not present. In RDBMS,client server architecture is present (i.e) the client sends the request to the server and the server responds to that particular request.



3) DBMS does not impose any constraints or security withregard to data manipulation. It is user or the programmerresponsibility to ensure the ACID PROPERTY of the database.RDBMS defines the integrity constraint for the purpose ofholding ACID PROPERTY.
4) In DBMS Normalization process will not be present.In RDBMS, normalization process will be present to checkthe database table cosistency.
5) In DBMS we treats Data as Files internally.In RDBMS we treats data as Tables internally.
6) DBMS supports 3 rules of E.F.CODD out off 12 rules.RDBMS supports minimum 6 rules of E.F.CODD.
7) DBMS does not support distributed databases. RDBMS support distributed databases.
8) In DBMS we see small organization with small amount of data. RDBMS designed to take care of large amount of data.
9) DBMS contains only flat data. RDBMS contains some relation between entities.
10) DBMS supports single user RDBMS supports multiple user


The key difference is that RDBMS (relational database management system) applications store data in a tabular form, while DBMS applications store data as files. Does that mean there are no tables in a DBMS? There can be, but there will be no “relation” between the tables, like in a RDBMS. In DBMS, data is generally stored in either a hierarchical form or a navigational form. This means that a single data unit will have one parent node and zero, one or more children nodes. It may even be stored in a graph form, which can be seen in the network model.
In a RDBMS, the tables will have an identifier called primary key. Data values will be stored in the form of tables. The relationships between these data values will be stored in the form of a table as well.  Every value stored in the relational database is accessible. This value can be updated by the system. The data in this system is also physically and logically independent.
You can say that a RDBMS is an in an extension of a DBMS, even if there are many differences between the two. Most software products in the market today are both DBMS and RDBMS compliant. Essentially, they can maintain databases in a (relational) tabular form as well as a file form, or both. This means that today a RDBMS application is a DBMS application, and vice versa. However, there are still major differences between a relational database system for storing data and a plain database system.
DBMS vs. RDBMS

• Relationship among tables is maintained in a RDBMS whereas this not the case DBMS as it is used to manage the database.• DBMS accepts the ‘flat file’ data that means there is no relation among different data whereas RDBMS does not accepts this type of design.• DBMS is used for simpler business applications whereas RDBMS is used for more complex applications.• Although the foreign key concept is supported by both DBMS and RDBMS but its only RDBMS that enforces the rules.• RDBMS solution is required by large sets of data whereas small sets of data can be managed by DBMS.