Inserting Null values

Results 1 to 2 of 2

Thread: Inserting Null values

  1. #1
    Join Date
    Dec 1969

    Default Inserting Null values

    I&#039;m trying to insert values into a database using a stored procedure. The problem is that it works fine as long as all the parameter values are not null but some of the fields need to allow for NULL values. <BR>I have tried setting myParameter.IsNullable = True and that didn&#039;t work. I am quite stuck at the moment. <BR>Here is my code (shortened).<BR><BR> Dim myCommand As SqlCommand<BR> myConnection.Open()<BR> myCommand = New SqlCommand("AddStandardLease", myConnection)<BR> myCommand.CommandType = CommandType.StoredProcedure<BR><BR> LseId = GetLeaseID()<BR> Dim parameterID As New SqlParameter("@LeaseID", SqlDbType.Int)<BR> parameterID.Value = LseId<BR> myCommand.Parameters.Add(parameterID)<BR><BR> Dim parameterDate As New SqlParameter("@LseDate", SqlDbType.SmallDateTime)<BR> parameterDate.Value = LseDate.SelectedDate<BR> myCommand.Parameters.Add(parameterDate)<BR><BR> Dim parameterPeriod As New SqlParameter("@LsePeriod", SqlDbType.Int)<BR> parameterPeriod.Value = LeasePeriod<BR> myCommand.Parameters.Add(parameterPeriod)<BR><BR> Dim parameterFloorArea As New SqlParameter("@LseFloorArea", SqlDbType.Float)<BR> parameterFloorArea.Value = LseFloorArea.Text<BR> myCommand.Parameters.Add(parameterFloorArea)<BR><B R> Dim parameterDepositAmount As New SqlParameter("@LseDepositAmount", SqlDbType.Char, 50)<BR> parameterDepositAmount.Value = LseDepositAmount.Text<BR> myCommand.Parameters.Add(parameterDepositAmount)<B R><BR> Dim parameterAdmin As New SqlParameter("@LseAdmin", SqlDbType.VarChar, 255)<BR> parameterAdmin.Value = LseAdmin.Text<BR> myCommand.Parameters.Add(parameterAdmin)<BR><BR> myCommand.ExecuteNonQuery()<BR> myConnection.Close()

  2. #2
    Join Date
    Dec 1969

    Default RE: Inserting Null values

    Hi Penny<BR><BR>I think you need to use the &#039;Allow DB Null&#039; method.<BR>Here is some code that handles null values returned from a DB. It&#039;s not exactly what you want but I hope it helps!<BR><BR>CW.<BR><BR>&#060;code&#062;<BR>Publi c Function IsDBNull(ByVal dbvalue) As Boolean<BR> Return dbvalue Is DBNull.Value <BR>End Function<BR><BR>Public Function FixNull(ByVal dbvalue) As String<BR> If dbvalue Is DBNull.Value Then<BR> Return ""<BR> Else<BR> &#039;NOTE: This will cast value to string if<BR> &#039;it isn&#039;t a string.<BR><BR> Return dbvalue.ToString<BR> End If<BR>End Function<BR>&#060;/code&#062;

Posting Permissions

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