dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online MembersSriram
devin
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Resources » Code Snippets » General

How to check whether the record exists or not in the database while inserting new record using C#?


Posted Date:     Category: General    
Author: Member Level: Gold    Points: 7


This code is used when unique logins have to be created as same credentials can not be provided to user. Depending up on the boolean value returned by the function, you can display success or failure message to user. I hope it will be useful for you all.



 


How to check whether the record exists or not in the database while inserting new record in C#?
Example:
Suppose user wants to add new unique record to the database then he needs to check whether that record is already present in database or not. If it is present then function will return true else false.

public bool checkRecordExistsOrNot(string login)
{
string ConnString = GetConnString();
string sqlString = "SELECT yourtablename.yourcolumnname FROM yourtablename WHERE yourcolumnname= ?";
using (OleDbConnection conn = new OleDbConnection(ConnString))
{
using (OleDbCommand cmd = new OleDbCommand(sqlString, conn))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("?yourcolumnname", OleDbType.VarChar).Value = login;
conn.Open();
if (cmd.ExecuteScalar() != null)
{
conn.Close();
conn.Dispose();
return true;
}
else
{
conn.Close();
conn.Dispose();
return false;
}
}
}
}


GetConnString() method is used to return the value of ConnectionString which is stored in web.config.


public static string GetConnString()
{
return ConfigurationManager.AppSettings["ConnectionString"].ToString();
}





Did you like this resource? Share it with your friends and show your love!


Responses to "How to check whether the record exists or not in the database while inserting new record using C#?"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: How to insert new record in database using c#?
    Previous Resource: How to view HTML source of any webpage using VB.net?
    Return to Resources
    Post New Resource
    Category: General


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    (No tags found.)



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.