Like Visual Studio 2008, in Visual Studio 2010 also we have multi-target. So ideally you can create project targeting Framework 2.0, 3.0, 3.5 and 4.0. This behaviour is little different here. Earlier, we have Framework 3,5 carrying both 2.0 and 3.0. So in Visual Studio 2008 it was just the intellisense alter and control load filter. Now in this version of Visual Studio 2010 we do have Framework 4.0 and 3.5 side-by-side support. Now based on your selection it loads CLR version 2.0 or 4.0.
We love Visual Studio.
There is now an option in ADO.Net Entity Framework v2 for us to execute store query,
IEnumerable<Customer> custs = ctx.ExecuteStoreQuery<Customer>("SELECT * FROM Customers", null);
foreach (var c in custs)
{
Console.WriteLine(c.CustomerID);
}
Namoskar!!!
Watch out the videos on Visual Studio 2010 at http://msdn.microsoft.com/en-us/teamsystem/dd441784.aspx
Here are some of them,
Ø Dynamic Programming in Visual C# and Visual Basic
Ø Office Programmability in Visual C# and VisualBasic
Ø Test-driven Development with Generate From Usage
Ø F# in Visual Studio 2010
Ø Deploying Multiple Office Solutions in a Single ClickOnce Installer
Ø Copying a Document to the End User Computer after a ClickOnce Installation
Ø How to Target a Specific .NET Framework Version or Profile
Ø How to Write a Parallel.For Loop with Thread-Local Variables
Ø How to Create and Execute a Simple PLINQ Query
Ø Using Task Groups to Improve Performance
Ø Implementing Futures
Ø Debugging a Parallel Application
Getting data from CSV is one of the mostly used business in applications/tools development.
Here how we can do it in LINQ,
You have a table called Emp with the below details,
CREATE TABLE [dbo].[Emp](
[Id] [int] IDENTITY(1,1) NOT NULL,
[FirstName] [varchar](50) NOT NULL,
[LastName] [varchar](50) NULL,
CONSTRAINT [PK_Emp] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Now you may want to get the CSV out of it. To do that you can save the query output to a .csv file.
The contents of that csv would look like,
1,Wriju,Ghosh
10,Writam,Ghosh
11,Debajyoti,Ghosh
12,Sumitra,Ghosh
82,Tupur,Sanyal
So when you get a single line you can Split() them with a comma (,). The code is very simple,
string[] allLines = File.ReadAllLines(@"E:\Temp\Emp.csv");
var query = from line in allLines
let data = line.Split(',')
select new
ID = data[0],
FirstName = data[1],
LastName = data[2]
};
foreach (var s in query)
Console.WriteLine("[{0}] {1} {2}", s.ID, s.FirstName, s.LastName);
Purpose
Users are allowed to pass Product Key while installing in .msi. During that we want to capture it for registration to avoid piracy.
Challenges
When we use “Customer Information” dialog in “Start” action, it has “SerialNumberTemplate”. But capturing information from there is really tough as mentioned in the article http://support.microsoft.com/kb/253683/en-us. So we have to use the “TextBoxes” dialog and get the data from there.
How
For sample test, I have created on Windows Forms Application “Deployment_SerialNumber”. There I have added InstallerClass called “InstallHelper.cs” with the below code
[RunInstaller(true)]
public partial class InstallerHelp : Installer
public override void Install(IDictionary stateSaver)
base.Install(stateSaver);
string strKey = Context.Parameters["KeyValue"];
string sPath = @"c:\Test.txt";
if (File.Exists(sPath))
File.Delete(sPath);
File.WriteAllText(sPath, strKey);
Then from the Visual Studio menu I have moved to User Interface of Setup project.
Then in the “Start” section I have added TextBoxes(A) and placed between “Installation Folder” and “Confirm Installation”.
In the property window I made the other two text boxes to invisible.
Then moved to “Custom Actions” by VS menu,
Then under “Install” added the Project where I have the InstallHelper.cs class.
Then we have added the “Custom Action Data” to pass the user input through parameter. That is the catch and because of this we can capture it through string strKey = Context.Parameters["KeyValue"];
Notice the Property and the code. Both has “KeyValue”
One caution, The value you pass here cannot be with a space, it will fail (because you are passing parameter). Namoskar!!!
Annotations are used for private use. It does not directly associated and known as Black Box in XML world. This can also be considered as meta tag.
Below is an example on how it can be handled. I have created a sample Windows Applications and will try to capture my understanding.
/// <summary>
/// Class for the Structure of the Data
/// </summary>
public class DataClass
public int ID { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public static XElement xmlData = new XElement("family");
private void Form2_Load(object sender, EventArgs e)
//Create a List in memory
List<DataClass> lstData = new List<DataClass>
new DataClass(){ID = 1, Name = "Debajyoti", Email = "d@contoso.com"},
new DataClass(){ID = 2, Name = "Sumitra", Email = "s@contoso.com"},
new DataClass(){ID = 3, Name = "Wriju", Email = "wg@contoso.com"},
new DataClass(){ID = 4, Name = "Tupur", Email = "t@contoso.com"},
new DataClass(){ID = 5, Name = "Writam", Email = "w@contoso.com"},
//Here create the XML which will hold the info also an annotation
foreach (DataClass data in lstData)
XElement member = new XElement("member",
new XAttribute("ID", data.ID),
new XAttribute("Name", data.Name));
//Adding Annotation to the element
member.AddAnnotation(data.Email);
xmlData.Add(member);
//Loading the List holding two values ID and Name
listBox1.DataSource = lstData;
listBox1.DisplayMember = "Name";
listBox1.ValueMember = "ID";
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
if (listBox1.SelectedIndex > 0)
//By ID we will capture the element to get the annotation
var email = xmlData.Descendants("member").
First(el => el.Attribute("ID").Value == listBox1.SelectedValue.ToString());
//Get the annotation to print that out.
label1.Text = email.Annotations<String>().First();
Resources and excitements are available for you in Visual Studio 2010 Beta 1. Try this out from today at http://go.microsoft.com/fwlink/?LinkId=151799. There are a bunch of resources available for you there apart from the bits.
I have installed it with my museum PC (which hold Visual Studio 6, Visual Studio.NET 2003, Visual Studio.NET 2005, Visual Studio.NET 2008). This version runs without any trouble. But ideally you should be doing it in VPC else use the machine which is not directly associated to production.
ASP.NET MVC book that Scott Hanselman, Rob Conery, Phil Haack and Scott Gu is available at http://aspnetmvcbook.s3.amazonaws.com/aspnetmvc-nerdinner_v1.pdf
When we use Response.Redirect to move from one page to another we ideally also call HTTP 302 Found. This increases extra trip. Now in Visual Studio 2010 you may write
Response.RedirectPermanent("Page1.aspx");
I have spent a while with LINQ and still feel very new whenever I explore some new power. Here I am going to describe you how LINQ uniformly allows you write for the various types of data.
In-memory data source
++++++++++++++++
List<int> arrInt = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
IEnumerable<int> q1 = from i in arrInt
where i % 2 == 0
select i;
Operating System data source (bringing into in-memory)
+++++++++++++++++++++++++++++++++++++
var q2 = from p in System.Diagnostics.Process.GetProcesses()
where p.Threads.Count > 9
orderby p.ProcessName
PName = p.ProcessName,
Count = p.Threads.Count
Reflection data source
+++++++++++++++
var q3 = from m in typeof(string).GetMethods()
where !m.IsStatic
orderby m.Name
group m by m.Name into g
where g.Count() > 2
select new { Name = g.Key, Count = g.Count() };
XML data source
+++++++++++
var data = XElement.Load(@"C:\Custs.xml").Descendants("cust");
var q4 = from c in data
where (string)c.Attribute("Country") == "USA"
select c.Attribute("CompanyName").Value;
LINQ to SQL for Relational data source
++++++++++++++++++++++++++
NWDataContext db = new NWDataContext();
db.Log = Console.Out;
var q = from c in db.Customers
where c.City == "London"
select c.CompanyName;
foreach (var k in q)
Console.WriteLine(k);
When you do not see “Immediate” window under Debug menu of Visual Studio you may get frustrated. But there is a quick way to get it when required,
Go to View > Other Windows > Command Window or press Ctrl+W, A
Then type immed. It will bring the Immediate Window.
And inside the Immediate Window if you type cmd it will bring the Command Window back again.
ASP.Net 3.5 Dynamic Data is a cool thing. I really like the effectiveness of the data driven approach.
Here is a video I have recorded,
In my previous text based blog I had promised to share the recording. Here it is and I am using Silverlight Streaming
Understanding Compiled Query connects me to my experience in C# 3.0. Based on my personal understanding I am discussing it.
You may be aware of Func<>. And you know it is a flexible delegate which allows you create reusable functions. Exactly the same way we can create compiled query so that we can prevent the Expression Compilation during runtime.
Let us first understand how Func works.
You create a Func for square and use it anywhere in your application
Func<double, double> sqr = x => x * x;
Console.WriteLine(sqr(10));
This is precompiled and works just like a variable. So the magic is you declaring a reusable function as variable.
Now assume that it is in form of expression. Things works differently
//Wrap it with Expression<>
Expression<Func<double, double>> sqr = x => x * x;
//Expression cannot be executed but can be viewed
Console.WriteLine(sqr);
//Make it executable you need to call Compile()
var mySqr = sqr.Compile();
//Now this becomes executable
Console.WriteLine(mySqr(5));
So every time you need to call this function you need to call Compile() or at least once.
This works exactly same in LINQ to SQL in Compiled Query. Now notice this below Compiled Query.
Func<NWDataContext, string, IQueryable<Customer>> q =
CompiledQuery.Compile<NWDataContext, string, IQueryable<Customer>>
((NWDataContext nw, string sCity) =>
from o in nw.Customers
where o.City == sCity
select o);
It is doing nothing but creating Func for your LINQ to SQL.
using (NWDataContext db = new NWDataContext())
foreach (Customer c in q(db, "London"))
Console.WriteLine(c.CompanyName);
It is very powerful and performance effective.