Tuesday, February 17, 2015

PIVOT TABLE IN C SHARP

Here is the Table  and I want to convert this rows into Colums. 





Default.aspx:

<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server"></asp:GridView>
    </div>
    </form>
</body>

</html>


.cs page Code.

protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("   ");
        SqlCommand cmd = new SqlCommand("select * from country", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        DataTable Return = GetDataTable(dt, "CountryID");
        GridView1.DataSource = Return;
        GridView1.DataBind();
    }

    public static DataTable GetDataTable(DataTable table, string columnXAxis,
                                                 params string[] columnsToIgnore)
    {
        DataTable table1 = new DataTable();

        if (columnXAxis == "")
            columnXAxis = table.Columns[0].ColumnName;

        table1.Columns.Add(columnXAxis);
        List<string> columnXValues = new List<string>();

        List<string> listColumnsToIgnore = new List<string>();
        if (columnsToIgnore.Length > 0)
            listColumnsToIgnore.AddRange(columnsToIgnore);

        if (!listColumnsToIgnore.Contains(columnXAxis))
            listColumnsToIgnore.Add(columnXAxis);
        
        foreach (DataRow dr in table.Rows)
        {
            string columnXTemp = dr[columnXAxis].ToString();
            if (!columnXValues.Contains(columnXTemp))
            {
                columnXValues.Add(columnXTemp);
                table1.Columns.Add(columnXTemp);
            }
            else
            {
                throw new Exception("Insertion Failed" + columnXAxis);
            }
        }
        foreach (DataColumn dc in table.Columns)
        {
            if (!columnXValues.Contains(dc.ColumnName) &&
                !listColumnsToIgnore.Contains(dc.ColumnName))
            {
                DataRow dr = table1.NewRow();
                dr[0] = dc.ColumnName;
                table1.Rows.Add(dr);
            }
        }

        for (int i = 0; i < table1.Rows.Count; i++)
        {
            for (int j = 1; j < table1.Columns.Count; j++)
            {
                table1.Rows[i][j] =
                  table.Rows[j - 1][table1.Rows[i][0].ToString()].ToString();
            }
        }
        return table1;

    }


Output:




Google Translate using Web API

Click on the following LINK

https://translate.google.com/manager/website/



Click on Add to your Website Now



Click on Next 



Write the following Code in webform.

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <meta name="google-translate-customization" content="2b3b4b31accf3a87-34ae433b7837812c-ge03cc87601ebf729-12"></meta>
</head>
<body>
    <form id="form1" runat="server">
       
        <div id="google_translate_element">
            Uday Kumar
        </div>
        <script type="text/javascript">
            function googleTranslateElementInit() {
                new google.translate.TranslateElement({ pageLanguage: 'en', layout: google.translate.TranslateElement.InlineLayout.SIMPLE }, 'google_translate_element');
            }
        </script>
        <script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
        
    </form>
</body>
</html>


Output:




Why Multiple Inheritance is supported using Interface


Using Class: It gives me error as shown below.

A Class cannot have multiple Base Classes




We can see in the above example, When i am inherting two classes to Class C, It gives me an error.


Using Interface:








Output:






Example 2:




,
Conclusion:

Use Abstract Class as a Parent class for big projects where most of functionality is common in all the Child  classes. 

Use an interface where, in every Child class, you want to define all the methods of the interface. 

Monday, February 16, 2015

MVC Custom Routing Simple Example

File ->New->Project
Note: Always select project when working with mvc (there is no option to work with file->new ->website) in MVC


Another Window will be displayed asking for the type of MVC Application we want to create

Select Project Template as


Internet Application




And Click on Ok Button


And Go to Controller Folder and Right Click  àAddController.




Write the following Code in Controller.

 public class Default1Controller : Controller

    {
       
        public ActionResult Index()
        {
            return Content("<h2> Index action </h2>");
        }

        public ActionResult register(String id)

        {
            return Content("<h2>" + id + " Registered Successfully </h2>");
        }

        public ActionResult ResultLogin(string uname, string pwd)

        {
            if (uname == "uday" && pwd == "uday")
                return Content("<h2> Valid </h2>");
            else
                return Content("<h2> Invalid </h2>");
        }
    }

Next





Write the Following Code in RouteConfig.cs

  
public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(

                name: "Default",
                url: "{uname}/{pwd}",
                defaults: new { controller = "Default1", action = "ResultLogin", uname = UrlParameter.Optional, pwd = UrlParameter.Optional }
            );

            routes.MapRoute(

                name: "Default2",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Default1", action = "Index", id = UrlParameter.Optional }
            );
        }
    }


Output:

Now, calling the Method register as follows






Now, calling the Method ResultLogin as follows


Validate Multiple Textboxes, Check boxes using jquery(Eg Textbox)

<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
        <input id="btnSubmit" type="button" value="button" />
    </div>
    </form>
</body>



<head runat="server">
    <title></title>
    <script type="text/javascript"></script>
    <script src="Jquery.js"></script>
    <script src="JavaScript2.js"></script>

</head>


Javascript2.js

$(document).ready(function () {
    $('#btnSubmit').click(function (e) {
        var isValid = true;
        $('input[type="text"]').each(function () {
            if ($.trim($(this).val()) == ' ') {
                isValid = false;
                $(this).css({
                    "border": "2px solid blue",
                    "background": "#FFCERE"
                });
            }
            else {
                $(this).css({
                    "border": "",
                    "background": ""
                });
            }
        });
        if (isValid == false)
            e.preventDefault();
        else
            alert('Thank you ');
    });
});


Output:





To Validate all Check Boxes

$("input[type='checkbox']:checked").each(

            function () {
});

Kubernetes

Prerequisites We assume anyone who wants to understand Kubernetes should have an understating of how the Docker works, how the Docker images...