Tuesday, July 3, 2012

.NET N-Tier Architecture


1- Business Logic Layer 
    it is a class 
    we have table in database called Branch with columns(pk_branch_id,mobile,branch_name,address,notes)
    so for each column or field we create property to set and get values


public class BusinessLogicLayer
{
    #region Variables

    private int pk_branch_id, mobile;
    private string branch_name, address, notes;

    #endregion

    #region Properties

    public int Branch_ID
    {
        get { return pk_branch_id; }
        set { pk_branch_id = value; }
    }

    public string Branch_Name
    {
        get { return branch_name; }
        set { branch_name = value; }
    }

    public string Branch_Address
    {
        get { return address; }
        set { address = value; }
    }
    public int Branch_Mobile
    {
        get { return mobile; }
        set { mobile = value; }
    }

    public string Branch_Notes
    {
        get { return notes; }
        set { notes = value; }
    }

    #endregion


2 - Data Access Layer
      it is a class
      which is responsible for creating connection , open, close it, create command and execute it

public class DataAccessLayer
{
    SqlConnection conn;
    SqlCommand cmd;
    public DataAccessLayer() // Constructor
    {
conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ADSL_DBConnectionString"].ConnectionString);
        conn.Open();
    }

    public bool Insert_Branch(BusinessLogicLayer bll_branch_INS)
    {
        using (cmd = new SqlCommand("PRC_INSERT_BRANCH", conn))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            
            cmd.Parameters.AddWithValue("@Branch_Name", bll_branch_INS.Branch_Name);
            cmd.Parameters.AddWithValue("@Mobile", bll_branch_INS.Branch_Mobile);
            cmd.Parameters.AddWithValue("@Address", bll_branch_INS.Branch_Address);
            cmd.Parameters.AddWithValue("@Notes", bll_branch_INS.Branch_Notes);
            int nn = cmd.ExecuteNonQuery();
            if (nn > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

    public bool Update_Branch( BusinessLogicLayer  bll_branch_UPD)
    {
        using (cmd = new SqlCommand("PRC_UPDATE_BRANCH", conn))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Pk_Branch_Id", bll_branch_UPD.Branch_ID);

            cmd.Parameters.AddWithValue("@Branch_Name", bll_branch_UPD.Branch_Name);
            cmd.Parameters.AddWithValue("@Mobile", bll_branch_UPD.Branch_Mobile);
            cmd.Parameters.AddWithValue("@Address", bll_branch_UPD.Branch_Address);
            cmd.Parameters.AddWithValue("@Notes", bll_branch_UPD.Branch_Notes);
            
            if (cmd.ExecuteNonQuery() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

    public Boolean Delete_Branch(int id)
    {
        using (cmd = new SqlCommand("PRC_DELETE_BRANCH", conn))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Pk_Branch_Id", id);

            if (cmd.ExecuteNonQuery() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

    public void Select_Branch_BYID(int id)
    {
        using (cmd = new SqlCommand("PRC_SELECTBRANCH_BYID", conn))
        {
            BusinessLogicLayer  bll_branch = new BusinessLogicLayer();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Pk_Branch_Id", id);
            SqlDataReader rdr;
            rdr = cmd.ExecuteReader();
            if (rdr.Read())
            {
            bll_branch.Branch_Name = rdr["Branch_Name"].ToString();
            bll_branch.Branch_Mobile = Convert.ToInt32(rdr["Mobile"]);
            bll_branch.Branch_Address = rdr["Address"].ToString();
            bll_branch.Branch_Notes = rdr["Notes"].ToString();
            }
        }
    }

    public void Fill_GridView(GridView GV)
    {
        using (cmd = new SqlCommand("PRC_FILL_GV", conn))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            DataTable dt = new DataTable();
            SqlDataAdapter adpr = new SqlDataAdapter(cmd);
            adpr.Fill(dt);
            GV.DataSource = dt;
            GV.DataBind();
        }
    }

    public void Fill_DropDownList(DropDownList DDL, string datatext, string datavalue)
    {
        using (cmd = new SqlCommand("PRC_FILL_DDR", conn))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataReader rdr;
            rdr = cmd.ExecuteReader();
            DDL.DataSource = rdr;
            DDL.DataTextField = datatext;
            DDL.DataValueField = datavalue;
            DDL.DataBind();
        }
    }

}

- MultiView



Multi View (Source Code ASP.NET)

    <asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">
        <asp:View ID="View1" runat="server">
            <table>
                <tr>
                    <td width="200" align="center">
                        <asp:Label ID="lblNameINS" runat="server" Text="User Name "></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="txtNameINS" runat="server" Width="200"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td width="200" align="center">
                        <asp:Label ID="lblAddINS" runat="server" Text="User Address "></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="txtAddINS" runat="server" Width="300"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td width="200" align="center">
                        <asp:Label ID="lblAgeINS" runat="server" Text="User Age "></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="txtAgeINS" runat="server" Width="100"></asp:TextBox>
                    </td>
                    <td width="200" align="right"><asp:LinkButton runat="server" ID="lnkbtnINS" 
                            Text="Go to Update" onclick="lnkbtn_Click"></asp:LinkButton></td>
                </tr>
            </table>
        </asp:View>
        <asp:View ID="View2" runat="server">
            <table>
                <tr>
                    <td width="200" align="center">
                        <asp:Label ID="lblNameUPD" runat="server" Text="User Name U"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="txtNameUPD" runat="server" Width="200"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td width="200" align="center">
                        <asp:Label ID="lblAddUPD" runat="server" Text="User Address U"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="txtAddUPD" runat="server" Width="300"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td width="200" align="center">
                        <asp:Label ID="lblAgeUPD" runat="server" Text="User Age U"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="txtAgeUPD" runat="server" Width="100"></asp:TextBox>
                    </td>
                
                <td width="200" align="right">
                    <asp:LinkButton runat="server" ID="lnkbtnUPD" 
                            Text="Go to Insert" onclick="lnkbtnUPD_Click"></asp:LinkButton></td>
                
                </tr>
                
            </table>
        </asp:View>
     
    </asp:MultiView>

 protected void lnkbtn_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 1;
    }
    protected void lnkbtnUPD_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 0;
    }

Sunday, July 1, 2012

- SQL


SELECT MAX (stdDegree)FROM dbo.STD WHERE stdDegree IN
(SELECT DISTINCT TOP 2 stdDegree FROM dbo.STD 
ORDER BY stdDegree DESC)