<?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>Naresh.Jois &#187; ASP.net</title>
	<atom:link href="http://www.nareshjois.com/tag/aspnet/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.nareshjois.com</link>
	<description>Home of a professional developer &#38; a aspiring photographer</description>
	<lastBuildDate>Fri, 03 Feb 2012 16:57:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Asp.net ReportViewer : Access Remote Server Report with Credentials</title>
		<link>http://www.nareshjois.com/2011/09/asp-net-reportviewer-access-remote-server-report-with-credentials/</link>
		<comments>http://www.nareshjois.com/2011/09/asp-net-reportviewer-access-remote-server-report-with-credentials/#comments</comments>
		<pubDate>Fri, 09 Sep 2011 07:38:30 +0000</pubDate>
		<dc:creator>naresh</dc:creator>
				<category><![CDATA[.net Programming]]></category>
		<category><![CDATA[Blog]]></category>
		<category><![CDATA[ASP.net]]></category>
		<category><![CDATA[credentials]]></category>
		<category><![CDATA[networkcredentials]]></category>
		<category><![CDATA[report server]]></category>
		<category><![CDATA[ssrs]]></category>

		<guid isPermaLink="false">http://www.nareshjois.com/?p=913</guid>
		<description><![CDATA[If you want to work with remote SSRS report while debugging your asp.net application you would want to authenticate with the remote server, you cannot use asp.net impersonation as this would actually make the entire application run under impersonation so to solve this we can use impersonation for the report viewer only to do this [...]]]></description>
			<content:encoded><![CDATA[<p>If you want to work with remote SSRS report while debugging your asp.net application you would want to authenticate with the remote server, you cannot use asp.net impersonation as this would actually make the entire application run under impersonation so to solve this we can use impersonation for the report viewer only to do this :</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:consolas, monaco;"><span style="color: #008080;">#if (DEBUG)</span>
<span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ReportViewer1</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ServerReport</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ReportServerCredentials</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ReportServerCredentials<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;username&quot;</span>, <span style="color: #666666;">&quot;password&quot;</span>,<span style="color: #666666;">&quot;domain&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008080;">#endif</span></pre></div></div>

<p>and a <b>ReportServerCredentials</b> class</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:consolas, monaco;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> ReportServerCredentials <span style="color: #008000;">:</span> IReportServerCredentials
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> _userName<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> _password<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> _domain<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> ReportServerCredentials<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> userName, <span style="color: #6666cc; font-weight: bold;">string</span> password, <span style="color: #6666cc; font-weight: bold;">string</span> domain<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            _userName <span style="color: #008000;">=</span> userName<span style="color: #008000;">;</span>
            _password <span style="color: #008000;">=</span> password<span style="color: #008000;">;</span>
            _domain <span style="color: #008000;">=</span> domain<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Security</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Principal</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">WindowsIdentity</span> ImpersonationUser
        <span style="color: #008000;">&#123;</span>
            get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Net</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">ICredentials</span> NetworkCredentials
        <span style="color: #008000;">&#123;</span>
            get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">new</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Net</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">NetworkCredential</span><span style="color: #008000;">&#40;</span>_userName, _password, _domain<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
&nbsp;
        <span style="color: #6666cc; font-weight: bold;">bool</span> IReportServerCredentials<span style="color: #008000;">.</span><span style="color: #0000FF;">GetFormsCredentials</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">out</span> Cookie authCookie, <span style="color: #0600FF; font-weight: bold;">out</span> <span style="color: #6666cc; font-weight: bold;">string</span> userName, <span style="color: #0600FF; font-weight: bold;">out</span> <span style="color: #6666cc; font-weight: bold;">string</span> password, <span style="color: #0600FF; font-weight: bold;">out</span> <span style="color: #6666cc; font-weight: bold;">string</span> authority<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            authCookie <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
            userName <span style="color: #008000;">=</span> _userName<span style="color: #008000;">;</span>
            password <span style="color: #008000;">=</span> _password
            authority <span style="color: #008000;">=</span> _domain<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.nareshjois.com/2011/09/asp-net-reportviewer-access-remote-server-report-with-credentials/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Asp.net Dyanamic Data : A leap for RAD</title>
		<link>http://www.nareshjois.com/2011/06/asp-net-dyanamic-data-a-leap-for-rad/</link>
		<comments>http://www.nareshjois.com/2011/06/asp-net-dyanamic-data-a-leap-for-rad/#comments</comments>
		<pubDate>Thu, 23 Jun 2011 06:29:41 +0000</pubDate>
		<dc:creator>naresh</dc:creator>
				<category><![CDATA[.net Programming]]></category>
		<category><![CDATA[ASP.net]]></category>
		<category><![CDATA[Dynamic Data]]></category>

		<guid isPermaLink="false">http://www.nareshjois.com/?p=878</guid>
		<description><![CDATA[I have always basically forms over data for the longest period of time (LOB Apps) and most of the time its the same thing over and over again, Setup Database, create scaffolding forms for all the master tables and then move to the next steps, but wait there should be an easy way to this, [...]]]></description>
			<content:encoded><![CDATA[<p>I have always basically forms over data for the longest period of time (LOB Apps) and most of the time its the same thing over and over again, Setup Database, create scaffolding forms for all the master tables and then move to the next steps, but wait there should be an easy way to this, In the MVC framework you can generate pages based on the model, but there are still a few things that you should wire up manually, enter dynamic data template and this feels like a magical unicorn at the beginning and once you dig deep into into it, its just beautiful, and will save a lot of steps for you.<br />
So lets begin</p>
<div id="attachment_879" class="wp-caption alignnone" style="width: 310px"><a href="http://www.nareshjois.com/wp-content/uploads/2011/06/newddproject.png" rel="lightbox[878]"><img class="size-medium wp-image-879" title="New Dynamic Data WebApplication" src="http://www.nareshjois.com/wp-content/uploads/2011/06/newddproject-300x182.png" alt="New Dynamic Data WebApplication" width="300" height="182" /></a><p class="wp-caption-text">New Dynamic Data WebApplication</p></div>
<p>You have 2 options a Linq2SQL based Application or Entity Framework based application and its just a matter of your preference, Then just create a data Context, This is what i did just to go through and this is not a perfect data model but you can get the point, just create your data context you are almost good to go</p>
<div id="attachment_880" class="wp-caption alignnone" style="width: 522px"><a href="http://www.nareshjois.com/wp-content/uploads/2011/06/ddmodel.png" rel="lightbox[878]"><img class="size-full wp-image-880" title="Dynamic Data : A Test Model" src="http://www.nareshjois.com/wp-content/uploads/2011/06/ddmodel.png" alt="Dynamic Data : A Test Model" width="512" height="258" /></a><p class="wp-caption-text">Dynamic Data : A Test Model</p></div>
<p>then you ll have to uncomment a single line in global.asax</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:consolas, monaco;">DefaultModel<span style="color: #008000;">.</span><span style="color: #0000FF;">RegisterContext</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>DataModel<span style="color: #008000;">&#41;</span>, <span style="color: #008000;">new</span> ContextConfiguration<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> ScaffoldAllTables <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Here the default property for ScaffoldAllTables is false, change it to true if you want scaffolding on all tables, (for now)<br />
and you are done<br />
And this is what you get , Projects and Tasks along with filter and foreign key correctly linked</p>
<div id="attachment_882" class="wp-caption alignnone" style="width: 487px"><a href="http://www.nareshjois.com/wp-content/uploads/2011/06/ddstep1.png" rel="lightbox[878]"><img class="size-full wp-image-882" title="Tables Listing" src="http://www.nareshjois.com/wp-content/uploads/2011/06/ddstep1.png" alt="" width="477" height="403" /></a><p class="wp-caption-text">Tables Listing</p></div>
<div id="attachment_883" class="wp-caption alignnone" style="width: 500px"><a href="http://www.nareshjois.com/wp-content/uploads/2011/06/ddstep2.png" rel="lightbox[878]"><img class="size-full wp-image-883" title="Projects Listing" src="http://www.nareshjois.com/wp-content/uploads/2011/06/ddstep2.png" alt="" width="490" height="465" /></a><p class="wp-caption-text">Projects Listing</p></div>
<div id="attachment_884" class="wp-caption alignnone" style="width: 500px"><a href="http://www.nareshjois.com/wp-content/uploads/2011/06/ddstep3.png" rel="lightbox[878]"><img class="size-full wp-image-884" title="Task Listing" src="http://www.nareshjois.com/wp-content/uploads/2011/06/ddstep3.png" alt="" width="490" height="465" /></a><p class="wp-caption-text">Task Listing</p></div>
<div id="attachment_885" class="wp-caption alignnone" style="width: 580px"><a href="http://www.nareshjois.com/wp-content/uploads/2011/06/ddstep4.png" rel="lightbox[878]"><img class="size-full wp-image-885" title="Task Insertion" src="http://www.nareshjois.com/wp-content/uploads/2011/06/ddstep4.png" alt="" width="570" height="541" /></a><p class="wp-caption-text">Task Insertion</p></div>
<p>Now lets customize this a little bit, we would never want all the scaffold all tables and also we would need some validation and hearer names to be changed a bit.<br />
To Scaffold specific tables we will have to mark the tables with [ScaffoldTable(true)] property, and as the Datacontext generated classes are partials we can extend the classes in a seperate file also to provide metadata we will use data annotations and mark all the necessary fields with the required information</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:consolas, monaco;"><span style="color: #0600FF; font-weight: bold;">namespace</span> TestApplication
<span style="color: #008000;">&#123;</span>
    <span style="color: #008000;">&#91;</span>ScaffoldTable<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
    <span style="color: #008000;">&#91;</span>MetadataType<span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>Attributes<span style="color: #008000;">.</span><span style="color: #0000FF;">Project</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">partial</span> <span style="color: #6666cc; font-weight: bold;">class</span> Project
    <span style="color: #008000;">&#123;</span>
&nbsp;
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008000;">&#91;</span>ScaffoldTable<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
    <span style="color: #008000;">&#91;</span>MetadataType<span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>Attributes<span style="color: #008000;">.</span><span style="color: #0000FF;">Task</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">partial</span> <span style="color: #6666cc; font-weight: bold;">class</span> Task
    <span style="color: #008000;">&#123;</span>
&nbsp;
    <span style="color: #008000;">&#125;</span>
&nbsp;
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">namespace</span> Attributes
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">partial</span> <span style="color: #6666cc; font-weight: bold;">class</span> Project
        <span style="color: #008000;">&#123;</span>
            <span style="color: #008000;">&#91;</span>Required<span style="color: #008000;">&#93;</span>
            <span style="color: #008000;">&#91;</span>StringLength<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">50</span>, MinimumLength <span style="color: #008000;">=</span> <span style="color: #FF0000;">5</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
            <span style="color: #008000;">&#91;</span>Display<span style="color: #008000;">&#40;</span>Name <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Project Name&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
            <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> ProjectName <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">partial</span> <span style="color: #6666cc; font-weight: bold;">class</span> Task
        <span style="color: #008000;">&#123;</span>
            <span style="color: #008000;">&#91;</span>Required<span style="color: #008000;">&#93;</span>
            <span style="color: #008000;">&#91;</span>Display<span style="color: #008000;">&#40;</span>Name <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Project&quot;</span>, Order <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
            <span style="color: #0600FF; font-weight: bold;">public</span> Project Project <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
            <span style="color: #008000;">&#91;</span>Required<span style="color: #008000;">&#93;</span>
            <span style="color: #008000;">&#91;</span>Display<span style="color: #008000;">&#40;</span>Name <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Employee Code&quot;</span>, Order <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span>, GroupName <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Employee&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
            <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> EmployeeCode <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
            <span style="color: #008000;">&#91;</span>Required<span style="color: #008000;">&#93;</span>
            <span style="color: #008000;">&#91;</span>Display<span style="color: #008000;">&#40;</span>Name <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Track Date&quot;</span>, Order <span style="color: #008000;">=</span> <span style="color: #FF0000;">2</span>, GroupName <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Date &amp; Time&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
            <span style="color: #0600FF; font-weight: bold;">public</span> DateTime TrackDate <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
            <span style="color: #008000;">&#91;</span>Required<span style="color: #008000;">&#93;</span>
            <span style="color: #008000;">&#91;</span>Display<span style="color: #008000;">&#40;</span>Name <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Hours&quot;</span>, Order <span style="color: #008000;">=</span> <span style="color: #FF0000;">3</span>, GroupName <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Date &amp; Time&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
            <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">decimal</span> TaskHours <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
            <span style="color: #008000;">&#91;</span>Required<span style="color: #008000;">&#93;</span>
            <span style="color: #008000;">&#91;</span>Display<span style="color: #008000;">&#40;</span>Name <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Task Descriprion&quot;</span>, Order <span style="color: #008000;">=</span> <span style="color: #FF0000;">4</span>, GroupName <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Information&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
            <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> TaskDescriprion <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The modified task screen would look like</p>
<div id="attachment_886" class="wp-caption alignnone" style="width: 580px"><a href="http://www.nareshjois.com/wp-content/uploads/2011/06/ddstep5.png" rel="lightbox[878]"><img src="http://www.nareshjois.com/wp-content/uploads/2011/06/ddstep5.png" alt="" title="Task Entry Screen Modified" width="570" height="541" class="size-full wp-image-886" /></a><p class="wp-caption-text">Task Entry Screen Modified</p></div>
<p>And this is just the beginning we can do a lot more stuff as all the controls can be customized,  I will post about grouping and also customizing display and edit controls in a subsequent blog.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nareshjois.com/2011/06/asp-net-dyanamic-data-a-leap-for-rad/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mix and Mash Asp.net MVC with Webforms</title>
		<link>http://www.nareshjois.com/2010/10/mix-asp-net-mvc-with-webforms/</link>
		<comments>http://www.nareshjois.com/2010/10/mix-asp-net-mvc-with-webforms/#comments</comments>
		<pubDate>Fri, 29 Oct 2010 07:48:26 +0000</pubDate>
		<dc:creator>naresh</dc:creator>
				<category><![CDATA[.net Programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[ASP.net]]></category>
		<category><![CDATA[Asp.net MVC]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[Webforms]]></category>

		<guid isPermaLink="false">http://www.nareshjois.com/?p=819</guid>
		<description><![CDATA[A lot of people including me wanted to to get on the bandwagon of ASP.net MVC, but the main problem is we do not always get to start new projects and there is always a timeline hanging. I will not talk about the benefits of MVC Pattern as most of us already aware of it, [...]]]></description>
			<content:encoded><![CDATA[<p>A lot of people including me wanted to to get on the bandwagon of ASP.net MVC, but the main problem is we do not always get to start new projects and there is always a timeline hanging. I will not talk about the benefits of MVC Pattern as most of us already aware of it, But for an ASP.net Developer where everything is tied up in Webforms this may seem difficult, but once you start taking advantages of this pattern, the usefulness becomes obvious, me having worked on Rails and PHP frameworks such as CodeIgnitor and CakePHP, It was never a question of If, but when, and the best way to begin would be use your existing skills with webforms and start bringin in MVC Pattern, and before you know it you can be completely taking the benefits out of both worlds.</p>
<h2>Webforms with MVC</h2>
<p>If you are starting with a new project its really simple, start of with a new MVC Project in Visual Studio Add a New Webform run it and thats it no configuration required, the reason is that because MVC Framework is built on top of same ASP.net platform, it runs and the reason why the routing doesn&#8217;t try and hijack this request is because of the way it works, if the physical file of the request exists the request is always made to the aspx file, for better performance and save disk query on each .aspx request you can also add the following line in your global.asax</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:consolas, monaco;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> RegisterRoutes<span style="color: #008000;">&#40;</span>RouteCollection routes<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
&nbsp;
            routes<span style="color: #008000;">.</span><span style="color: #0000FF;">IgnoreRoute</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;{resource}.axd/{*pathInfo}&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            routes<span style="color: #008000;">.</span><span style="color: #0000FF;">IgnoreRoute</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;{resource}.aspx/{*pathInfo}&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            routes<span style="color: #008000;">.</span><span style="color: #0000FF;">MapRoute</span><span style="color: #008000;">&#40;</span>
                <span style="color: #666666;">&quot;Default&quot;</span>, <span style="color: #008080; font-style: italic;">// Route name</span>
                <span style="color: #666666;">&quot;{controller}/{action}/{id}&quot;</span>, <span style="color: #008080; font-style: italic;">// URL with parameters</span>
                <span style="color: #008000;">new</span> <span style="color: #008000;">&#123;</span> controller <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Home&quot;</span>, action <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Index&quot;</span>, id <span style="color: #008000;">=</span> UrlParameter<span style="color: #008000;">.</span><span style="color: #0000FF;">Optional</span> <span style="color: #008000;">&#125;</span> <span style="color: #008080; font-style: italic;">// Parameter defaults</span>
            <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008000;">&#125;</span></pre></div></div>

<h2>MVC with Webforms</h2>
<p>But most of us never start with new projects right ?, so you have to do a bit more work, Add the following as Reference to your Web Application<br />
<a href="http://www.nareshjois.com/wp-content/uploads/2010/10/references.png" rel="lightbox[819]"><img src="http://www.nareshjois.com/wp-content/uploads/2010/10/references.png" alt="" title="references" width="500" height="401" class="alignnone size-full wp-image-822" /></a></p>
<p>Now Update your <b>web.config </b> to reflect</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:consolas, monaco;"> <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;system</span> .web<span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;compilation</span> <span style="color: #000066;">debug</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000066;">targetFramework</span>=<span style="color: #ff0000;">&quot;4.0&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;assemblies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;add</span> <span style="color: #000066;">assembly</span>=<span style="color: #ff0000;">&quot;System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;add</span> <span style="color: #000066;">assembly</span>=<span style="color: #ff0000;">&quot;System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;add</span> <span style="color: #000066;">assembly</span>=<span style="color: #ff0000;">&quot;System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/assemblies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/compilation<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;pages<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;namespaces<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;add</span> <span style="color: #000066;">namespace</span>=<span style="color: #ff0000;">&quot;System.Web.Mvc&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;add</span> <span style="color: #000066;">namespace</span>=<span style="color: #ff0000;">&quot;System.Web.Mvc.Ajax&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;add</span> <span style="color: #000066;">namespace</span>=<span style="color: #ff0000;">&quot;System.Web.Mvc.Html&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;add</span> <span style="color: #000066;">namespace</span>=<span style="color: #ff0000;">&quot;System.Web.Routing&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/namespaces<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/pages<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
......
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/system<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Add the following code to <b>global.asax</b></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:consolas, monaco;"> <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> RegisterRoutes<span style="color: #008000;">&#40;</span>RouteCollection routes<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            routes<span style="color: #008000;">.</span><span style="color: #0000FF;">IgnoreRoute</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;{resource}.axd/{*pathInfo}&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            routes<span style="color: #008000;">.</span><span style="color: #0000FF;">IgnoreRoute</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;{resource}.aspx/{*pathInfo}&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            routes<span style="color: #008000;">.</span><span style="color: #0000FF;">MapRoute</span><span style="color: #008000;">&#40;</span>
                <span style="color: #666666;">&quot;Default&quot;</span>, <span style="color: #008080; font-style: italic;">// Route name</span>
                <span style="color: #666666;">&quot;{controller}/{action}/{id}&quot;</span>, <span style="color: #008080; font-style: italic;">// URL with parameters</span>
                <span style="color: #008000;">new</span> <span style="color: #008000;">&#123;</span> controller <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Home&quot;</span>, action <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Index&quot;</span>, id <span style="color: #008000;">=</span> UrlParameter<span style="color: #008000;">.</span><span style="color: #0000FF;">Optional</span> <span style="color: #008000;">&#125;</span> <span style="color: #008080; font-style: italic;">// Parameter defaults</span>
            <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008000;">&#125;</span>
&nbsp;
&nbsp;
&nbsp;
        <span style="color: #6666cc; font-weight: bold;">void</span> Application_Start<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, EventArgs e<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            AreaRegistration<span style="color: #008000;">.</span><span style="color: #0000FF;">RegisterAllAreas</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            RegisterRoutes<span style="color: #008000;">&#40;</span>RouteTable<span style="color: #008000;">.</span><span style="color: #0000FF;">Routes</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008000;">&#125;</span></pre></div></div>

<p>Now Add the Controllers, Views, Folders to the root of application and you are good to go:<br />
<br />
<b>Home Controller &#8211; /Controllers/HomeControllers.cs</b></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:consolas, monaco;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Collections.Generic</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Linq</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Web</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Web.Mvc</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">namespace</span> WebApp<span style="color: #008000;">.</span><span style="color: #0000FF;">Test</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Controllers</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #008000;">&#91;</span>HandleError<span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> HomeController <span style="color: #008000;">:</span> Controller
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> ActionResult Index<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            ViewData<span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;Message&quot;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Welcome to ASP.NET MVC!&quot;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF; font-weight: bold;">return</span> View<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>View &#8211; /Views/Home/Index.aspx</p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:consolas, monaco;"><span style="color: #006600; font-weight: bold;">&lt;</span> <span style="color: #006600; font-weight: bold;">%@</span> Page Language<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;C#&quot;</span> MasterPageFile<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;~/Site.Master&quot;</span> Inherits<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;System.Web.Mvc.ViewPage&quot;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span>
&nbsp;
<span style="color: #006600; font-weight: bold;">&lt;</span>asp<span style="color: #006600; font-weight: bold;">:</span>content ID<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;Content1&quot;</span> ContentPlaceHolderID<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;HeadContent&quot;</span> runat<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;server&quot;</span><span style="color: #006600; font-weight: bold;">&gt;</span>
<span style="color: #006600; font-weight: bold;">&lt;/</span>asp<span style="color: #006600; font-weight: bold;">:</span>content<span style="color: #006600; font-weight: bold;">&gt;</span>
<span style="color: #006600; font-weight: bold;">&lt;</span>asp<span style="color: #006600; font-weight: bold;">:</span>content ID<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;Content2&quot;</span> ContentPlaceHolderID<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;MainContent&quot;</span> runat<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;server&quot;</span><span style="color: #006600; font-weight: bold;">&gt;</span>
  <span style="color: #006600; font-weight: bold;">&lt;</span>h2<span style="color: #006600; font-weight: bold;">&gt;&lt;</span> <span style="color: #006600; font-weight: bold;">%:</span> ViewData<span style="color: #006600; font-weight:bold;">&#91;</span><span style="color: #cc0000;">&quot;Message&quot;</span><span style="color: #006600; font-weight:bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span><span style="color: #006600; font-weight: bold;">&lt;/</span>h2<span style="color: #006600; font-weight: bold;">&gt;</span>
    <span style="color: #006600; font-weight: bold;">&lt;</span>p<span style="color: #006600; font-weight: bold;">&gt;</span>
        <span style="color: #990099; font-weight: bold;">To</span> learn more about ASP.<span style="color: #9900cc;">NET</span> MVC visit <span style="color: #006600; font-weight: bold;">&lt;</span>a href<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;http://asp.net/mvc&quot;</span> title<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;ASP.NET MVC Website&quot;</span><span style="color: #006600; font-weight: bold;">&gt;</span>http<span style="color: #006600; font-weight: bold;">:</span><span style="color: #ff6600;">//asp.net/mvc&lt;/a&gt;.</span>
    <span style="color: #006600; font-weight: bold;">&lt;/</span>p<span style="color: #006600; font-weight: bold;">&gt;</span>
<span style="color: #006600; font-weight: bold;">&lt;/</span>asp<span style="color: #006600; font-weight: bold;">:</span>content<span style="color: #006600; font-weight: bold;">&gt;</span></pre></div></div>

<p>You can even use the same Master Page<br />
<a href="http://www.nareshjois.com/wp-content/uploads/2010/10/screen1.png" rel="lightbox[819]"><img src="http://www.nareshjois.com/wp-content/uploads/2010/10/screen1-300x227.png" alt="" title="screen1" width="300" height="227" class="alignnone size-medium wp-image-823" /></a><br />
<a href="http://www.nareshjois.com/wp-content/uploads/2010/10/screen2.png" rel="lightbox[819]"><img src="http://www.nareshjois.com/wp-content/uploads/2010/10/screen2-300x227.png" alt="" title="screen2" width="300" height="227" class="alignnone size-medium wp-image-824" /></a></p>
<p>P.S: The VS Project doesn&#8217;t support integrated way of creating Views and Controllers in this method</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nareshjois.com/2010/10/mix-asp-net-mvc-with-webforms/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using a ASP.net Grid view using code</title>
		<link>http://www.nareshjois.com/2007/11/using-a-aspnet-grid-view-using-code/</link>
		<comments>http://www.nareshjois.com/2007/11/using-a-aspnet-grid-view-using-code/#comments</comments>
		<pubDate>Sun, 18 Nov 2007 04:28:41 +0000</pubDate>
		<dc:creator>naresh</dc:creator>
				<category><![CDATA[.net Programming]]></category>
		<category><![CDATA[ASP.net]]></category>
		<category><![CDATA[GridView]]></category>
		<category><![CDATA[VB.net]]></category>

		<guid isPermaLink="false">http://nareshjois.wordpress.com/2007/11/18/using-a-aspnet-grid-view-using-code/</guid>
		<description><![CDATA[We have all used grid view along with data sources but some time you need to to that from code, the problem is most of us do not know how to use this from code, but Its not that difficult to that. Here&#8217;s a simple way to do that. A Simple grid  First column is [...]]]></description>
			<content:encoded><![CDATA[<p>We have all used grid view along with data sources but some time you need to to that from code, the problem is most of us do not know how to use this from code, but Its not that difficult to that. Here&#8217;s a simple way to do that.</p>
<p>A Simple grid  First column is a readonly templated field, second one a readonly bound field and third a templated entry field. At the Edit the edit command Field</p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:consolas, monaco;">&lt;asp:GridView ID=&quot;GridView1&quot; runat=&quot;server&quot;  CssSelectorClass=&quot;PrettyGridView&quot; PageSize=&quot;50&quot; AllowSorting=&quot;True&quot;   AutoGenerateColumns=&quot;False&quot; EnableSortingAndPagingCallbacks=&quot;True&quot;&gt;
   &lt;columns&gt;
   &lt;asp:templatefield HeaderText=&quot;Center Id&quot; SortExpression=&quot;center_Id&quot;&gt;
   &lt;edititemtemplate&gt;
   &lt;asp:Label ID=&quot;Label10&quot; runat=&quot;server&quot; Text='<span style="color: #000000; font-weight: bold;">&lt;%</span># Eval<span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;bind1&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span>'/&gt;    &lt;/edititemtemplate&gt;
   &lt;itemtemplate&gt;
   &lt;asp:Label ID=&quot;Label10&quot; runat=&quot;server&quot; Text='<span style="color: #000000; font-weight: bold;">&lt;%</span># Bind<span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;bind1&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span>'/&gt;    &lt;/itemtemplate&gt;
   &lt;/asp:templatefield&gt;
   &lt;asp:boundfield DataField=&quot;City_Name&quot; HeaderText=&quot;City Name&quot;            ReadOnly=&quot;True&quot; SortExpression=&quot;City_Name&quot;&gt;&lt;/asp:boundfield&gt;
   &lt;asp:templatefield HeaderText=&quot;Rate&quot;&gt;
   &lt;edititemtemplate&gt;
   &lt;asp:TextBox ID=&quot;TextBox1&quot; runat=&quot;server&quot; Text='<span style="color: #000000; font-weight: bold;">&lt;%</span># Bind<span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;bind2&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span>'/&gt;    &lt;/edititemtemplate&gt;
   &lt;itemtemplate&gt;
   &lt;asp:Label ID=&quot;Label1&quot; runat=&quot;server&quot; Text='<span style="color: #000000; font-weight: bold;">&lt;%</span># Bind<span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;bind2&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span>'/&gt;    &lt;/itemtemplate&gt;
   &lt;/asp:templatefield&gt;
   &lt;asp:commandfield ShowEditButton=&quot;True&quot;&gt;&lt;/asp:commandfield&gt;
   &lt;/columns&gt;
&lt;/asp:GridView&gt;</pre></div></div>

<p>First Write a Function for loading data which will be reused on every action. I call it BindData()</p>

<div class="wp_syntax"><div class="code"><pre class="vbnet" style="font-family:consolas, monaco;"><span style="color: #FF8000;">Private</span> <span style="color: #0600FF;">Sub</span> BindData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>        conn <span style="color: #008000;">=</span> <span style="color: #FF8000;">New</span> OleDbConnection<span style="color: #000000;">&#40;</span>GetJetConnectionString<span style="color: #000000;">&#41;</span>
       adap <span style="color: #008000;">=</span> <span style="color: #FF8000;">New</span> OleDbDataAdapter<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Custom SQL Query&quot;</span>, conn<span style="color: #000000;">&#41;</span>
       <span style="color: #0600FF;">Dim</span> datatable1 <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">New</span> DataTable
       adap.<span style="color: #0000FF;">Fill</span><span style="color: #000000;">&#40;</span>datatable1<span style="color: #000000;">&#41;</span>
       dataset1 <span style="color: #008000;">=</span> <span style="color: #FF8000;">New</span> DataSet
       dataset1.<span style="color: #0000FF;">Tables</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>datatable1<span style="color: #000000;">&#41;</span>
       <span style="color: #FF8000;">Me</span>.<span style="color: #0000FF;">GridView1</span>.<span style="color: #0000FF;">DataSource</span> <span style="color: #008000;">=</span> dataset1
       <span style="color: #FF8000;">Me</span>.<span style="color: #0000FF;">GridView1</span>.<span style="color: #0000FF;">DataBind</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> 
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span></pre></div></div>

<p>Now since the gridview is binded from code we have to handle all its events. So When the user clicks on edit. Here I have done two things  one is enable editing mode, then I store the data of textbox in a viewstate, so that it can be accessed later when updating, please remember that the regular method of e.OldValues and e.NewValues Will not work because we have to do that manually, (which can be done, but for our purposes we will use viewstate)</p>

<div class="wp_syntax"><div class="code"><pre class="vbnet" style="font-family:consolas, monaco;">Protected <span style="color: #0600FF;">Sub</span> GridView1_RowEditing<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> sender <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Object</span>,          <span style="color: #FF8000;">ByVal</span> e <span style="color: #FF8000;">As</span> System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">WebControls</span>.<span style="color: #0000FF;">GridViewEditEventArgs</span><span style="color: #000000;">&#41;</span>          <span style="color: #FF8000;">Handles</span> GridView1.<span style="color: #0000FF;">RowEditing</span>
    GridView1.<span style="color: #0000FF;">EditIndex</span> <span style="color: #008000;">=</span> e.<span style="color: #0000FF;">NewEditIndex</span>
    BindData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #0600FF;">Dim</span> row <span style="color: #FF8000;">As</span> GridViewRow <span style="color: #008000;">=</span> GridView1.<span style="color: #0000FF;">Rows</span><span style="color: #000000;">&#40;</span>GridView1.<span style="color: #0000FF;">EditIndex</span><span style="color: #000000;">&#41;</span>
    <span style="color: #0600FF;">If</span> row <span style="color: #FF8000;">IsNot</span> <span style="color: #FF8000;">Nothing</span> <span style="color: #FF8000;">Then</span>
        <span style="color: #0600FF;">Dim</span> t <span style="color: #FF8000;">As</span> TextBox <span style="color: #008000;">=</span> TryCast<span style="color: #000000;">&#40;</span>row.<span style="color: #0000FF;">FindControl</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;TextBox1&quot;</span><span style="color: #000000;">&#41;</span>, TextBox<span style="color: #000000;">&#41;</span>
        <span style="color: #0600FF;">If</span> t <span style="color: #FF8000;">IsNot</span> <span style="color: #FF8000;">Nothing</span> <span style="color: #FF8000;">Then</span>
            ViewState<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;OldRate&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">=</span> t.<span style="color: #0000FF;">Text</span>
        <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span>
    <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span></pre></div></div>

<p>Now for the cancel part</p>

<div class="wp_syntax"><div class="code"><pre class="vbnet" style="font-family:consolas, monaco;">Protected <span style="color: #0600FF;">Sub</span> GridView1_RowCancelingEdit<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> sender <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Object</span>,          <span style="color: #FF8000;">ByVal</span> e <span style="color: #FF8000;">As</span> System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">WebControls</span>.<span style="color: #0000FF;">GridViewCancelEditEventArgs</span><span style="color: #000000;">&#41;</span>         <span style="color: #FF8000;">Handles</span> GridView1.<span style="color: #0000FF;">RowCancelingEdit</span>
   GridView1.<span style="color: #0000FF;">EditIndex</span> <span style="color: #008000;">=</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span>
   BindData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
 <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span></pre></div></div>

<p>Now for the update part. First new value is found out, if it is blank nothing is done, if old value does not Exist INSERT command is used if old value does exist UPDATE command is used. Here I did a Simple SQL execution but I think you will figure out a better way</p>

<div class="wp_syntax"><div class="code"><pre class="vbnet" style="font-family:consolas, monaco;">Protected <span style="color: #0600FF;">Sub</span> GridView1_RowUpdating<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> sender <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Object</span>,          <span style="color: #FF8000;">ByVal</span> e <span style="color: #FF8000;">As</span> System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">WebControls</span>.<span style="color: #0000FF;">GridViewUpdateEventArgs</span><span style="color: #000000;">&#41;</span>          <span style="color: #FF8000;">Handles</span> GridView1.<span style="color: #0000FF;">RowUpdating</span>
    <span style="color: #0600FF;">Dim</span> new_rate, old_rate <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span>
    old_rate <span style="color: #008000;">=</span> ViewState<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;OldRate&quot;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #0600FF;">Dim</span> row <span style="color: #FF8000;">As</span> GridViewRow <span style="color: #008000;">=</span> GridView1.<span style="color: #0000FF;">Rows</span><span style="color: #000000;">&#40;</span>e.<span style="color: #0000FF;">RowIndex</span><span style="color: #000000;">&#41;</span>
    <span style="color: #0600FF;">If</span> row <span style="color: #FF8000;">IsNot</span> <span style="color: #FF8000;">Nothing</span> <span style="color: #FF8000;">Then</span>
        <span style="color: #0600FF;">Dim</span> t <span style="color: #FF8000;">As</span> TextBox <span style="color: #008000;">=</span> TryCast<span style="color: #000000;">&#40;</span>row.<span style="color: #0000FF;">FindControl</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;TextBox1&quot;</span><span style="color: #000000;">&#41;</span>, TextBox<span style="color: #000000;">&#41;</span>
        <span style="color: #0600FF;">If</span> t <span style="color: #FF8000;">IsNot</span> <span style="color: #FF8000;">Nothing</span> <span style="color: #FF8000;">Then</span>
            new_rate <span style="color: #008000;">=</span> t.<span style="color: #0000FF;">Text</span>
            <span style="color: #0600FF;">If</span> new_rate <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;&quot;</span> <span style="color: #FF8000;">Then</span>
                GridView1.<span style="color: #0000FF;">EditIndex</span> <span style="color: #008000;">=</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span>
                BindData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
                <span style="color: #0600FF;">Exit</span> <span style="color: #0600FF;">Sub</span>
            <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span>
            <span style="color: #0600FF;">If</span> new_rate <span style="color: #008000;">=</span> old_rate <span style="color: #FF8000;">Then</span>
                GridView1.<span style="color: #0000FF;">EditIndex</span> <span style="color: #008000;">=</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span>
                BindData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
                <span style="color: #0600FF;">Exit</span> <span style="color: #0600FF;">Sub</span>
            <span style="color: #FF8000;">Else</span>
                <span style="color: #0600FF;">Dim</span> label1 <span style="color: #FF8000;">As</span> Label <span style="color: #008000;">=</span> TryCast<span style="color: #000000;">&#40;</span>row.<span style="color: #0000FF;">FindControl</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;label10&quot;</span><span style="color: #000000;">&#41;</span>, Label<span style="color: #000000;">&#41;</span>
                <span style="color: #0600FF;">Dim</span> center_id <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> <span style="color: #008000;">=</span> label1.<span style="color: #0000FF;">Text</span>
                <span style="color: #0600FF;">Dim</span> conn <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">New</span> OleDbConnection<span style="color: #000000;">&#40;</span>ConnectionString<span style="color: #000000;">&#41;</span>
                <span style="color: #0600FF;">Dim</span> cmd <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">New</span> OleDbCommand
                cmd.<span style="color: #0000FF;">Connection</span> <span style="color: #008000;">=</span> conn
                cmd.<span style="color: #0000FF;">CommandType</span> <span style="color: #008000;">=</span> CommandType.<span style="color: #0000FF;">Text</span>
                <span style="color: #0600FF;">Dim</span> SQL <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span>
                <span style="color: #0600FF;">If</span> old_rate <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;&quot;</span> <span style="color: #FF8000;">Then</span>
                    SQL <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;INSERT INTO QUERY&quot;</span>
                <span style="color: #FF8000;">Else</span>
                    SQL <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;UPDATE QUERY&quot;</span>
                <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span>
                cmd.<span style="color: #0000FF;">CommandText</span> <span style="color: #008000;">=</span> SQL
                conn.<span style="color: #0600FF;">Open</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
                cmd.<span style="color: #0000FF;">ExecuteNonQuery</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
                conn.<span style="color: #0600FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
                GridView1.<span style="color: #0000FF;">EditIndex</span> <span style="color: #008000;">=</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span>
                BindData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span>
        <span style="color: #FF8000;">Else</span>
            GridView1.<span style="color: #0000FF;">EditIndex</span> <span style="color: #008000;">=</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span>
            BindData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span>
    <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.nareshjois.com/2007/11/using-a-aspnet-grid-view-using-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

