Duplicate keys

Discussion in 'ASP .Net' started by bbawa1@yahoo.com, May 17, 2007.

  1. Guest

    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.ConnectionStrings["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, 2007
    #1
    1. Advertising

  2. <> wrote in message
    news:...
    > 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
     
    Alexey Smirnov, May 17, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    14
    Views:
    14,219
    Zer0Frequency
    Feb 16, 2005
  2. L Clarke

    Duplicate Keys in a Map !

    L Clarke, Feb 22, 2006, in forum: Java
    Replies:
    7
    Views:
    36,960
    bugbear
    Feb 27, 2006
  3. =?Utf-8?B?RGFiYmxlcg==?=

    ConfigurationSection with duplicate keys?

    =?Utf-8?B?RGFiYmxlcg==?=, Apr 26, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    4,204
    =?Utf-8?B?RGFiYmxlcg==?=
    Apr 27, 2006
  4. reppisch
    Replies:
    6
    Views:
    1,010
    reppisch
    Jun 19, 2007
  5. vsoler

    Duplicate keys in dict?

    vsoler, Mar 7, 2010, in forum: Python
    Replies:
    6
    Views:
    348
    Yinon Ehrlich
    Mar 9, 2010
Loading...

Share This Page