Wednesday 25 July 2012

How to select values from a datatable based on the condition

// here 'm datatable from cache which is already stored, u can careate ur own datatable  
              
               string district = e.Row.Cells[0].Text.ToString();

                string cachekey = "PPNPreauthDistrictMIS_" + Session.SessionID;
                DataTable NewGdt = (DataTable)HttpRuntime.Cache[cachekey];

                DataRow[] rows = NewGdt.Select("DISTRICT = '" + district + "'");


                 dt = new DataTable();
                dt.Columns.Add("SL No");
                dt.Columns.Add("DISTRICT");
                dt.Columns.Add("PKG_CATEGORY");
                dt.Columns.Add("CASES");
                dt.Columns.Add("AMT");
                dt.Columns.Add("TOTALAPPAMOUNT");

                foreach (DataRow dr in rows)
                {

                    object[] row = dr.ItemArray;

                    dt.Rows.Add(row);

                    casesum = casesum + Convert.ToInt32(dr["CASES"]);
                    pratesum = pratesum + Convert.ToInt32(dr["AMT"]);
                    appamtsum = appamtsum + Convert.ToInt32(dr["TOTALAPPAMOUNT"]);
                }

                DataRow newrow = dt.NewRow();

                newrow["PKG_CATEGORY"] = "Total";
                newrow["CASES"] = casesum.ToString();
                newrow["AMT"] = pratesum.ToString();
                newrow["TOTALAPPAMOUNT"] = appamtsum.ToString();
                dt.Rows.Add(newrow);
                ChildGridview.DataSource = dt;
                ChildGridview.DataBind();
                int count = ChildGridview.Rows.Count;
                ChildGridview.Rows[count - 1].Font.Bold = true;





Caluclating the sum of all the rows in a datatable and place it in the last row

  // here 'm datatable from cache which is already stored, u can careate ur own datatable    

                DataTable NewGdt = (DataTable)HttpRuntime.Cache[cachekey];

                int casesum = 0;
                int pratesum = 0;
                int appamtsum = 0;
                DataTable dt = null;

                DataTable StatusDT = null;
                OleDbCommand cmd = null;
                OleDbDataAdapter StatusAdpt = null;

                DataRow[] rows = NewGdt.Rows;

                dt = new DataTable();
                dt.Columns.Add("SL No");
                dt.Columns.Add("DISTRICT");
                dt.Columns.Add("PKG_CATEGORY");
                dt.Columns.Add("CASES");
                dt.Columns.Add("AMT");
                dt.Columns.Add("TOTALAPPAMOUNT");

                foreach (DataRow dr in rows)
                {

                    object[] row = dr.ItemArray;

                    dt.Rows.Add(row);

                   // here 'm getting sum on each row

                    casesum = casesum + Convert.ToInt32(dr["CASES"]);
                    pratesum = pratesum + Convert.ToInt32(dr["AMT"]);
                    appamtsum = appamtsum + Convert.ToInt32(dr["TOTALAPPAMOUNT"]);
                }

                DataRow newrow = dt.NewRow();

                newrow["PKG_CATEGORY"] = "Total";
                newrow["CASES"] = casesum.ToString();
                newrow["AMT"] = pratesum.ToString();
                newrow["TOTALAPPAMOUNT"] = appamtsum.ToString();
                dt.Rows.Add(newrow);

                /// 'm binding final table to gridview

                ChildGridview.DataSource = dt;
                ChildGridview.DataBind();
                int count = ChildGridview.Rows.Count;
                ChildGridview.Rows[count - 1].Font.Bold = true;

How to select a distinct values from particular column in the datatable

// here 'm datatable from cache which is already stored, u can careate ur own datatable  

                    DataTable NewGdt = (DataTable)HttpRuntime.Cache[cachekey];
                    DataView Griddataview = new DataView(NewGdt);


                    DataTable distinctValues = Griddataview.ToTable(true, "Columnname");

How to implement Nested Gridview (Gridview within a gridview)



In Design page:

 <asp:GridView CssClass="content" ID="GVDistrict" runat="server" Width="100%" HorizontalAlign="Left" DataKeyNames="DISTRICT"
 AutoGenerateColumns="False" CaptionAlign="Left" OnRowDataBound="GVDistrict_OnRowDataBound"   >
 <Columns>
 <asp:BoundField DataField="DISTRICT" HeaderText="District">
     <HeaderStyle Width="12%" />
 </asp:BoundField>
   <asp:TemplateField HeaderText="Category Details">
 <ItemTemplate>

 <asp:GridView CssClass="content" ID="CategoryDeatils" runat="server" Width="100%" CaptionAlign="Left"
  AutoGenerateColumns="False" AllowSorting="True" HorizontalAlign="Left" >
  <columns>
   <asp:BoundField Datafield="PKG_CATEGORY" HeaderText="Category" />
    <asp:BoundField Datafield="CASES" HeaderText="#Cases" />
     <asp:BoundField Datafield="AMT" HeaderText="Package Amt." />     
    <asp:BoundField Datafield="TOTALAPPAMOUNT" HeaderText="Total App. Amt."/>
  </columns>
  <HeaderStyle CssClass="headerContent" HorizontalAlign="Left" />
 <AlternatingRowStyle HorizontalAlign="Left" BackColor="GhostWhite" />
 </asp:GridView>


 </ItemTemplate>
 </asp:TemplateField>
 </Columns>
 <HeaderStyle CssClass="headerContent" HorizontalAlign="Left" />
 <AlternatingRowStyle HorizontalAlign="Left" BackColor="GhostWhite" />
 </asp:GridView>




In code behind:

protected void bindparentgridview()

            
    {       

                  try
                  {

                    // Get some data in datatable and bind it to parent gridview


                    GVDistrict.DataSource = Gdt;
                    GVDistrict.DataBind();
                  }
                   
                
                   catch (HttpException ex)
                 {
                   Lblmsg.Text = "There seems to be problem in Network Connectivity. Please try again." + ex.ToString();
                 }
                   catch (Exception ex)
                 {
                   Lblmsg.Text = "There seems to be an Exception.  !!" + ex.ToString();
                 }

             
    }




 protected void GVDistrict_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {



        if (e.Row.RowType == DataControlRowType.DataRow)
        {
           
           // get childgridview control here

           GridView ChildGridview = ((GridView)e.Row.FindControl("CategoryDeatils"));


          
            try
            {

              // get some datatable dt and bind the childgridview here

               
                ChildGridview.DataSource = dt;
                ChildGridview.DataBind();


            }
            catch (HttpException ex)
            {
                Lblmsg.Text = "There seems to be problem in Network Connectivity. Please try again." +    ex.ToString();
            }
            catch (Exception ex)
            {
                Lblmsg.Text = "There seems to be an Exception.  !!" + ex.ToString();
            }


        }

    }



Monday 2 July 2012

How to get only valid dates from a varchar date field in oracle





First we need to create a function as below:


CREATE OR REPLACE FUNCTION MY2DATE (p_str IN VARCHAR2
  ,format_picture IN VARCHAR2
)
   RETURN DATE
IS
BEGIN
   RETURN TO_DATE(p_str, format_picture);
EXCEPTION
   WHEN OTHERS
   THEN
      RETURN NULL;
END;




Call that function with field name and the valid date format while selecting:

SELECT MY2DATE(FIRST,'dd/mm/YYYY HH:MM AM') FROM ORDERS