View Full Version : How to force a value to be in a db
paulafernandes
12-03-2002, 11:56 AM
Hello!!!
I have a form with some combobox that are filled with the content of tables from the database (SQL Server 7.0). The combos show the "name" and store the "code".
Since the user is not forced to chose one of the options in the combo, I added a selected value called "None".
Now... My problem...
If the user choses the "None", how I'm going to save it on the database? (the database record is an integer)
I was thinking that in the table should be an record with, ex: "code" = 1 and "name"=none. But, I'm going to deliver an empty database, how do I do it????????
I'm being to confuse?...
Thank's
Paula
codefox
12-03-2002, 12:11 PM
I'm not too sure if I got your question right. If you want to "save" the code or name the user chooses only if the name is not "None", do not save that record. Or, if you are wondering how to save the code if the user chooses "none" insert a null for that column (well, i assume your code field can store null values).
paulafernandes
12-03-2002, 12:17 PM
Yes, that was my first option but... The situation is this...
I have a secondary table that fills the combo. The user selects one option and the code of that option is stored in the main table. The problem of storing a null value is that the code is a primary key in the secondary table.
Paula
codefox
12-03-2002, 12:27 PM
Why do you want to insert a value for the code when the user selects the name "none" from the combo box? Anyway there's no corresponding code for "none" and hopefully there's none by the name "none" :)
paulafernandes
12-03-2002, 12:32 PM
Because if I don't select a value on the combo, my SQL Insert String doesn't work...
glenngv
12-03-2002, 12:40 PM
so why not force the user to select a name?
paulafernandes
12-03-2002, 12:47 PM
Because in some cases I canīt force the user to select a value. Thatīs why I put the option value "none". But, because of my SQL insert string I need to put a value on that field...
Paula
codefox
12-03-2002, 12:48 PM
Give the corresponding value for "None" in the combo box as "". For example,
<select name="cmbUser">
<option value="">None</option>
<%
While Not <your secondary table's recordset>
Response.Write "<option value=" & rs("code") & ">" & rs("name") & "</option>"
Wend
%>
</select>
...
Now in your form processing script say
If Request.Form("cmbUser") <> "" Then
sSQL = "INSERT INTO Table VALUES..."
End If
That would insert the code only if the name was not "none'
glenngv
12-03-2002, 12:54 PM
In what case you can't force the user to select a value?
codefox's code is one way to do it. expanding on it:
If Request.Form("cmbUser") <> "" Then
sSQL = "INSERT INTO Table VALUES..."
else
response.write("Please select a name!")
End If
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.