Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Duplicate keys

Reply
Thread Tools

Duplicate keys

 
 
bbawa1@yahoo.com
Guest
Posts: n/a
 
      05-17-2007
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();

 
Reply With Quote
 
 
 
 
Alexey Smirnov
Guest
Posts: n/a
 
      05-17-2007

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Duplicate keys in dict? vsoler Python 6 03-09-2010 12:27 PM
stl multimap, insert with duplicate keys, is ordering stable? reppisch C++ 6 06-19-2007 09:38 AM
ConfigurationSection with duplicate keys? =?Utf-8?B?RGFiYmxlcg==?= ASP .Net 4 04-27-2006 02:56 AM
Duplicate Keys in a Map ! L Clarke Java 7 02-27-2006 11:21 AM
URGENT - plz help - Duplicate Keys in Hashmap :( alisyedm@gmail.com Java 14 02-16-2005 04:07 PM



Advertisments