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