Saturday, July 10, 2010

Implicitly Typed Array C#

C# 4.0 in a Nutshell: The Definitive Reference
Implicitly typed array is a feature introduced with C# 3.0

that allows compiler to deduce the type of array.


private void Window_Loaded(object sender, RoutedEventArgs e)
{
//1. An implicitly typed array of string
var friends = new[] { "Praseed", "Biju" };
MessageBox.Show(friends.GetType().ToString());

//2.  An implicitly typed array
var points = new[] { 90, 95, 966 };
MessageBox.Show (points.GetType().ToString());


//3. An array of doubles
var price = new[] { 10.40, 10, 60 };
MessageBox.Show (price.GetType().ToString());
}


In this example instead of I giving a type to the arrays

I am expecting the compiler to deduce the type. Compiler no doubt is a smarter guy than me.

The first two examples are self explanatory in nature.

The first example will produce an output of System.String[].

Second example will produce and output of System.Int32[].

In the third case where I am providing multiple types, the compiler will deduce the type that all other given types can be conveted to. In this case it will be producing an array of double.

Tuesday, June 29, 2010

DevCon 2010, Trivandrum

Programming Windows Azure: Programming the Microsoft CloudThe event for software professionals who want to get ahead and stay ahead.

When
03 (Saturday) and 04 (Sunday) – July – 2010
Where
ParkCenter, Technopark, Trivandrum, Kerala, India
Register
Register for DevCon 2010 in Trivandrum, India  on Eventbrite

http://k-mug.org/events/devcon2010/

Sessions

DevCon 2010 – 3rd and 4th July at ParkCenter, Technopark, Trivandrum
Day 1 (3rd July – Saturday)
08:30AM – 09:15AM – Registration Confirmation
09:15AM – 09:30AM – Welcome Speech
09:30AM – 10:15AM – Key Note Session – “Cloud – The Meta Platform”
10:15AM – 11:00AM – New features in .NET 4.0 & Visual Studio 2010
11:00AM – 11:15AM – Tea break
11:15AM – 12:15PM – Robotics Programming
12:15PM – 01:15PM – Web Security and Security Auditing
01:15PM – 02:15PM – Lunch
02:15PM – 03:15PM – Windows Azure
03:15PM – 04:00PM – Great Developer Contest – Final
04:00PM – 05:00PM – Managing Application Compatibility in Windows 7
Day 2 (4th July – Sunday)
08:30AM – 09:30AM – Registration Confirmation
09:30AM – 10:30AM – Data on the Cloud
10:30AM – 11:30AM – Mixed Mode Windows development using C# and C++
11:30AM – 11:45AM – Tea Break
11:45AM – 12:00PM – Visual Studio 2010 tips
12:30PM – 01:30PM – Tuning Tools in SQL Server 2008
01:30PM – 02:30PM – Lunch
02:30PM – 03:15PM – ASP.NET MVC2
03:15PM – 04:00PM – Windows 7 Phone
04:00PM – 05:00PM – Closing Ceremony

Wednesday, May 26, 2010

Asp .Net Web Site Project in Asp .Net 4

Pro ASP.NET 4 in C# 2010, Fourth EditionAsp .Net Web Web Site Project in Asp .Net 4 is an excellent starting point for a new project. It comes packed with Membership, Master Page, Style Sheet etc.














Lets explore the contents of a project. The Accounts folder contains the Membership related forms. Most important being Login.aspx and Register.aspx. Forms Authentication is enabled by default.


In the Styles Folder there is Site.css.


Site.master is the Master Page.


First Let me take Site.master. I am editing the heading 'My ASP.NET Application' to 'Shalvin Content Mangement System' which is an H1.


Now I am selecting Default.aspx. I am editing the existing contents of the page to suit my project.
















Clicking on the Login link at the right side of the page will take you to the Login page.















There is an option for Registering as a new user.


Sunday, May 16, 2010

Silverlight DataBinding with Visual Studio 2010 IDE

Silverlight 4 in ActionI discussed Silverlight Databinding in the blog Silverlight 3 / WPF Databinding. There I tweaked the raw XAML for enabling DataBinding.

The Binding class is used to define a connection between a CLR Object and an User Interface Component. The three elements of binding are Source, Binding Mode and the Target. Source is the CLR Object and Target is the Dependency Property.

Visual Studio 2010 have support for Silverlight DataBinding in the IDE.

As in the previous example here too I am placing a Slider Control with Maximum 100 and a TextBox.

In the properties window there is an Advanced Option icon next the Text in the Text Property.


















Clicking the icon another list will appear. Select Apply Data Binding.


















First you can select the ElementName ie. slider1.











Then select the Path. In our case it is the Value.













This action will automtically generate the Binding Syntax.

















Related Blogs
 

Silverlight Out of Browser Application

Pro Silverlight 4 in C#Starting with Silverlight 3 it is possible to run a Silverlight Application as Out of Browser Application.
Means you can work with yours silverlight Application as if working with a Windows Application.

For enabling this feature Select the Project Properties page by going to Project menu and Select <projectname> Properties.

Check the "Enable running application out of browser" option.

























Now by right clicking the Silverlight application in the Browser you will receive an additional option called "Install HelloSilverlight Application on this computer..".







Clicking the option enables you select the location(s) for the shortcuts.





Friday, April 30, 2010

C# 4 Optional Parameters and COM no ref

Pro .NET 4 Parallel Programming in C#COM had the concept of Optional Parameters. For example the COM TreeView control's Nodes.Add method expects six parameters. The last two parameters and Image and Selected Image.

Working with such controls from C# requires embedding a lot of Type.Missing statements which makes the code bulky as described in the latter part of my blog Using COM Controls in .Net

In that blog I have demonstrated working with COM controls from both VB .Net and C#.

With the introduction of Optional Parameters in C# 4 you can call COM Components by reducing unwanted tweaking.

COM no ref

COM Components also expect certain parameters to be of pass by ref and you had to explicitly mention the ref keyword in the previous version of C#. C# 4 relaxes the code by allowing you to omit the ref modified.

Here is the C# 4 code

private void Form1_Load(object sender, EventArgs e)
{
mscomctl.Node Nodx;
Nodx = axTreeView1.Nodes.Add(Type.Missing, Type.Missing, "India", "India");
Nodx = axTreeView1.Nodes.Add("India", mscomctl.TreeRelationshipConstants.tvwChild, "Kerala", "Kerala");
Nodx.EnsureVisible();

axTreeView1.Nodes.Add(Nodx);
}



Asp .Net MVC 2 with Visual Studio 2010 or Visual Web Developer 2010

Pro ASP.NET MVC 3 Framework, Third EditionAsp .net Mvc is the Web Development framework from Microsoft based on
the MVC architecture.

Asp .Net MVC is getting installed as a part of Visual Studio 2010 and
Visual Web Developer 2010.

Asp .Net 2 MVC Empty Web Application Project

Asp .Net MVC 2 Web Application comes with predefined
Membership and other frills which makes it easy
to get a head start, but abstracts away the inner working of MVC.

But I am taking another route. There is another
Asp .Net MVC 2 Empty Web Application Project.

Asp .Net MVC






















Lets start with Controllers. Controllers are responsible for handling
incoming request. It's a class that inherits from System.Web.Mvc.Controller.























I am naming the controller HomeController.

















I am replacing the existing code of HomeController which expects a View (which will be explained late) with a function that returns a string.
























If you run the application you will see a web page with the value of function.





















Views


Now Let's turn our attention to Views. A view is typically what you see in an Asp .Net MVC Application.

Preferably start a new new Asp .Net MVC 2 Empty Web Application so that we can start afresh.


The following diagram shows the default code within the HomeController class.































If you run the application you will receive a detailed error message stating that the view is missing.

































So right click the Index method and select Add View.



















Make sure you check off the Select MasterPage option because we haven't introduced Master Pages.
A view template called Index.aspx will created for you under ~/Views/Home/ directory.

Which comprises of plain HTML.





































Now if you run the application you will receive a blank form.


Lets replace the existing code with the code below. The code will return a
ViewData structure



public ViewResult Index()
{
ViewData["Blog"] = "ShalvinPD.blogspot.com";
return View();
}



Now lets show the ViewData in the View.
















Asp .Net MVC Views


Easiest way to develop Asp .Net MVC views are by using Html Helpers.


Name <%: Html.TextBox("Name") %>

<br />
<%: Html.CheckBox(".Net", true) %>.Net

<br />
<%: Html.RadioButton("Male", true) %>Male
<br />
<%: Html.RadioButton("Female", false) %>Female
<div>
Password <%: Html.Password("myPassword")%>
</div>
<div>
Address <%: Html.TextArea("Address", "Shalvin P D", 5, 20, null)%>
</div>

Kmug<%: Html.DropDownList("Kmug", new SelectList(new[] { "Shalvin", "Mathew" }), "Shalvin") %>

<br />
Technologies<%: Html.ListBox("myList", new SelectList(new[] { "Asp .Net", "Asp .Net Mvc" }), "Shalvin") %>

Tuesday, April 6, 2010

Asp .Net Membership -III aspnet_regsql command tool, configuring the Site to using Instance Database and Working with Membership and Roles Api

Membership and Role Api Methods


In the blog Asp .Net Membership - II aspnet_regsql to port Membership API database to Sql Server we saw aspnet_regsql tool.


The tool can also be invoked from command prompt, which provides fine grained support on which tables you want to dump to sql server Instance database.

If you want to dump just Membership and Role tables to Sql Server you issue the command as follows:

>aspnet_regsql -S . -E -d Shalvin -A mr


Here -S denoted the Sql Serve instance. Since I am using Sql server 2000 I am specifying a dot. -E denoted integrated security. Optionally you can specify user name and password if you are using Sql Server Security. -d denoted the database name. -A is used for adding the tables. Here I am adding Membership and Role tables.
Like wise you can remove tables using -R.

Next order of business it the instruct your Asp .Net project to use Instance database instead of File System Database.

For that you have edit the web.config as follows:









<configuration>
<connectionStrings>
<add name="ShalvinConnectionString" connectionString="Data Source=.\shalvin;Initial Catalog=Shalvin;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>

<roleManager enabled="true">
<providers>
<clear/>
<add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ShalvinConnectionString"/>
</providers>
</roleManager>

<authentication mode="Forms"/>

<membership>
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ShalvinConnectionString"/>
</providers>
</membership>
</system.web>
</configuration>



Programmatic working with Membership and Roles Api

using System.Web.Security;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddlUsers.DataSource = Membership.GetAllUsers();
ddlRole.DataSource = Roles.GetAllRoles();

DataBind();
}
}
protected void btnAddUserToRole_Click(object sender, EventArgs e)
{
try
{
Roles.AddUserToRole(ddlUsers.Text, ddlRole.Text);
Response.Write("User Added to role");
}
catch (Exception ex)
{
Response.Write(String.Format("{0} is already an {1}",
ddlUsers.Text, ddlRole.Text));
}
}






Related Blog
Asp .Net 2.0 Membership I - LoginStatus, Login and LoginView Controls
Asp .Net Membership - II aspnet_regsql to port Membership API database to Sql Server