Tuesday, August 1, 2017

Implement Google Column Stack Charts With Link integration

Please find the following code.

<!DOCTYPE html>
<html lang="en-US">
<body>

<h1>My Web Page</h1>

<div id="piechart" style="width:80%;height:600px;"></div>

<script type="text/javascript" src="https://www.google.com/jsapi"></script>

<script type="text/javascript">
      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = google.visualization.arrayToDataTable([
          ['Division', 'Compliances','link', 'Non Compliances'],
            ['ILTD', 500,'http://en.wikipedia.org/wiki/2003', 100],
            ['PSPD', 682,'http://en.wikipedia.org/wiki/2004', 200],
            ['LRBD', 1874,'http://en.wikipedia.org/wiki/2005', 180],
            ['ESPB', 1324,'http://en.wikipedia.org/wiki/2006', 99],
            ['CORP', 457,'http://en.wikipedia.org/wiki/2007', 56],
            ['PSPB', 1598,'http://en.wikipedia.org/wiki/2008', 45]
        ]);
       var view = new google.visualization.DataView(data);
       view.setColumns([0,1,3]);

       var options = {
            title: 'Summary Report',
            /*width:600, height:400,
            vAxis: {title: "Year"},
            hAxis: {title: "Cups"}*/
            legend: { position: 'bottom', maxLines: 3 },
            isStacked: true,
            series: {              
                0: { color: '#109618' },
                1: { color: '#dc3912' }             
            }
        };

       var chart = new google.visualization.ColumnChart(
           document.getElementById('piechart'));
       chart.draw(view, options);

       var selectHandler = function(e) {
          window.location = data.getValue(chart.getSelection()[0]['row'], 2 );
       }

       google.visualization.events.addListener(chart, 'select', selectHandler);
      }
    </script>

</body>
</html>

Tuesday, September 27, 2016

Export ASPX GridView control to Excel

GridView control in aspx page
Report.aspx
<asp:Button ID="btnExport" runat="server" Text="Export To Excel" OnClick ="btnExport_Click" />
            <br /><br />
            <asp:GridView ID="GridView1" PageSize="5" AllowPaging="True" DataKeyField="PSID" OnRowDataBound="RowDataBound" AutoGenerateColumns="False" runat="server">
                <Columns>
                    <asp:BoundField DataField="PSID" HeaderText="PSID" SortExpression="PSID"></asp:BoundField>
                    <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name"></asp:BoundField>
                    <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email"></asp:BoundField>
                    <asp:BoundField DataField="Area" HeaderText="Area" SortExpression="Area"></asp:BoundField>
                    <asp:BoundField DataField="Department" HeaderText="Department" SortExpression="Department"></asp:BoundField>               
                    <asp:BoundField DataField="Attempted" HeaderText="No of Attemped" SortExpression="Attempted"></asp:BoundField>
                    <asp:BoundField DataField="AcceptedStatus" HeaderText="Status" SortExpression="AcceptedStatus"></asp:BoundField>
                </Columns>
            </asp:GridView>


Report.aspx.cs

 protected void Page_Load(object sender, EventArgs e)
        {

            using (SqlConnection con = new SqlConnection(conString))
            {

             if(!IsPostBack){
                 Status.Items.Add(new ListItem("All Status", ""));
                Status.Items.Add(new ListItem("Accepted", "1"));
                Status.Items.Add(new ListItem("Decline", "0"));
               }


                using (SqlCommand cmd = new SqlCommand("SELECT * FROM TBL_TRN_REPORT", con))
                {
                    cmd.CommandType = CommandType.Text;
                    using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                    {
                        using (DataTable dt = new DataTable())
                        {
                            sda.Fill(dt);
                            GridView1.DataSource = dt;
                            GridView1.DataBind();
                        }
                    }
                }
            }
        }

 protected void btnExport_Click(object sender, EventArgs e)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xlsx");
            Response.Charset = "";
            Response.ContentType = "application/vnd.ms-excel";
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            GridView1.RenderControl(hw);
            Response.Write(sw.ToString());
            Response.End();
           
        }

        public override void VerifyRenderingInServerForm(Control control)
        {
            //It solves the error "Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server."
        }

Friday, January 1, 2016

Ajax Call from Web form application

Hi,
I had a problem while i trying to submit variable value with dynamic button control.
The example was like

<% foreach(int productId in products){ %>
<asp:Button ID="Button2" onclick="callfunction" 
CommandArgument="<%= productId %>" runat="server" Text="Button" />
<% }?>

But i found it's not working because of control value will not assign after page_load. Then i try to do it with AJAX

I have change the Button control with HTML Button

<% foreach(int productId in products){ %>
 <input id="Button1" onclick="AddtoCartItems(<%=product.ProductId %>)" type="button" value="Add To Cart" /> 
<% }?>

After that i have written the following ajax code.

<script type="text/javascript">            
       function AddtoCartItems(pid) {    
                 $.ajax({
                     url: 'AddToCart.aspx/GetData',
                     type: "POST",
                     dataType: "json",
                     data: "{'name': '" + pid + "'}",
                     contentType: "application/json; charset=utf-8",
                     success: function (data) {
                         alert(JSON.stringify(data));
                     }
             });
       }
    </script>


Now i have create AddToCart.aspx With GetData Methord

using System.Web.Services;
using Newtonsoft.Json;


[WebMethod]
        public static string GetData(string name)
        {
           --------Your code here-----------

           return "Success";
        }


2 more thing you need to do.
Go to App_Start/RouteConfig.cs 
Change 
settings.AutoRedirectMode = RedirectMode.Permanent;       
           TO
settings.AutoRedirectMode = RedirectMode.Off;

Same As go to Web.config and add the following line
<system.web>
  ... 
    <webServices>
        <protocols>
            <add name="HttpGet"/>
            <add name="HttpPost"/>
        </protocols>
    </webServices>
    <authorization>
      <allow users="*"/>
    </authorization>
 
  </system.web>


That's it. Now you can call your AJAX method successfully.    

Monday, December 28, 2015

Date time discussion

Finally i found how to convert time stamp to Date & Date to time stamp . I found some places in project people keep date as time stamp for get difference quickly. so in this case they use to keep the table column as Int or time stamp. now the problem is that in the application while showing the data, you need to convert it into date variable. So for that we can use the following code to convert time stamp to Date
 
int ts = 1451174400;
DateTime dt = new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(ts).ToLocalTime();
string formattedDate = dt.ToString("dd-MM-yyyy");
 
Now you can get any date format from this variable.
In the second case if you want to convert Date to time stamp then check the following code.

int ts = (dt.Ticks - 621356166000000000) / 10000000;
Where dt is the date time variable & holding a date value.

If you want to convert string to date find the following code.
DateTime dt = Convert.ToDateTime("2015-12-13");