[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Check for duplicate values before letting user insert

Posted on 2012-08-10
7
Medium Priority
?
337 Views
Last Modified: 2012-08-12
I have an asp.net page that allows users to insert new records into a table. When they enter the data, I want to do a search of the empnum field to ensure that value does not exist already in the system. So before inserting, the page searches the users table for the empnum entered by the user. If it exits, show a message stating it already exists. If not, insert the record. What is the best way to do this? I'm using C# code behind.
0
Comment
Question by:dodgerfan
  • 5
  • 2
7 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 38281590
string strConn="your connection string here";
string sql="SELECT COUNT(*) FROM Table1 WHERE ID=1";


using (SqlConnection conn = new SqlConnection(strConn))
                {

                    using (SqlCommand cmd = new SqlCommand(sql, conn))
                    {
                        conn.Open();
                        int count = (int)cmd.ExecuteScalar();
                        if (count> 0)
                            //record exists
                        else
                           //insert here
                    }

                }

Open in new window

0
 
LVL 35

Accepted Solution

by:
YZlat earned 2000 total points
ID: 38281613
string strConn="your connection string here";
string sql="SELECT COUNT(*) FROM Table1 WHERE ID=1";


using (SqlConnection conn = new SqlConnection(strConn))
{
	using (SqlCommand cmd = new SqlCommand(sql, conn))
        {
        	conn.Open();
                int count = (int)cmd.ExecuteScalar();
                if (count = 0)
                {
			using (SqlCommand cmdInsert = new SqlCommand("insert into Table1 (ID,Name) values (1,'test');", conn))
			{ 
				cmdInsert.ExecuteNonQuery(); 
			}
		}
         }

}

Open in new window

0
 

Author Comment

by:dodgerfan
ID: 38282350
YZlat,
I'm grabbing the empum from a value entered into a textbox. The value can contain letters, so it's a string. Your code is erroring out on the line: int count = (int)cmd.ExecuteScalar(). It does not know what to do, I guess. Can I fix that?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 35

Expert Comment

by:YZlat
ID: 38283925
can you post your sql statement?
0
 
LVL 35

Expert Comment

by:YZlat
ID: 38283932
your sql should be something like

sql="SELECT COUNT(*) FROM Table1 WHERE empnum='" + TextBox1.Text + "'";
0
 
LVL 35

Expert Comment

by:YZlat
ID: 38283933
if the field in your where clause is of type text, you will need single quotes around the value

WHERE empnum = 'Test'
0
 

Author Closing Comment

by:dodgerfan
ID: 38285404
Got it, thanks for the help. I just needed to get the syntax with those single and double quotes correct.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question