<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dahlin Development - Blog &#187; linq</title>
	<atom:link href="http://blog.dahlindevelopment.com/tag/linq/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.dahlindevelopment.com</link>
	<description></description>
	<lastBuildDate>Thu, 26 Aug 2010 18:37:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Create Excel file dynamically (Simple Way)</title>
		<link>http://blog.dahlindevelopment.com/2010/05/create-excel-file-dynamically-simple-way/</link>
		<comments>http://blog.dahlindevelopment.com/2010/05/create-excel-file-dynamically-simple-way/#comments</comments>
		<pubDate>Mon, 17 May 2010 04:31:10 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
				<category><![CDATA[asp.net]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Excel]]></category>
		<category><![CDATA[linq]]></category>

		<guid isPermaLink="false">http://blog.dahlindevelopment.com/?p=247</guid>
		<description><![CDATA[Scenerio: I’ve recently had a a few projects that I’ve created forms for.&#160; I have given the client a login to view all the form submissions, but in the end they want this data in an Excel file so they can easily manage it. Research: I have found there a quite a few ways to [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Scenerio: </strong>I’ve recently had a a few projects that I’ve created forms for.&#160; I have given the client a login to view all the form submissions, but in the end they want this data in an Excel file so they can easily manage it.</p>
<p><strong>Research: </strong>I have found there a quite a few ways to achieve this.&#160; Some take advantage of the Excel interop, some create a .csv, some use third party tools, some use xml. Here is a good discussion I found on the topic at Stack Overflow, <a title="http://stackoverflow.com/questions/151005/create-excel-xls-and-xlsx-file-from-c" href="http://stackoverflow.com/questions/151005/create-excel-xls-and-xlsx-file-from-c">http://stackoverflow.com/questions/151005/create-excel-xls-and-xlsx-file-from-c</a>.     <br />All have pros and cons.</p>
<p><strong>Solution: </strong>Here is what I have found to be the simplest way to read your dataset in an Excel file. First off, this doesn’t product a true Excel file, but it is something Excel can read into a table which often is all you need.</p>
<p>What I found is you can write the contents of a datagrid to an output stream that can be saved as a .xls file.</p>
<p>code snippet: I am using linq to bind to a datagrid</p>
<pre class="brush: csharp;">
var registrations = from reg in db.Registrations
                    where reg.JumpPageId == int.Parse((String)e.CommandArgument)
                    orderby reg.TimeStamp descending
                    select reg;
HttpResponse response = HttpContext.Current.Response;

// first let's clean up the response.object
response.Clear();
response.Charset = &quot;&quot;;

// set the response mime type for excel
response.ContentType = &quot;application/vnd.ms-excel&quot;;
response.AddHeader(&quot;Content-Disposition&quot;, &quot;attachment; filename=registrants.xls&quot;);

// create a string writer
using (StringWriter sw = new StringWriter())
{
    using (HtmlTextWriter htw = new HtmlTextWriter(sw))
    {
        // instantiate a datagrid
        DataGrid dg = new DataGrid();
        dg.DataSource = registrations;
        dg.DataBind();
        dg.RenderControl(htw);
        response.Write(sw.ToString());
        response.End();
    }
}
 </pre>
<p>LIke I said, this doesn’t output a true Excel file so when you go to open it, you will be prompted a warning:</p>
<p><a href="http://blog.dahlindevelopment.com/wp-content/uploads/2010/05/excelwarning.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="excelwarning" border="0" alt="excelwarning" src="http://blog.dahlindevelopment.com/wp-content/uploads/2010/05/excelwarning_thumb.png" width="499" height="74" /></a> </p>
<p>It is saying the contents of the file are in a different format than what the .xls extension says it is.&#160; </p>
<p>Click Yes and it will open.</p>
</p>
<p>If you were to open this file in a text editor you will see that it really is just the datagrid html output.</p>
<p>Not the most elegant solution but it is a quick way to throw an Export to Excel button on a report page.</p>
<p>My next blog post will show how to create an Excel file using a third party tool.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://blog.dahlindevelopment.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://blog.dahlindevelopment.com/2010/05/create-excel-file-dynamically-simple-way/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Linq to Sql- AutoID not working</title>
		<link>http://blog.dahlindevelopment.com/2009/07/linq-to-sql-autoid-not-working/</link>
		<comments>http://blog.dahlindevelopment.com/2009/07/linq-to-sql-autoid-not-working/#comments</comments>
		<pubDate>Sat, 01 Aug 2009 03:09:08 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[linq]]></category>
		<category><![CDATA[MSSQL]]></category>

		<guid isPermaLink="false">http://blog.dahlindevelopment.com/?p=86</guid>
		<description><![CDATA[Using Linq to Sql Auto IDs do not work by default.&#160; Lets say you have a table “Property” with a primary key of “PropertyId”.&#160; In the table definition it is set as a unqueidentifier and by default it is assigned a NewID(). Now go to your Linq to Sql and create your dbml, drag your [...]]]></description>
			<content:encoded><![CDATA[<p>Using Linq to Sql Auto IDs do not work by default.&#160; </p>
<p>Lets say you have a table “Property” with a primary key of “PropertyId”.&#160; In the table definition it is set as a unqueidentifier and by default it is assigned a NewID().</p>
<p><img src="http://www.dahlindevelopment.com/jing/PropertyId.png" /> </p>
<p>Now go to your Linq to Sql and create your dbml, drag your Property table into it.</p>
<p>In order to have the PropertyId generated using the Default Value go&#160; to the properties of the PropertyId field and set <strong>Auto Generate Value</strong> to <strong>True</strong> and <strong>Auto-Sync</strong> to <strong>OnInsert</strong>.</p>
<p><img src="http://www.dahlindevelopment.com/jing/PropertyDbml.png" /></p>
<p>I am sure many already know this, but I hope it can help save some time for others.&#160; </p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://blog.dahlindevelopment.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://blog.dahlindevelopment.com/2009/07/linq-to-sql-autoid-not-working/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
