Xceedas

Xceedas
xceedas

Wednesday 24 September 2014

Rename Website Project in Visual Studio 2013

Introduction 
You cannot rename a website type project in Visual Studio 2013 by simply right click and rename...!!

This issue is caused by IIS Express. You can solve it by changing the site name in applicationhost.cofig of your IIS Express. While changing the name, make sure that this name is unique.

Rename website in IIS Express
Steps to modify applicationhost.cofig
  1. Find the applicationhost.cofig of your IIS Express, usually it should be listed in Folder C:\Users{username}\Documents\IISExpress\config
  2. Locate the corresponding site section of your project in the cofig file, shown below.>
    1. <sites>    
    2.   <site name="WebSite1" id="1" serverAutoStart="true">    
    3.     <application path="/">    
    4.       <virtualDirectory path="/" physicalPath="%IIS_SITES_HOME%\WebSite1" />    
    5.     </application>    
    6.     <bindings>    
    7.       <binding protocol="http" bindingInformation=":8080:localhost" />    
    8.     </bindings>    
    9.   </site   
  3. Rename "WebSite1" to "WebSite2" and make sure that "WebSite2" is not already in use. Save the file.
  4. Close and Reopen your solution in Visual Studio, you'll found that the specif name for your website project has been renamed.
Thank you

Visual Studio 2013 Update 4 CTP 2 is available

Visual Studio 2013 Update 4 CTP 2 is available, including a few features (e.g. performance improvements for the Visual C++ browsing experience) and some bug fixes.
Finally, Visual Studio Tools for Unity (VSTU) 1.9.1 on the Visual Studio Gallery: VSTU for VS 2013VSTU for VS 2012, and VSTU for VS 2010. This release fixed many of the issues .

Tuesday 23 September 2014

IQ Game - Test Your Memory

Mirshahibk.jpg


This is a test your visual memory game. Run the game and memorize the location of each image on the screen and then click back to match the image.


Sample code:

        public void showpic() // show each picture if clicked by computer...
        {
            gn = new generate();
            for (int k = 0; k < global.siz; k++)
            { 
                switch (j[k])
                {
                    case 1:
                        plym1();
                        gn.pp1.Visible = true;
                        gn.pp1.Image = global::WindowsFormsApplication3.Properties.Resources.christmas_ball_1;
                        gn.pp2.Visible=false;
                        gn.pp3.Visible = false;
                        gn.pp4.Visible = false;
                        gn.ShowDialog(); 
                        break;
                    case 2:
                        plym2();
                        gn.pp2.Visible = true;
                        gn.pp2.Image = global::WindowsFormsApplication3.Properties.Resources.christmas_ball_2;
                        gn.pp1.Visible = false;
                        gn.pp3.Visible = false;
                        gn.pp4.Visible = false;
                        gn.ShowDialog(); 
                        break;
                    case 3:
                        plym3();
                        gn.pp3.Visible = true;
                        gn.pp3.Image = global::WindowsFormsApplication3.Properties.Resources.christmas_ball_3;
                        gn.pp2.Visible = false;
                        gn.pp1.Visible = false;
                        gn.pp4.Visible = false;
                        gn.ShowDialog();
                        break; 
                    case 4:
                        plym4();
                        gn.pp4.Visible = true;
                        gn.pp4.Image = global::WindowsFormsApplication3.Properties.Resources.christmas_ball_5;
                        gn.pp2.Visible = false;
                        gn.pp3.Visible = false;
                        gn.pp1.Visible = false;
                        gn.ShowDialog(); 

                        break;
                }
            } 
        } 

            Random r = new Random(); // generate random number between 1 to 4
            a = r.Next(1, 5);
            while (i != global.siz)
            {
                j[i] = 0;
                a = r.Next(1, 5);
                { 
                    j[i] += a;

Rock-Paper-Scissors Game in C#

Rock-Paper-Scissors Game
Here I am going to present a famous game named “Rock-Paper-Scissors” developed in C#. A sample code is also attached.

Rule

Rock-paper-scissors is a hand game usually played by two people, where players simultaneously form one of three shapes with an outstretched hand. The "rock" beats scissors, the "scissors" beat paper and the "paper" beats rock; if both players throw the same shape, the game is tied.

Rule

For more details on this game, you may refer to following link:
Rock-paper-scissors 

This game is implemented using C#. It is played between a User and Computer. On each round, this lets the user select his choice out of Rock, paper and Scissors. Once user selects his choice, now computer randomly picks its choice and then program decides who won that round. This program also keeps on storing all the previous game results. Following are the some screen shots in various stages of the game running.

Screenshot at Startup
Screenshot at startup

Scenario 1: When Rock breaks Scissors
 
Rock breaks Scissors

Scenario 2: When Scissors cuts Paper
 
Scissors cuts Paper

Scenario 3: When Paper covers Rock
 
Paper covers Rock

Posting Data to MVC Action Using KnockoutJS

Overview 

In this article we will learn how to post data to a MVC action using KnockoutJS.

Demo

In this demo application we will create a simple form for capturing product information like product code, product name and price and submit this information to a MVC action.

Let's start by creating a demo application. I am using Visual Studio 2012 Ultimate edition for this demo.

Procedure

1. Create a new MVC 4 web application project and select the Basic template.

2. Add a controller “HomeController” and “Index” view.

3. Now we will add the required scripts. Make the following changes in App_Start -> BundleConfig.cs and Views->Shared->_layout.chtml.

BundleConfig.cs



_Layout.chtml



4. Now we will create a Product model and ProductViewModel. Create an App folder under the Scripts folder and add a product.js file and add the following code to it. Now we have a ProductViewModel with currentProduct property initialized with some test data. 



Now we will bind the ProductViewModel with the view which is Index.chtml. We have made all the properties of the Product model and ProductViewModel observable so that whenever there is a change in the View it will automatically update the ViewModel. This is the beauty of the Knockout framework and that is why we call it a binding library. 

5. Let's add the following markup into our Index.html.

In the view we need to first add the reference of the product.js.,  Next we need to bind our viewmodel with the view using the applyBindings method., Next we are using with: binding to bind the currentProduct view model property to the product section (<div>) and also bind the child controls with the currentProduct sub properties. So whenever there is a change in the control value it will be reflected in the currentProduct object.
 
6. Now at this point we are ready to run the application and we should see the index view auto-populated with the product information. Now press F5.



7. Now there are the following two ways to POST the data to a MVC action:
  • Using $.ajax (ajax call to post the data): In this case the MVC action should return the JsonResult and control will return to the callback function either in SUCCESS or ERROR. But we can't use this approach if we want to redirect our control to some other action.
  • Another way is to use a Knockout builtin function (ko.postJSON). Here you can redirect to some other page after saving.
We will look at option (b) below.

8. Now the next task is to POST the changes to the MVC action on the click of the Save button. Let's create a method “saveProduct” in our view model and bind it with the onclick event of the Save button. Let's add the following code to product.js and index.chtml.

Product.js



Index.chtml



9. Now the next thing we need to do is to convert our view model object into a JSON object so that we can post it to a MVC action. To do this we will use the Knockout mapping plugin “knockout.Mapping 2.4.0” which is available as Nuget.

Go to "TOOLS" -> "Library Package Manager" -> "Package Manager Console".

Install the Nuget as shown below. It will add “knockout.mapping-latest.js” into the Script folder.



Add the “knockout.mapping-latest.js” into the existing knockout script bundle.


Now we are ready to use the knockout mapping plugin in our demo.

10. Now create the class ProductModel under the Model folder which should contain the same properties as the Product class in product.js. It will look as in the following:



Now create an Action called “SaveProduct” which should be called when we click on the “Save” button and we will use “ProductModel” as an input parameter.



Now go to the product.js and first convert the knockout model into JS then post it to the MVC action “SaveProduct”. Product.js will look like as below. We will use the ko.utils.postJson function to post the JSON form of our product model to the MVC action.



Now if we run the application and hit “Save”, we should receive the data in the product model as a parameter in the “SaveProduct” action but we did not, we received NULL instead.



The reason is MVC default model binding is unable to de-serialize the Knockout model (in JSON format) into a ProductModel. So we will need to create a custom model binding attribute to de-serialize the knockout model into MVC ProductModel. 



In the code above we have used the JavaScriptSerializer to de-serialize the JSON data which is taken from the request object.

Now apply the attribute on SaveProduct action's input parameter and run the application again.



Now we are receiving the update model in our MVC action.

Introduction to Knockout.js



Let's try to figure out what knockout.js is as in the following:
  1. Knockout.js is a JavaScript library allowing us to bind HTML elements to a data-model. It provides a two-way communication between data model and HTML element means any changes made in any of them will be reflected to each other.
  2. Knockout.js is also preferred to create Declarative Bindings allowing us to bind DOM/HTML elements with any Data Model. The data-bind attribute of knockoutjs is used for this connectivity.
  3. Dependency Tracking: As I said earlier, knockout.js provides two-way bindings, its easy to update only that portion of the UI rather than rendering an entire portion of the UI. And it is done using the observables attribute of knockout.js.
  4. Knockoutjs also allows us to use the power of a template engine to show a complex data view into a cleaner version of the UI.
Now just above, I tried to make you all understand in a nutshell about Knockoutjs.

Still people have the misunderstanding that Knockoutjs is a replacement for jQuery. So here is the answer of that question.

Knockout.js is not a replacement to jQuery. It doesn't try to provide animation or any generic event handling, However Knockout.js can parse the data received from an AJAX call.

The main purose of Knockout.js is to for designing a scalable and data-driven User Interface.

One more important thing about Knockout.js is that it follows a Model-View-ViewModel (MVVM) design pattern.



Model: Model is your data.

View: This is the interface part of the application.

Veiw-Model: This represents temporary data and logic on which application is working with. Mostly they are pure JavaScript objects. 

Add Countries in Your DropDownList Using WebService

Here is the code to add countries in your DropDownList using WebService.

Here I am using a webservice from http://www.webservicex.net and a link to the webservice used is http://www.webservicex.net/country.asmx.

In the .aspx page put a 
DropDownList as in the following:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form2" runat="server">
    <div>
    <asp:DropDownList ID="drpCNT" runat="server"></asp:DropDownList>
    </div>
    </form>
</body>
</html>

Now to add the reference of your webservice, do following 
procedure:
  1. Open Solution Explorer, select your website and right-click and select "Add Web Reference"
  2. Now the following window will be opened, enter "http://www.webservicex.net/country.asmx" in the URL area that is circled in the image and press Enter.

    Country-DropDownList-1.jpg
  3. Now change Web reference name (optional) and click the "Add Reference" Button (circled in the image).

    Country-DropDownList-2.jpg

    After that it will create the following structure in Solution Explorer.

    Country-DropDownList-3.jpg
     
  4. Now put the following code in the .aspx.cs file (explained in code):
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Xml;
    using System.Text;

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                //get reference of your webservice
                myservice.country ct = new myservice.country();

                // str is an XML String which will hold all the countries in xml format
                string str = ct.GetCountries();

                // add first item in dropdownlist
                drpCNT.Items.Add("-Select-");

                //Create an XML Document and load your XML
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(str);

                //Get your nodes, here our node in Table because webservice will give us following format
                // http://www.webservicex.net/country.asmx/GetCountries
                XmlNodeList nodes = doc.DocumentElement.SelectNodes("//Table");

                //Iterates for xml nodes and add them in dropdownlist
                foreach (XmlNode node in nodes)
                {
                    drpCNT.Items.Add(node["Name"].InnerText);
                }
            }

        }
    } 
     
  5. Finally you will get all the countries in your DropDownList.

Get Cities by Country Using Web Service Without DataBase

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head runat="server">  
  6.     <title>article By vithal wadje</title>  
  7. </head>  
  8. <body style="background-color: #C0C0C0">  
  9.     <form id="form1" runat="server">  
  10.     <h2 style="color: #808000; font-size: x-large; font-weight: bolder;">  
  11.         Article by vithal wadje</h2>  
  12.     <br />  
  13.     <table style="margin-left: 30Px">  
  14.         <tr>  
  15.             <th>  
  16.                 Enter Country  
  17.             </th>  
  18.             <td>  
  19.                 <asp:TextBox ID="TextBox1" AutoPostBack="true" runat="server" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>  
  20.             </td>  
  21.         </tr>  
  22.         <tr>  
  23.             <td>  
  24.             </td>  
  25.             <td>  
  26.             </td>  
  27.         </tr>  
  28.         <tr>  
  29.             <td>  
  30.             </td>  
  31.             <td>  
  32.                 <asp:GridView ID="GridView1" runat="server" AllowPaging="True" OnPageIndexChanging="PageIndexChanging"  
  33.                     CellPadding="3" GridLines="Vertical" BackColor="White" BorderColor="#999999"  
  34.                     BorderStyle="None" BorderWidth="1px">  
  35.                     <AlternatingRowStyle BackColor="#DCDCDC" />  
  36.                     <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />  
  37.                     <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />  
  38.                     <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />  
  39.                     <RowStyle BackColor="#EEEEEE" ForeColor="Black" />  
  40.                     <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />  
  41.                     <SortedAscendingCellStyle BackColor="#F1F1F1" />  
  42.                     <SortedAscendingHeaderStyle BackColor="#0000A9" />  
  43.                     <SortedDescendingCellStyle BackColor="#CAC9C9" />  
  44.                     <SortedDescendingHeaderStyle BackColor="#000065" />  
  45.                 </asp:GridView>  
  46.             </td>  
  47.         </tr>  
  48.         <tfoot>  
  49.             <tr>  
  50.                 <td>  
  51.                 </td>  
  52.                 <td>  
  53.                     <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>  
  54.                 </td>  
  55.             </tr>  
  56.         </tfoot>  
  57.     </table>  
  58.     </form>  
  59. </body>  
  60. </html> 
  61.  Consume Web Service in the web application

    Now the next step is to consume the GetCitiesByCountry web service in our web application. The following is the URL of the GetCitiesByCountry Web services.

    http://www.webservicex.net/globalweather.asmx?WSDL
  62. Now open the Default.aspx Page code behind of our web application and create the object of the web service class as: 
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using System.Web.UI;  
    6. using System.Web.UI.WebControls;  
    7. using System.Data;  
    8. using System.Xml;  
    9. using System.Xml.Serialization;  
    10. using System.Text;  
    11. using System.Drawing;  
    12.   
    13. public partial class _Default : System.Web.UI.Page  
    14. {  
    15.     DataSet ds;  
    16.     protected void Page_Load(object sender, EventArgs e)  
    17.     {  
    18.          
    19.     }  
    20.   
    21.     private string GetCitiesByCountry(string CountryName) //taking the input from User  
    22.     {  
    23.         //Creating the object of web service GlobalWeather class with the help of service Reference   
    24.         GetCitiesByCountryServiceRef.GlobalWeather obj = new GetCitiesByCountryServiceRef.GlobalWeather();    
    25.   
    26.         return obj.GetCitiesByCountry(CountryName); //passing country name input to Service  
    27.     }  
    28.     protected void TextBox1_TextChanged(object sender, EventArgs e)  
    29.     {  
    30.   
    31.         // calling GetCities() method on the change event of textbox  
    32.         GetCities();    
    33.   
    34.     }  
    35.   
    36.     private void GetCities()  
    37.     {  
    38.         //ensuring whether the country Name is not empty  
    39.         if (TextBox1.Text == "")  
    40.         {  
    41.              
    42.             Label1.Text = "Please Enter Country Name";  
    43.         }  
    44.         else  
    45.         {  
    46.             //getting the service Response to string   
    47.             string Data = GetCitiesByCountry(TextBox1.Text);  
    48.   
    49.             //converting the string Respsonse to XML   
    50.             XmlTextReader xtr = new XmlTextReader(new System.IO.StringReader(Data));  
    51.             ds = new DataSet();  
    52.             ds.ReadXml(xtr);  
    53.             if (ds.Tables.Count >=1 && ds.Tables[0].Rows.Count >= 1)  
    54.             {  
    55.                 //Binding Gridview  
    56.                 GridView1.DataSource = ds.Tables[0];  
    57.                 GridView1.DataBind();  
    58.                 Label1.Text = ds.Tables[0].Rows.Count.ToString() + "    Records Found";  
    59.             }  
    60.             else  
    61.             {  
    62.                 Label1.ForeColor = Color.Red;  
    63.                 Label1.Text = "Please Check the Spell of Entered Country Name   "+TextBox1.Text;  
    64.               
    65.             }  
    66.           
    67.         }  
    68.   
    69.   
    70.     }  
    71.     protected void PageIndexChanging(object sender, GridViewPageEventArgs e)  
    72.     {  
    73.         GridView1.PageIndex = e.NewPageIndex;  
    74.         GetCities();  
    75.   
    76.     }  
    77. }