• Json Request Behaviour in MVC2

    by Venkata Koppaka | Nov 23, 2010

    If you are working with a MVC2 project, or happen to upgrade a MVC1 Project to a MVC2 Project you might run to some issues while getting Json data using AJAX calls.

    Specifically MVC 2 will give you an error which says

    This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet.

    To get past this error all you need to do is Allow Json data to be gettable in your Controller Action method.

    You can obtain this by the following line –

    1 return Json(data, JsonRequestBehavior.AllowGet); 

    The above line will solve your problem.

    Hope this helped,

    Cheers,

    Venkata


    Go comment!
  • Changing you Application Title in Windows Phone 7

    by Venkata Koppaka | Nov 21, 2010

    This is another quick post in which I will show you how to change Application Title in Windows Phone 7 Application List.

    Here is how the "Save the world Ninja!" app title is looking right now (If you haven't had a chance to read the post I did on how to create the Save the world Ninja! app I would suggest that you do here.) 

    Now I want to change this title to something more meaningful.

    To change the title of your app in Windows Phone 7 App list, open up WMAppManifest.xml file in the properties folder of your Windows Phone 7 Project.

    In the WMAppManfiest.xml file you will find a node called "App" and change change that line to be something like this

    1 <App xmlns="" ProductID="{97a383c7-abb9-40ec-839d-0ac3aa168ccb}" Title="Save the World, Ninja!" RuntimeType="Silverlight" Version="1.0.0.0" Genre="apps.normal"  Author="Venkata Koppaka" Description="Save your world Ninja!" Publisher="Venkata Koppaka"> 

    Note the title in the above XML has been changed to "Save the World, Ninja!". I actually changed the Application icon the Windows Phone 7 App shows, here is how you would put your own  icon.

    1 <IconPath IsRelative="true" IsResource="false">ninja.png</IconPath> 

    And now if I run my App this is how it would look like -

    If you wish to checkout the updated code, its checked in to GitHub here -https://github.com/subbaraokv/Hello-World---Windows-Phone-7

    Hope this helped,

    Cheers,

    Venkata

    Go comment!
  • Bringing up Software Keyboard in Windows Phone 7

    by Venkata Koppaka | Nov 20, 2010

    This post is short and quick one, in the previous post I created a Hello World Windows Phone 7 App. If you haven't read that already please do so here.

    In this post I will just add a small functionality to the app. I wanted the software keyboard on the Windows Phone 7 device to come up as soon as the App loads.

    Generally the software keyboard in Windows Phone 7 automatically pops up whenever a textbox control has a focus on the phone, but I since my Ninja App only had one textbox it makes sense to automatically bring up the software keyboard as soon as the app loads so the user can start typing in right away.

    Here is how you would do it. There is a "PhoneApplicationPage_Loaded" event that gets fired on the app load.

    So all you have to do is to set the focus on a textbox in the loaded event and the software keyboard will automatically showup. 

    Here is the code sample for doing so -

    1 private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) 
    2         { 
    3             tbNinjaPhrase.Focus(); 
    4         } 
    That's it.

    If you wish to checkout the updated code, its checked in to GitHub here -https://github.com/subbaraokv/Hello-World---Windows-Phone-7.

    Hope this helped,

    Cheers,

    Venkata


    Go comment!
  • Building Hello World! Windows Phone 7 App

    by Venkata Koppaka | Nov 19, 2010

    In this post I will walk you through on how to build a Hello World Windows Phone 7 App. The "Hello World" app that I am building in this tutorial is a "Save the World" app in which if you enter a correct pass phrase you will save the world for you :), if not you cannot save the world :P

    To get started you will need Windows Phone 7 Developer Tools, you can download them here.

    Once you download and install Windows Phone 7 Developer Tools, Create a Windows Phone Application.

    You will be presented with a dialog which would look like this -

    The first thing I did after creating the app is to rename Application Title and Page Title. Here is what I renamed them to

    1 <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> 
    2             <TextBlock x:Name="ApplicationTitle" Text="My Ninja App" Style="{StaticResource PhoneTextNormalStyle}"/> 
    3             <TextBlock x:Name="PageTitle" Text="Save the world" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> 
    4         </StackPanel> 

    And this is what the Phone Emulator looked after I renamed -

    I will now drag couple of textblocks, a textbox and a button on to the Phone designer and then rename them to something which makes more sense, here is the XAML -

    1      <TextBox Height="72" HorizontalAlignment="Left" Margin="12,116,0,0" Name="tbNinjaPhrase" Text="" VerticalAlignment="Top" Width="460" /> 
    2             <TextBlock Height="79" HorizontalAlignment="Left" Margin="25,31,0,0" Name="textBlock1" Text="What is your Ninja pass phrase to save the world?" VerticalAlignment="Top" Width="425" TextWrapping="Wrap" /> 
    3             <Button Content="Save the World!" Height="69" HorizontalAlignment="Left" Margin="109,194,0,0" Name="btnSaveTheWorld" VerticalAlignment="Top" Width="249" Click="btnSaveTheWorld_Click" /> 
    4             <TextBlock Height="114" HorizontalAlignment="Left" Margin="25,269,0,0" Name="messageBlock" Text="" VerticalAlignment="Top" Width="425" TextWrapping="Wrap" /> 

    and this is how the phone UI will look like after dragging all the UI elements -

    Then I double click on button and put in this code in the code behind -

    1 private void btnSaveTheWorld_Click(object sender, RoutedEventArgs e) 
    2         { 
    3             string ninjaPhrase = tbNinjaPhrase.Text; 
    4             if (!string.IsNullOrEmpty(ninjaPhrase)) 
    5             { 
    6                 if (string.Compare("startrek", ninjaPhrase, StringComparison.OrdinalIgnoreCase) == 0) 
    7                 { 
    8                     messageBlock.Text = "Vola! Way to go Ninja, You saved the world!"; 
    9                 } 
    10                 else 
    11                 { 
    12                     messageBlock.Text = "That is not a Ninja Phrase! You cannot save the world!"; 
    13                 } 
    14             } 
    15             else 
    16             { 
    17                 messageBlock.Text = "Are you kidding? That is not a Ninja Phrase! You cannot save the world!"; 
    18             } 
    19         } 
    I run the App and vola!,

    Here is what you would see when you put it "stratrek" as pass code :)

    And if you put in some other pass phrase you would see "You didn't save the world" :)

    And that is your first Hello World Windows Phone 7 App.

    If you wish to checkout the code here is the code on GitHub https://github.com/subbaraokv/Hello-World---Windows-Phone-7

    Hope this helped,

    Cheers,

    Venkata

    Go comment!
  • Peforming CRUD with LINQ to SQL

    by Venkata Koppaka | Oct 28, 2010

    LINQ to SQL is an ORM(Object-relational mapping) tool introduced by Microsoft.

    In this post I will walk through how to perform basic CRUD(Create, Retrieve, Update, Delete) operations using LINQ to SQL.

    (NOTE: This post assumes you have basic knowledge of using LINQ to SQL like adding the DBML class and dragging tables on to a DBML designer)

    Lets get started -

    The table we are going use for this example is a simple Person Table. Here is the schema for the table -

     

    Create

    To perform a Create operation on a table using LINQ to SQL you need to create an instance of the class and populate the data into the instance. Once you are done call "InsertOnSubmit" and "SaveChanges" to insert the object.

    Here is the code sample for doing so -

    1 using (var context = new TestDbDataContext()) 
    2             { 
    3                 Person aPersonToInsert = new Person(); 
    4                 aPersonToInsert.Name = "Test User"; 
    5                 aPersonToInsert.Age = 26; 
    6                 aPersonToInsert.Email = "test@test.com"; 
    7                 context.Persons.InsertOnSubmit(aPersonToInsert); 
    8                 context.SubmitChanges(); 
    9             } 

    The above code will insert the object only after context.SubmitChanges() is called.

    Retrieve

    To perform a retrieve from a table using you can use one of the two syntax. The first one is a normal LINQ syntax which reads more like plain SQL. The second syntax is called "Lamda" syntax which is just a more convenient way to create a delegate.

    Here are the code samples for doing both.

    1 using (var context = new TestDbDataContext()) 
    2             { 
    3                 //Normal Syntax 
    4                 Person person = (from aPerson in context.Persons 
    5                                  where aPerson.PersonId == 1 
    6                                  select aPerson).SingleOrDefault(); 
    7  
    8                 //Lambda Syntax 
    9                 Person personLamda = context.Persons.SingleOrDefault(p => p.PersonId == 1); 
    10                  
    11             } 

    The SingleOrDefault() method above gets a single entity that matches the criteria or returns NULL if nothing is found.

    Update

    To perform a Update on a database record using LINQ to SQL, you have to get the record using one of the two syntax shown above and make changes to record and then Submit the changes to the database using SubmitChanges().

    Here is the code sample for doing so -

    1 using (var context = new TestDbDataContext()) 
    2             { 
    3                 //Lambda Syntax 
    4                 Person person = context.Persons.SingleOrDefault(p => p.PersonId == 1); 
    5                 person.Name = "Updated User"; 
    6                 person.Age = 99; 
    7                 person.Email = "noone@someone.com"; 
    8  
    9                 context.SubmitChanges(); //this will save your changes to the database 
    10             } 

    Again please note unless you call SubmitChanges() none of your changes are saved to the database.

     

    Delete

     Delete operation using LINQ to SQL is similar to Update operation. You have to get the record, delete the record and Submit the changes to the database using SubmitChanges().

    Here is the code for doing so -

    1 using (var context = new TestDbDataContext()) 
    2             { 
    3                 //Lambda Syntax 
    4                 Person person = context.Persons.SingleOrDefault(p => p.PersonId == 1); 
    5  
    6                 context.Persons.DeleteOnSubmit(person); 
    7                 context.SubmitChanges(); //this will save your changes to the database 
    8             } 

    Again unless you call SubmitChanges() nothing is deleted from the database.

    So we are done performing a simple CRUD operations using LINQ to SQL.

    Hope you find it useful,

     Cheers,

    Venkata

    Go comment!