<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://forums.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>SQL Server, SQL Server Express, and SqlDataSource Control</title><link>http://forums.asp.net/54.aspx</link><description>All about SQL Server, SQL Server Express, MSDE, and the SqlDataSource control.</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>Re: Check to see if record already exists</title><link>http://forums.asp.net/thread/3275134.aspx</link><pubDate>Sun, 05 Jul 2009 06:00:52 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:3275134</guid><dc:creator>ramireddyindia</dc:creator><author>ramireddyindia</author><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/3275134.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=54&amp;PostID=3275134</wfw:commentRss><description>Thanks Naom, I forgot this point. :)</description></item><item><title>Re: Check to see if record already exists</title><link>http://forums.asp.net/thread/3275060.aspx</link><pubDate>Sun, 05 Jul 2009 02:37:31 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:3275060</guid><dc:creator>Naom</dc:creator><author>Naom</author><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/3275060.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=54&amp;PostID=3275060</wfw:commentRss><description>&lt;p&gt;&amp;nbsp;Rami,&lt;/p&gt;
&lt;p&gt;Think a bit. You made a check and found that no record exists. Another user made the same check at the same time and inserted the record, now since your check also just returned 0, you also inserted the record and ended up with duplicates. Even if you put the whole logic in one SP, it is not 100% bullet-proof solution in a very busy website. Only using UNIQUE index in addition with the stored procedure will guarantee uniqueness. Therefore, any extra checks like you suggest, become useless.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Re: Check to see if record already exists</title><link>http://forums.asp.net/thread/3274321.aspx</link><pubDate>Sat, 04 Jul 2009 06:20:02 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:3274321</guid><dc:creator>ramireddyindia</dc:creator><author>ramireddyindia</author><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/3274321.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=54&amp;PostID=3274321</wfw:commentRss><description>&lt;p&gt;&lt;BLOCKQUOTE&gt;&lt;div&gt;&lt;img src="/Themes/fan/images/icon-quote.gif"&gt; &lt;strong&gt;Naom:&lt;/strong&gt;&lt;/div&gt;&lt;div&gt;Using Rami&amp;#39;s approach would not guarantee uniqueness &lt;/div&gt;&lt;/BLOCKQUOTE&gt;&lt;/p&gt;&lt;p&gt;why it won&amp;#39;t work? when u call that stored proc. by&amp;nbsp; passing the input the user entered, it will returns how many times its in table if its greater than 0, we will give a message that already exists,otherwise, we will insert the record? y this won&amp;#39;t work?&lt;br /&gt;&lt;/p&gt;</description></item><item><title>Re: Check to see if record already exists</title><link>http://forums.asp.net/thread/3274114.aspx</link><pubDate>Sat, 04 Jul 2009 00:28:51 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:3274114</guid><dc:creator>malcolms</dc:creator><author>malcolms</author><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/3274114.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=54&amp;PostID=3274114</wfw:commentRss><description>&lt;p&gt;A good option is to insert the record, and if it fails, catch that in your C#/VB.NET code.&amp;nbsp; You can put a try/catch/finally block around your code, or set the customErrors attribute in your web.config to catch the error and fail gracefully.&lt;br /&gt;&lt;/p&gt;</description></item><item><title>Re: Check to see if record already exists</title><link>http://forums.asp.net/thread/3274099.aspx</link><pubDate>Sat, 04 Jul 2009 00:06:37 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:3274099</guid><dc:creator>RickNZ</dc:creator><author>RickNZ</author><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/3274099.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=54&amp;PostID=3274099</wfw:commentRss><description>&lt;p&gt;I agree with Naom, and would add that with an appropriate unique key in place, you should just let the insert fail if there&amp;#39;s a duplicate, rather than first checking for one.&amp;nbsp; That saves a round-trip to the DB.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;</description></item><item><title>Re: Check to see if record already exists</title><link>http://forums.asp.net/thread/3274002.aspx</link><pubDate>Fri, 03 Jul 2009 20:54:24 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:3274002</guid><dc:creator>Naom</dc:creator><author>Naom</author><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/3274002.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=54&amp;PostID=3274002</wfw:commentRss><description>&lt;p&gt;Using Rami&amp;#39;s approach would not guarantee uniqueness and therefore there is no need to use such SPs at all. If you want to check for uniqueness, you need to do this in SP with &lt;/p&gt;&lt;p&gt;IF NOT EXISTS(select ...)&lt;/p&gt;&lt;p&gt;&amp;nbsp; insert ...&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;However, IMHO, even this would not guarantee 100% uniqueness, so the best way would be to add a unique index on the table as well.&lt;br /&gt;&lt;/p&gt;&lt;p&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;</description></item><item><title>Re: Check to see if record already exists</title><link>http://forums.asp.net/thread/3273999.aspx</link><pubDate>Fri, 03 Jul 2009 20:47:31 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:3273999</guid><dc:creator>PeteNet</dc:creator><author>PeteNet</author><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/3273999.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=54&amp;PostID=3273999</wfw:commentRss><description>&lt;p&gt;It would be efficient to address this into the stored procedure &lt;b&gt;&lt;i&gt;IF&lt;/i&gt;&lt;/b&gt; you already have one in place; you then can accommodate the stored procedure to return a particular status and status message (as output parameters) which basically would say something to the effect that the record already exists - the advantage would be that if the record does not exist the code, in the procedure, would go ahead and insert like normal and you would have handled both situations nicely (and additional code in the page)&lt;br /&gt;&lt;br /&gt;But, if you don&amp;#39;t have a stored procedure in place then you could always look for a record (based on whatever are the fields that make it unique) using a simple ExecuteScalar statement with a SqlCommand and only if it does not then go ahead and insert the new record etc&lt;br /&gt;Look at this example here for a simple ExecuteScalar: &lt;a href="http://www.java2s.com/Tutorial/CSharp/0560__ADO.Net/ExecuteScalarExample.htm"&gt;http://www.java2s.com/Tutorial/CSharp/0560__ADO.Net/ExecuteScalarExample.htm&lt;/a&gt; , another within an insert function: &lt;a href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar.aspx"&gt;http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar.aspx&lt;/a&gt; (it would be nice do it like this function as it will then give you a straight-forward integer value returned it the record exists)&lt;br /&gt;&lt;/p&gt;</description></item><item><title>Re: Check to see if record already exists</title><link>http://forums.asp.net/thread/3273995.aspx</link><pubDate>Fri, 03 Jul 2009 20:41:22 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:3273995</guid><dc:creator>ramireddyindia</dc:creator><author>ramireddyindia</author><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/3273995.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=54&amp;PostID=3273995</wfw:commentRss><description>&lt;p&gt;&lt;pre name="code" class="sql"&gt;Creae a stored proc. like below.

create proc sc_getCount

as

begin

select count(*) from tablename where columnname = @inputvalue

end


&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;then call this stored procedure using either sqlcommand(if you are using layered appraoch in your application, then use them.)&lt;/p&gt;&lt;p&gt;eg: to call using sqlcommand see the below&lt;/p&gt;&lt;p&gt;&lt;pre name="code" class="c-sharp"&gt;SqlConnection sqlConnection1 = new SqlConnection(&amp;quot;Your Connection String&amp;quot;);
SqlCommand cmd = new SqlCommand();
Object returnValue;

cmd.CommandText = &amp;quot;StoredProcedureName&amp;quot;;
cmd.CommandType = CommandType. StoredProcedure;
cmd.Connection = sqlConnection1;

sqlConnection1.Open();

returnValue = cmd.ExecuteScalar();

sqlConnection1.Close();
&lt;/pre&gt;&lt;br /&gt; &lt;br /&gt;&lt;/p&gt;</description></item><item><title>Re: Check to see if record already exists</title><link>http://forums.asp.net/thread/3273994.aspx</link><pubDate>Fri, 03 Jul 2009 20:40:57 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:3273994</guid><dc:creator>Naom</dc:creator><author>Naom</author><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/3273994.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=54&amp;PostID=3273994</wfw:commentRss><description>&lt;p&gt;The simplest and best way would be to put this in SP and also have a unique index on the column to ensure no duplicates.&lt;br /&gt;&lt;/p&gt;</description></item><item><title>Check to see if record already exists</title><link>http://forums.asp.net/thread/3273905.aspx</link><pubDate>Fri, 03 Jul 2009 18:28:27 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:3273905</guid><dc:creator>tcreynolds</dc:creator><author>tcreynolds</author><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/3273905.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=54&amp;PostID=3273905</wfw:commentRss><description>&lt;p&gt;&amp;nbsp;I need a way to check to see if a record has already been inputted. If it has, the record should not be reinputted and should raise an error.&lt;/p&gt;
&lt;p&gt;I&amp;#39;m wondering whether this would be best done in C#, my stored procedure, or both. This is for a site where the user completes a survey and the information is stored in my database. Once the survey is completed, they should not have any way to modify their input.&lt;/p&gt;</description></item></channel></rss>