Results 1 to 2 of 2

Thread: Insert into

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2009
    Posts
    1,058

    Insert into

    Hi,

    I have the following SQL on my dataset (trainingloc.xsd)

    Code:
    SELECT        - 1 AS LocationID, '--Please Select--' AS TrainingLoc
    UNION ALL
    SELECT        LocationID, TrainingLoc
    FROM            tblTrainingLocType
    ORDER BY LocationID
    Now, on Form Load,

    I have

    Training Location (combobox)
    --Please Select--
    Location A
    Location B
    Location C

    Now, there is an insert statement when to enter fields as below;

    Code:
                                                                              Try
    				Using sqlConn As SqlConnection = GetDbConnection()
    					Using sqlCmnd As New SqlCommand("INSERT INTO dbo.tblCustomer (TrainingLocation, Notes) Values ( @TrainingLocation, @Notes)", sqlConn)
    						With sqlCmnd.Parameters
    							.AddWithValue("@TrainingLocation", IIf(cmbtrainloc.Text.Length > 0, cmbtrainloc.Text, DBNull.Value))
    							.AddWithValue("@Notes", IIf(txtnotes.Text.Length > 0, txtnotes.Text, DBNull.Value))
    						End With
    						sqlCmnd.ExecuteNonQuery()
    						sqlConn.Close()
    					End Using
    				End Using

    The issue is - when it is the --Please Select--, I would like to enter NOTHING to the database. At present, it enters and saves --Please Select--

    Please note; this field does allow NULL values, in other occassions the location cited might not be in the combo box list.


    Thank you

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Insert into

    Just enclose your sql insert block in an if statement that tests cmbtrainloc.Text

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width