473,473 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Duplicate keys

Hi,

I have dropdownlist named ddlapplication in my web form. When I
click any item it insert
that record in database with primary key and show in gridview.
If user try to add the same record next time by clicking same item
from the dropdown list
which already exist in database it should show error message that
this
record alredy exist or in other words before inserting it should check
wheather the record is already in database or not.

How can I do that. Here is my code

string str1 = this.ddlApplication.DataTextField;
string str2 = this.ddlApplication.SelectedValue;
string str3 = this.ddlPermission.SelectedItem.Value;
String constr =
System.Configuration.ConfigurationManager.Connecti onStrings["twcsanConnectionString"].ConnectionString;
SqlConnection objConn = new SqlConnection(constr);
SqlCommand objCmd = new SqlCommand("INSERT INTO
tbUsersAppRights(AppId, AppRightId, UserId) " +
"VALUES (@str1,@str2,@str3)", objConn);
objCmd.Parameters.AddWithValue("@str1",
int.Parse(this.ddlApplication.SelectedValue));
objCmd.Parameters.AddWithValue("@str2",
int.Parse(this.ddlPermission.SelectedValue));
objCmd.Parameters.AddWithValue("@str3",
int.Parse(this.lblUserid11.Text));

objConn.Open();
objCmd.ExecuteNonQuery();
objConn.Close();
this.gdAddUser.DataBind();

May 17 '07 #1
1 1297

<bb****@yahoo.comwrote in message
news:11**********************@q23g2000hsg.googlegr oups.com...
Hi,

I have dropdownlist named ddlapplication in my web form. When I
click any item it insert
that record in database with primary key and show in gridview.
If user try to add the same record next time by clicking same item
from the dropdown list
which already exist in database it should show error message that
this
record alredy exist or in other words before inserting it should check
wheather the record is already in database or not.

How can I do that. Here is my code
1. Make a select before

SELECT count(*) FROM tbUsersAppRights WHERE ...

if it returned 0 -no records found and you can INSERT INTO

otherwise, show an error message

2. Exception handling using try...catch

3. Use a stored procedure
May 17 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Manish | last post by:
The topic is related to MySQL database. Suppose a table "address" contains the following records ------------------------------------------------------- | name | address | phone |...
2
by: Rich | last post by:
Hello, I am just checking if there is a property or technique for displaying or retrieving from a dataTable the top 1 row(s) for rows containing duplicate keys (IDs). I have to pull data from a...
2
by: Harry Haller | last post by:
I want to duplicate the form data, edit it to remove some items (such as __EVENTTARGET, __EVENTVALIDATION, etc) and save it to a log. How can I make a duplicate (editable) copy of the Form...
6
by: reppisch | last post by:
Hi Ng, I have a multiset for keeping elements sorted in a container but key values may also be equal. Is there any guaranteed traversal order within the duplicate keys of a multimap? When...
1
by: st12iker | last post by:
I managed to write a file to a hash. However the file contains multiple keys and value in the format listed. Note how the values are sorted in descending order. i.e. of file written to hash ...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.