Friday, July 20, 2012

Print Selected Row in GridView using VB.NET


<asp:GridView runat="server" ID="grdLaneDoors" ShowHeaderWhenEmpty="true"
                            AutoGenerateColumns="false" DataKeyNames="lane_assignment_id">
                            <Columns>
                           

                                <asp:ButtonField Text="Print BOL" CommandName="p" HeaderText="" ButtonType="Button"
                                        ControlStyle-Width="70Px">
                                        <HeaderStyle BackColor="LightSkyBlue" Font-Bold="False" HorizontalAlign="Center"
                                            ForeColor="Black" BorderColor="LightSkyBlue" BorderWidth="1px" />
                                        <ItemStyle BorderColor="LightSkyBlue" BorderWidth="1px" HorizontalAlign="Center" />
                                    </asp:ButtonField>
                                <asp:BoundField DataField="Created_dt" HeaderText="Created" DataFormatString="{0:MM/dd/yy hh:mm tt}"
                                    HeaderStyle-Wrap="false" ItemStyle-Wrap="false">
                                    <HeaderStyle BackColor="LightSkyBlue" Font-Bold="False" HorizontalAlign="Left" BorderColor="LightSkyBlue"
                                        BorderWidth="1px" ForeColor="Black" />
                                    <ItemStyle HorizontalAlign="Left" BorderColor="LightSkyBlue" />
                                </asp:BoundField>

                                <asp:BoundField DataField="door_no" HeaderText="Door #" HeaderStyle-Wrap="false"
                                    ItemStyle-Wrap="true">
                                    <HeaderStyle BackColor="LightSkyBlue" Font-Bold="False" HorizontalAlign="Center"
                                        BorderColor="LightSkyBlue" BorderWidth="1px" ForeColor="Black" />
                                    <ItemStyle HorizontalAlign="Center" BorderColor="LightSkyBlue" />
                                </asp:BoundField>
                                <asp:BoundField DataField="door_name" HeaderText="Name" HeaderStyle-Wrap="false"
                                    ItemStyle-Wrap="true">
                                    <HeaderStyle BackColor="LightSkyBlue" Font-Bold="False" HorizontalAlign="Left" BorderColor="LightSkyBlue"
                                        BorderWidth="1px" ForeColor="Black" />
                                    <ItemStyle HorizontalAlign="Left" BorderColor="LightSkyBlue" />
                                </asp:BoundField>
                                <asp:BoundField DataField="direction" HeaderText="Direction" HeaderStyle-Wrap="false"
                                    ItemStyle-Wrap="true">
                                    <HeaderStyle BackColor="LightSkyBlue" Font-Bold="False" HorizontalAlign="Left" BorderColor="LightSkyBlue"
                                        BorderWidth="1px" ForeColor="Black" />
                                    <ItemStyle HorizontalAlign="Left" BorderColor="LightSkyBlue" />
                                </asp:BoundField>
                                <asp:BoundField DataField="Trailer_no" HeaderText="Trailer" HeaderStyle-Wrap="false"
                                    ItemStyle-Wrap="true">
                                    <HeaderStyle BackColor="LightSkyBlue" Font-Bold="False" HorizontalAlign="Left" BorderColor="LightSkyBlue"
                                        BorderWidth="1px" ForeColor="Black" />
                                    <ItemStyle HorizontalAlign="Left" BorderColor="LightSkyBlue" />
                                </asp:BoundField>
                                <asp:BoundField DataField="carrier" HeaderText="Carrier" HeaderStyle-Wrap="false"
                                    ItemStyle-Wrap="true">
                                    <HeaderStyle BackColor="LightSkyBlue" Font-Bold="False" HorizontalAlign="Left" BorderColor="LightSkyBlue"
                                        BorderWidth="1px" ForeColor="Black" />
                                    <ItemStyle HorizontalAlign="Left" BorderColor="LightSkyBlue" />
                                </asp:BoundField>
                                <asp:BoundField DataField="Going_to" HeaderText="Destination" HeaderStyle-Wrap="false"
                                    ItemStyle-Wrap="true">
                                    <HeaderStyle BackColor="LightSkyBlue" Font-Bold="False" HorizontalAlign="Left" BorderColor="LightSkyBlue"
                                        BorderWidth="1px" ForeColor="Black" />
                                    <ItemStyle HorizontalAlign="Left" BorderColor="LightSkyBlue" />
                                </asp:BoundField>
                                <asp:BoundField DataField="SupplierCustomer_name" HeaderText="Company" HeaderStyle-Wrap="false"
                                    ItemStyle-Wrap="true">
                                    <HeaderStyle BackColor="LightSkyBlue" Font-Bold="False" HorizontalAlign="Left" BorderColor="LightSkyBlue"
                                        BorderWidth="1px" ForeColor="Black" />
                                    <ItemStyle HorizontalAlign="Left" BorderColor="LightSkyBlue" />
                                </asp:BoundField>
                                <asp:BoundField DataField="supcustloc_name" HeaderText="Company Loc" HeaderStyle-Wrap="false"
                                    ItemStyle-Wrap="true">
                                    <HeaderStyle BackColor="LightSkyBlue" Font-Bold="False" HorizontalAlign="Left" BorderColor="LightSkyBlue"
                                        BorderWidth="1px" ForeColor="Black" />
                                    <ItemStyle HorizontalAlign="Left" BorderColor="LightSkyBlue" />
                                </asp:BoundField>
                                <asp:BoundField DataField="Clear_dt" HtmlEncode="false" HeaderText="Clear Date" DataFormatString="{0:MM/dd/yy hh:mm tt}"
                                    HeaderStyle-Wrap="false" ItemStyle-Wrap="false">
                                    <HeaderStyle BackColor="LightSkyBlue" Font-Bold="False" HorizontalAlign="Left" BorderColor="LightSkyBlue"
                                        BorderWidth="1px" ForeColor="Black" />
                                    <ItemStyle HorizontalAlign="Left" BorderColor="LightSkyBlue" />
                                </asp:BoundField>
                            </Columns>
                        </asp:GridView>




 Public Sub fillGrid()
        grdLaneDoors.DataSource = Nothing
     
        Dim cmd As New SqlCommand("select * from [fnLaneAssignmentDoorsList]() where  location_id=" & hdnLocationID.Value & " and facility_id=" & hdnFacilityID.Value & " and  convert(date,Update_dt)<=convert(date,GETDATE()) order by Update_dt desc")
        Dim dt As DataTable = DataConnect.GetInstance.GetDt(cmd)
        grdLaneDoors.DataSource = dt
        grdLaneDoors.DataBind()
    End Sub



Private Sub grdLaneDoors_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles grdLaneDoors.RowCommand
        Try
            If e.CommandName = "p" Then
                Dim id As Integer
                id = Convert.ToInt32(grdLaneDoors.DataKeys(Convert.ToInt32(e.CommandArgument)).Value)
                grdLaneDoors.SelectRow(Int32.Parse(e.CommandArgument.ToString()))
                Dim html As String = createHTML(id)
                Print(html)
            End If
        Catch ex As Exception
            pnlGlobalMessage.Visible = True
            lblGlobalMessage.Text = "A technical issue has occurred.  A message has been sent to the development team.  Sorry for the inconvenience."
            lblGlobalMessage.Visible = True
            Dim dt As DataTable = Session("user_session")
            Worker.HandleError(ex, "Lane History Print", Int64.Parse(dt.Rows(0)("user_id")), Request.Browser.Browser.ToString())
        End Try
    End Sub

    Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
        'Verifies that the control is rendered
    End Sub
    Protected Function createHTML(ByVal id As Integer) As String
        Dim html As String = "<table cellspacing='0' cellpadding='0' border='1'><tbody>" &
                "<tr>" &
                "<td valign='top' align='left'>" &
                grdLaneDoors.HeaderRow.Cells(3).Text &
                "</td>" &
                "<td valign='top' align='left'>" &
                grdLaneDoors.HeaderRow.Cells(4).Text &
                "</td>" &
                "<td valign='top' align='left'>" &
                grdLaneDoors.HeaderRow.Cells(5).Text &
                "</td>" &
                "<td valign='top' align='left'>" &
                grdLaneDoors.HeaderRow.Cells(6).Text &
                "</td>" &
                "<td valign='top' align='left'>" &
                grdLaneDoors.HeaderRow.Cells(7).Text &
                "</td>" &
                "<td valign='top' align='left'>" &
                grdLaneDoors.HeaderRow.Cells(8).Text &
                "</td>" &
                "<td valign='top' align='left'>" &
                grdLaneDoors.HeaderRow.Cells(9).Text &
                "</td>" &
                "</tr>" &
                "<tr>" &
                "<td valign='top' align='left'>" &
                grdLaneDoors.SelectedRow.Cells(3).Text &
                "</td>" &
                "<td valign='top' align='left'>" &
                grdLaneDoors.SelectedRow.Cells(4).Text &
                "</td>" &
                "<td valign='top' align='left'>" &
                grdLaneDoors.SelectedRow.Cells(5).Text &
                "</td>" &
                "<td valign='top' align='left'>" &
                grdLaneDoors.SelectedRow.Cells(6).Text &
                "</td>" &
                "<td valign='top' align='left'>" &
                grdLaneDoors.SelectedRow.Cells(7).Text &
                "</td>" &
                "<td valign='top' align='left'>" &
                grdLaneDoors.SelectedRow.Cells(8).Text &
                "</td>" &
                "<td valign='top' align='left'>" &
                grdLaneDoors.SelectedRow.Cells(9).Text &
                "</td>" &
                "</tr>" &
                "</tbody></table>"
        Dim con As String = "<table cellpadding='0' cellspacing='0' border='1'><tbody>" &
            "<tr>" &
            "<td valign='top' align='left'>Container ID</td>" &
            "<td valign='top' align='left'>Container Number</td>" &
            "<td valign='top' align='left'>Length</td>" &
            "<td valign='top' align='left'>Width</td>" &
            "<td valign='top' align='left'>Height</td>" &
            "</tr>"
        Dim ast As String = "<table cellpadding='0' cellspacing='0' border='1'><tbody>" &
            "<tr>" &
            "<td valign='top' align='left'>Container ID</td>" &
            "<td valign='top' align='left'>Asset ID</td>" &
            "<td valign='top' align='left'>Total</td>" &
            "</tr>"
        Dim astCntS As String = ""
        Dim astCnt As Integer = 0
        Dim dtCon As New DataTable
        Dim dtAst As New DataTable
        dtCon = DataConnect.GetInstance.GetDt("select tra.container_id,container_nbr,length,width,height, COUNT(*) from transactions tra" &
                    " inner join container con on tra.container_id=con.container_id" &
                    " where lane_assignment_id = " &
                    id.ToString() &
                    " group by tra.container_id,container_nbr,length,width,height")
        For Each dr As DataRow In dtCon.Rows
            con = con & "<tr>" &
                "<td valign='top' align='left'>" &
                dr("container_id").ToString() &
                "</td>" &
                "<td valign='top' align='left'>" &
                dr("container_nbr").ToString() &
                "</td>" &
                "<td valign='top' align='left'>" &
                dr("length").ToString() &
                "</td>" &
                "<td valign='top' align='left'>" &
                dr("width").ToString() &
                "</td>" &
                "<td valign='top' align='left'>" &
                dr("height").ToString() &
                "</td>" &
                "</tr>"
            dtAst = DataConnect.GetInstance.GetDt("select distinct ast.container_id,ast.asset_id,ast.total from transactions tra" &
                    " inner join asset ast on ast.asset_id=tra.asset_id" &
                    " where lane_assignment_id=" &
                    id.ToString() &
                    " and tra.container_id=" &
                    dr("container_id").ToString())
            astCnt += dtAst.Rows.Count
            For Each dr1 As DataRow In dtAst.Rows
                ast = ast & "<tr>" &
                    "<td valign='top' align='left'>" &
                    dr1("container_id").ToString() &
                    "</td>" &
                    "<td valign='top' align='left'>" &
                    dr1("asset_id").ToString() &
                    "</td>" &
                    "<td valign='top' align='left'>" &
                    dr1("total").ToString() &
                    "</td>" &
                    "</tr>"
            Next
        Next
        con = con & "</tbody></table>"
        ast = ast & "</tbody></table>"
        astCntS = "Total number of assets: " & astCnt
        html = html & "<br />" & con & "<br />" & astCntS & "<br />" & ast
        Return html
    End Function
    Protected Sub Print(ByVal html As String)
        Dim sb As New StringBuilder()
        sb.Append("<script type = 'text/javascript'>")
        sb.Append("window.onload = new function(){")
        sb.Append("var printWin = window.open('','','left=0")
        sb.Append(",top=0,width=900,height=800');")
        sb.Append("printWin.document.write(""")
        sb.Append(html)
        sb.Append(""");")
        sb.Append("printWin.document.close();")
        sb.Append("printWin.focus();")
        sb.Append("printWin.print();")
        sb.Append("printWin.close();")
        sb.Append("};")
        sb.Append("</script>")
        ClientScript.RegisterStartupScript(Me.[GetType](), "GridPrint", sb.ToString())
    End Sub





Thursday, May 17, 2012

How to call jQuery code only before the ASP.NET postback



"PostBack is the name given to the process of submitting an ASP.NET page to the server for processing ."



protected void Page_Load(object sender, EventArgs e)
{
   if (IsPostBack)
   {
        ClientScript.RegisterHiddenField("hdnPostBack", "1");
   }
 }


$(document).ready(function () {
    var isPostBackOccured = document.getElementById('hdnPostBack');
    if (isPostBackOccured== null) alert('Page is called first Time');
    else alert('Page is called after Postback');
});

3 Tier Login Example


UI:
Login Page
public partial class login : System.Web.UI.Page
{
businessLogic obj = new businessLogic();

protected void button_Click(object sender, EventArgs e)
{
obj.Username = username.Text;
obj.Password = password.Text;
obj.loginbll();
Response.Redirect("HOme.aspx");
}
}

BLL:

using databaselogic;
public class businessLogic
{

public businessLogic ()
{
databaselogic obj_Bl = new databaselogic();

}
public string _username, _password;

public string Username
{
get { return _username; }
set { _username = value; }
}

public string Password
{
get { return _password; }
set { _password = value; }
}

public void loginbll()
{

obj_Bl.login(Username,Password);

}


DAL:

namespace databaselogic
{

public class databaselogic
{
public databaselogic()
{
}
public void login(string username, string password)
{
//code related to Queries goes on here
}
}
}

OOPS and examples


All the programming languages supporting Object oriented Programming will be supporting these three main concepts:
1. Encapsulation
2. Inheritance
3. Polymorphism

Encapsulation in C#:

Encapsulation is process of keeping data and methods together inside objects. In this way developer must define some methods of object?s interaction. In C# , encapsulation is realized through the classes. A Class can contain data structures and methods. Consider the following class.

public class Aperture
{
public Aperture()
{

}

protected double height;
protected double width;
protected double thickness;

public double GetVolume()
{
double volume = height*width*thickness;
if(volume<0)
return 0;
return volume;
}
}

In this example we encapsulate some data such as height, width, thickness and method GetVolume. Other methods or objects can interact with this object through methods that have public access modifier. It must be done using ?.? operator.


Inheritance in C#:

In a few words, Inheritance is the process of creation new classes from already existing classes. The inheritance feature allows us to reuse some parts of code. So, now we have some derived class that inherits base class?s members. Consider the following code snippet:

public class Door : Aperture
{
public Door() : base()
{

}

public bool isOutside = true;
}

As you see to inherit one class from another, we need to write base class name after ?:? symbol. Next thing that was done in code Door () ? constructor also inherits base class constructor. And at last we add new private field. All members of Aperture class are also in Door class. We can inherit all the members that has access modifier higher than protected.

Polymorphism in C#:

Polymorphism is possibility to change behavior with objects depending of object?s data type. In C# polymorphism realizes through the using of keyword virtual and override. Let look on the example of code:

public virtual void Out()
{
Console.WriteLine("Aperture virtual method called");
} 
//This method is defined in Aperture class. 
public override void Out()
{
Console.WriteLine("Door virtual method called");
}


Now we need to re-define it in our derived Door class. The usage of virtual methods can be clarified when we creating an instance of derived class from the base class:
Aperture ap = new Door();
ap.Out();

Using Authorization with Swagger in ASP.NET Core

 Create Solution like below LoginModel.cs using System.ComponentModel.DataAnnotations; namespace UsingAuthorizationWithSwagger.Models {     ...