Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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 <code>TextBox</code> and <code>DropDownList</code>. I then pass those values to the following function in my codebehind:</p> <pre class="lang-vb prettyprint-override"><code>'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 &gt; 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 &lt; 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 </code></pre> <p>Then my stored procedure handles the values something like so...</p> <p><em>(Forgive me if I neglected to replace some of the values. I tried to catch them all.)</em></p> <pre><code>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) &gt; LEN(@User) AND CHARINDEX(' ', @User) &gt; 0 BEGIN -- Confirm First &amp; 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 &amp; Last name IF LEN(@forename) &gt; 1 AND LEN(@surname) &gt; 1 BEGIN -- Confirm First &amp; 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 &amp; replaces spaces &amp; 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 </code></pre> <p>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. :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload