Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7167441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:34:44+00:00 2026-05-28T14:34:44+00:00

The title, while long, pretty much says it all. What I have is a

  • 0

The title, while long, pretty much says it all.

What I have is a master table with a bunch of supporting table relations through foreign keys. For a few of the foreign tables, upon attempting to insert a record into the master table where one of the foreign keys doesn’t exist, the data would be passed to the foreign table to create the record first, thereby making the constraint valid and passing the key to the created record back to the insert procedure of the master table.

This data comes from a form in String form, but naturally the foreign key will be an int. The process would look something like this:

-- ASP.NET Web Form --
Requestor Name:    _____________ (combobox)
Request:           _____________ (dropdownlist)
Date:              _____________ (datepicker)

This is a super simplified version, but assume we have a master table with the above data, where both names are foreign keys to a People table. The name fields are comboboxes with a populated list of names linking to People. However, if I wanted to enter a person who didn’t yet exist in the People table, the procedure should first create the Person, then use the ID from that new record as the foreign key in the Master table containing columns for the above.

I’m using SQL Server and ASP.NET with VB.NET codebehind. I’ve been scratching my head over this one for awhile, how to pass data (in different forms such as a foreign key or string) between the web server and DB server, as well as where to validate / transform the data.

It seems the entered name will be passed as an ID if the foreign key exists, and a String if not.

This is my most perplexing problem so far, and no idea where else to look. I’ve read up on Scott Mitchell’s site and others.

MY SOLUTION (?)

The best I can come up with is to pass the user input from the user as a string and convert it to int in the T-SQL procedure. If the value was selected from the drop down, it should match precisely with a valid foreign key. If it doesn’t match, then create a new Person and return a foreign key. Is this best practice?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:34:45+00:00Added an answer on May 28, 2026 at 2:34 pm

    Here’s my implementation. I don’t know if it’s the best, but it worked well for me. Basically I take the values from the controls; in the case of the combobox I need the values from both the TextBox and DropDownList. I then pass those values to the following function in my codebehind:

    'This method determines if the name selected already exists in the selection
    '  options and if so assigns the corresponding ID value to an object variable,
    '  if not it assigns the value of the `TextBox` to the variable.
    Protected Function _ValidateValues(ByRef ddl As DropDownList, ByRef cb As TextBox) As Object
        'Ensures the selected value is valid by checking against the entered value in the textbox
        If Not String.IsNullOrEmpty(cb.Text) Then
            If ddl.Items.Count > 0 Then
                If StrComp(cb.Text, ddl.SelectedItem.ToString) = 0 Then
                    Return ddl.Items.Item(ddl.SelectedIndex).Value 'Returns the index of dropdown selected name
                End If
            End If
            'This counts the capital letters in the entered value and if fewer than 2
            '  auto capitalizes the first letters. This also allows for project code
            '  names such as "DOORS" and people names such as "Allen McPherson" etc.
            '  Be careful though because if "allen McPherson" is entered, it will NOT
            '  be corrected, though it displays correctly.
            Dim rg As New Regex("[A-Z]")
            Dim mc As MatchCollection = rg.Matches(cb.Text)
            If mc.Count < 2 Then
                Return StrConv(cb.Text, VbStrConv.ProperCase)
            Else : Return cb.Text
            End If
        End If
        'Returns a SQL DB NULL object if an empty string is submitted
        Return DBNull.Value
    End Function
    

    Then my stored procedure handles the values something like so…

    (Forgive me if I neglected to replace some of the values. I tried to catch them all.)

    CREATE PROCEDURE spInsertUser
        @User nvarchar(50)    = NULL,
        @Role nvarchar(50)    = NULL,
        @RecordID int output  -- Returned Value
    AS
    BEGIN
        -- SET NOCOUNT ON added to prevent extra result sets from
        -- interfering with SELECT statements.
        SET NOCOUNT ON;
    
        -- CHECK IF USER EXISTS
        -- Insert new record to Users table if the requested user doesn't exist
        -- Needed to ensure that the foreign keys are relevant
        IF @User = '' OR @User IS NULL BEGIN SET @User = NULL SET @RecordID = NULL END --Ensures that an empty string cannot be submitted, thereby causing an error.
        ELSE BEGIN
            declare @forename varchar(50), @surname varchar(50)
            declare @uid table (ID int)
            declare @users table (ID smallint, Name nvarchar(50))
            insert into @users
            select ID, Name from Users
    
            --If the value can be converted into an int, we need go no further.
            BEGIN TRY SET @RecordID = CONVERT(smallint, @User) END TRY
                BEGIN CATCH
                    BEGIN TRY --Otherwise, attempt to parse the name
                        Set @User = LTRIM(RTRIM(@User)) --Trim the extra space at the beginning and end. This ensures the following "IF" test will evaluate properly.
                        IF NOT CHARINDEX(' ', @User) > LEN(@User) AND CHARINDEX(' ', @User) > 0 BEGIN -- Confirm First & Last Name exist
                            Set @forename = RTRIM(LEFT(@User, CHARINDEX(' ',@User,0)-1))
                            Set @surname = LTRIM(RIGHT(@User, LEN(@User) - CHARINDEX(' ',@User,0)))
                            Set @User = @forename + ' ' + @surname --Ensure that there is a valid First & Last name
                            IF LEN(@forename) > 1 AND LEN(@surname) > 1 BEGIN -- Confirm First & Last Name exist
                                --First ensure that the User doesn't already exist, and if
                                --  so use their ID, if not insert the new User.
                                IF NOT EXISTS (select Name from @users where Name like @User) BEGIN --Check if the user already exists
                                    INSERT INTO Users (Name, Forename, Surname) OUTPUT INSERTED.ID INTO @uid Values (@User, -- If not, insert them
                                        @forename, @surname) --Nicely manicured first, last, and full names
                                    SET @RecordID = CONVERT(smallint, (select MAX(ID) from @uid)) END -- Now set the Role to the ID of the new user
                                ELSE BEGIN --Otherwise if the user already exists, set the Role to the ID of that user
                                    SET @RecordID = (select ID from @users where Name like @User) END
    
                                IF NOT EXISTS (select * from rUsersInRoles where UserID = @RecordID) BEGIN
                                --Do some string manipulation to increase the chances of matching the role
                                SET @Role = REPLACE(REPLACE(REPLACE(LTRIM(RTRIM(@Role)), ' ', '%'), '.', '%'), '@', '%') --Trims & replaces spaces & periods with wildcards
                                INSERT INTO rUsersInRoles (UserID, UserRoleID) VALUES
                                    (@RecordID, (select top 1 ID from rUserRoles where Role like @Role)) END
                            END
                        END
                    END TRY
                    BEGIN CATCH END CATCH    
                END CATCH
        END
    END
    

    This stored procedure deals with the case of User Roles as well. If the more simple case of Users only is needed, simply remove the clauses dealing with the checking and insertion of User Roles. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The title pretty much says it all. When I'm doing some reflection through my
The title pretty much says it already. I've looked through the AMD website and
Title pretty much sums it up. The external style sheet has the following code:
I'm kind of stumped. I have a table of items with fields: ID, title,
like the title says, I have a single bug in a script for a
As the title suggests I am looking for a open source ERP package. While
Title says what i'm trying to do. I can successfully generate an assembly if
Title says it mostly. I want to add a simple extension method to the
Title might be a bit confusing, so let me explain. I have a website
I have a IBAction that looks like this: self.title = @Logging in ...; [MyClass

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.