Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Prevent Duplicate Entry while update

Prevent Duplicate Entry while update

Scheduled Pinned Locked Moved Database
databasehelpquestionannouncement
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    eraser950
    wrote on last edited by
    #1

    Helllo I have a problem while updating data in Sql table, i have a table named City it contains CityID,CityName,CityShortName i have created a stored procedure PROCEDURE [dbo].[UpdateCity] @CityID int,@CityName nvarchar(50),@CityShortName nvarchar(50) AS BEGIN Declare @ReturnVal nvarchar(20) SET NOCOUNT ON; SET XACT_ABORT ON Begin Try IF EXISTS (SELECT CityName From City WHERE CityName =@CityName) BEGIN SET @ReturnVal ='Already Exists!' --Return @ReturnVal END ELSE BEGIN Update City Set CityName=@CityName,CityShortName=@CityShortName where CityID=@CityID Set @ReturnVal ='Updated SuccessFully' --Return @ReturnVal End end try but when i m just editing Shortname its not updating , but its working perfect when i m editing either cityname or cityname and shortname,, i know that i m doing mistake but where i can't found second thng how can i show message updated successfully or already exist as i m using asp gridview Please help me Thanx

    B 1 Reply Last reply
    0
    • E eraser950

      Helllo I have a problem while updating data in Sql table, i have a table named City it contains CityID,CityName,CityShortName i have created a stored procedure PROCEDURE [dbo].[UpdateCity] @CityID int,@CityName nvarchar(50),@CityShortName nvarchar(50) AS BEGIN Declare @ReturnVal nvarchar(20) SET NOCOUNT ON; SET XACT_ABORT ON Begin Try IF EXISTS (SELECT CityName From City WHERE CityName =@CityName) BEGIN SET @ReturnVal ='Already Exists!' --Return @ReturnVal END ELSE BEGIN Update City Set CityName=@CityName,CityShortName=@CityShortName where CityID=@CityID Set @ReturnVal ='Updated SuccessFully' --Return @ReturnVal End end try but when i m just editing Shortname its not updating , but its working perfect when i m editing either cityname or cityname and shortname,, i know that i m doing mistake but where i can't found second thng how can i show message updated successfully or already exist as i m using asp gridview Please help me Thanx

      B Offline
      B Offline
      Bernhard Hiller
      wrote on last edited by
      #2

      Here is the source of your problem:

      eraser950 wrote:

      IF EXISTS (SELECT CityName From City WHERE CityName =@CityName) BEGIN SET @ReturnVal ='Already Exists!' --Return @ReturnVal END

      When you do not change the city name, that if clause returns true, and hence you return 'Already Exists!'. Try:

      IF EXISTS (SELECT CityName From City WHERE CityName =@CityName AND CityID<>@CityID)

      E 2 Replies Last reply
      0
      • B Bernhard Hiller

        Here is the source of your problem:

        eraser950 wrote:

        IF EXISTS (SELECT CityName From City WHERE CityName =@CityName) BEGIN SET @ReturnVal ='Already Exists!' --Return @ReturnVal END

        When you do not change the city name, that if clause returns true, and hence you return 'Already Exists!'. Try:

        IF EXISTS (SELECT CityName From City WHERE CityName =@CityName AND CityID<>@CityID)

        E Offline
        E Offline
        eraser950
        wrote on last edited by
        #3

        thnx i will try this

        1 Reply Last reply
        0
        • B Bernhard Hiller

          Here is the source of your problem:

          eraser950 wrote:

          IF EXISTS (SELECT CityName From City WHERE CityName =@CityName) BEGIN SET @ReturnVal ='Already Exists!' --Return @ReturnVal END

          When you do not change the city name, that if clause returns true, and hence you return 'Already Exists!'. Try:

          IF EXISTS (SELECT CityName From City WHERE CityName =@CityName AND CityID<>@CityID)

          E Offline
          E Offline
          eraser950
          wrote on last edited by
          #4

          Thnx its working perfect kindly tell me is this good approach or theres any other method to do this? second how can show message as i m usng sqldatasource(GridView) to update and delete

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups