<?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>SolidPitch &#187; SQL</title>
	<atom:link href="http://www.solidpitch.com/category/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.solidpitch.com</link>
	<description>Video and Web Development</description>
	<lastBuildDate>Thu, 29 Jul 2010 18:18:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Getting the ID for the last inserted item with ASP.NET and VB</title>
		<link>http://www.solidpitch.com/2009/11/18/getting-the-id-for-the-last-inserted-item-with-aspnet-and-vb/</link>
		<comments>http://www.solidpitch.com/2009/11/18/getting-the-id-for-the-last-inserted-item-with-aspnet-and-vb/#comments</comments>
		<pubDate>Wed, 18 Nov 2009 19:16:50 +0000</pubDate>
		<dc:creator>Dragos</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.webfixtech.com/blog/?p=134</guid>
		<description><![CDATA[I know it's recommended to use stored procedures but sometimes you need a short one for domestic use, an one-liner]]></description>
			<content:encoded><![CDATA[<p>OK, I know it&#8217;s recommended to use stored procedures but sometimes you need a short one for domestic use.<br />
So, how do we do it, without setting two  SqlCommand objects?</p>
<p>1. We define a connection<br />
Dim conSQLDBConn As New SqlConnection([Connection String])</p>
<p>2.  Create a command and set the params.<br />
Dim cmdSQLCommand As New SqlCommand</p>
<p>cmdSQLCommand.CommandType = Data.CommandType.Text</p>
<p>3. Assign the connection<br />
cmdSQLCommand.Connection = conSQLDBConn</p>
<p>4. Will define and configure a parameter that will return the value we need: the last inserted item ID<br />
Dim prmInsert As New SqlParameter<br />
prmInsert.Direction = ParameterDirection.Output<br />
prmInsert.ParameterName = &#8220;ProductID&#8221;   &#8216; that would be your ID, in my case I was playing with products<br />
prmInsert.Size = 10 &#8216;this should match what you defined in your table<br />
cmdSQLCommand.Parameters.Add(prmInsert)</p>
<p>5. Now we create the insert statement<br />
cmdSQLCommand.CommandText = &#8220;INSERT INTO Products (ProductName,ProductDescription)  VALUES ([ProductName],[ ProductDescription]); SELECT SCOPE_IDENTITY() AS ProductID&#8221;</p>
<p>SELECT SCOPE_IDENTITY() is the magic word, that will return our ID.</p>
<p>6.  Now we make sure we get that back from the SQL statement by using &#8220;ExecuteScalar&#8221;<br />
Dim strProductID as String = cmdSQLCommand.ExecuteScalar()</p>
<p>Note that the values in square brackets would be your constants, whatever.</p>
<p>That&#8217;s it. Easy peasy, lemon squeezy <img src='http://www.solidpitch.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-knowledge">
<ul class="socials">
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://www.solidpitch.com/2009/11/18/getting-the-id-for-the-last-inserted-item-with-aspnet-and-vb/&amp;t=Getting+the+ID+for+the+last+inserted+item+with+ASP.NET+and+VB" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Getting+the+ID+for+the+last+inserted+item+with+ASP.NET+and+VB+-+http://tinyurl.com/2e9c3jn&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://www.solidpitch.com/2009/11/18/getting-the-id-for-the-last-inserted-item-with-aspnet-and-vb/&amp;title=Getting+the+ID+for+the+last+inserted+item+with+ASP.NET+and+VB" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://www.solidpitch.com/2009/11/18/getting-the-id-for-the-last-inserted-item-with-aspnet-and-vb/&amp;title=Getting+the+ID+for+the+last+inserted+item+with+ASP.NET+and+VB" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://www.solidpitch.com/2009/11/18/getting-the-id-for-the-last-inserted-item-with-aspnet-and-vb/&amp;title=Getting+the+ID+for+the+last+inserted+item+with+ASP.NET+and+VB" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-gmail">
			<a href="https://mail.google.com/mail/?ui=2&amp;view=cm&amp;fs=1&amp;tf=1&amp;su=Getting+the+ID+for+the+last+inserted+item+with+ASP.NET+and+VB&amp;body=Link: http://www.solidpitch.com/2009/11/18/getting-the-id-for-the-last-inserted-item-with-aspnet-and-vb/ (sent via shareaholic)%0D%0A%0D%0A----%0D%0A I%20know%20it%27s%20recommended%20to%20use%20stored%20procedures%20but%20sometimes%20you%20need%20a%20short%20one%20for%20domestic%20use%2C%20an%20one-liner" rel="nofollow" class="external" title="Email this via Gmail">Email this via Gmail</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://www.solidpitch.com/2009/11/18/getting-the-id-for-the-last-inserted-item-with-aspnet-and-vb/&amp;title=Getting+the+ID+for+the+last+inserted+item+with+ASP.NET+and+VB&amp;summary=I%20know%20it%27s%20recommended%20to%20use%20stored%20procedures%20but%20sometimes%20you%20need%20a%20short%20one%20for%20domestic%20use%2C%20an%20one-liner&amp;source=SolidPitch" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://www.solidpitch.com/2009/11/18/getting-the-id-for-the-last-inserted-item-with-aspnet-and-vb/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-googlebookmarks">
			<a href="http://www.google.com/bookmarks/mark?op=add&amp;bkmk=http://www.solidpitch.com/2009/11/18/getting-the-id-for-the-last-inserted-item-with-aspnet-and-vb/&amp;title=Getting+the+ID+for+the+last+inserted+item+with+ASP.NET+and+VB" rel="nofollow" class="external" title="Add this to Google Bookmarks">Add this to Google Bookmarks</a>
		</li>
		<li class="shr-hotmail">
			<a href="http://mail.live.com/?rru=compose?subject=Getting+the+ID+for+the+last+inserted+item+with+ASP.NET+and+VB&amp;body=Link: http://www.solidpitch.com/2009/11/18/getting-the-id-for-the-last-inserted-item-with-aspnet-and-vb/ (sent via shareaholic)%0D%0A%0D%0A----%0D%0A I%20know%20it%27s%20recommended%20to%20use%20stored%20procedures%20but%20sometimes%20you%20need%20a%20short%20one%20for%20domestic%20use%2C%20an%20one-liner" rel="nofollow" class="external" title="Email this via Hotmail">Email this via Hotmail</a>
		</li>
		<li class="shr-technorati">
			<a href="http://technorati.com/faves?add=http://www.solidpitch.com/2009/11/18/getting-the-id-for-the-last-inserted-item-with-aspnet-and-vb/" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://www.solidpitch.com/2009/11/18/getting-the-id-for-the-last-inserted-item-with-aspnet-and-vb/&amp;title=Getting+the+ID+for+the+last+inserted+item+with+ASP.NET+and+VB" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.diigo.com/post?url=http://www.solidpitch.com/2009/11/18/getting-the-id-for-the-last-inserted-item-with-aspnet-and-vb/&amp;title=Getting+the+ID+for+the+last+inserted+item+with+ASP.NET+and+VB&amp;desc=I%20know%20it%27s%20recommended%20to%20use%20stored%20procedures%20but%20sometimes%20you%20need%20a%20short%20one%20for%20domestic%20use%2C%20an%20one-liner" rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-comfeed">
			<a href="http://www.solidpitch.com/2009/11/18/getting-the-id-for-the-last-inserted-item-with-aspnet-and-vb/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://www.solidpitch.com/2009/11/18/getting-the-id-for-the-last-inserted-item-with-aspnet-and-vb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
