Cannot insert the value NULL into column 'OrderID' -- BUT IT IS NOT NULL!

Last post 10-31-2007 7:13 PM by Stevishere. 8 replies.

Sort Posts:

  • Cannot insert the value NULL into column 'OrderID' -- BUT IT IS NOT NULL!

    04-02-2007, 4:11 AM
    • Member
      140 point Member
    • madkidd
    • Member since 08-19-2005, 2:37 AM
    • Charlotte, NC
    • Posts 40

    I am getting this error: "Cannot insert the value NULL into column 'OrderID', table 'outman.outman.Contact'; column does not allow nulls. INSERT fails." -- But my value is not null. I did a response.write on it and it show the value. Of course, it would be nice if I could do a breakpoint but that doesn't seem to be working. I'll attach a couple of images below of my code, the error, and the breakpoint error.

     

     

     

    Server Error in '/' Application.

    Cannot insert the value NULL into column 'OrderID', table 'outman.outman.Contact'; column does not allow nulls. INSERT fails.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.Data.SqlClient.SqlException: Cannot insert the value NULL into column 'OrderID', table 'outman.outman.Contact'; column does not allow nulls. INSERT fails.

    Source Error:

    Line 89:         sContact.Phone = sPhone.Text.Trim
    Line 90:         sContact.Email = sEmail.Text.Trim
    Line 91:         sContact.Save()
    Line 92: 
    Line 93:         Dim bContact As Contact = New Contact()

    Source File: F:\Inetpub\wwwroot\Outman Knife\Checkout.aspx.vb    Line: 91

    Stack Trace:

    [SqlException (0x80131904): Cannot insert the value NULL into column 'OrderID', table 'outman.outman.Contact'; column does not allow nulls. INSERT fails.]
       System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +857354
       System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +734966
       System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +188
       System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +1838
       System.Data.SqlClient.SqlDataReader.HasMoreRows() +150
       System.Data.SqlClient.SqlDataReader.ReadInternal(Boolean setTimeout) +214
       System.Data.SqlClient.SqlDataReader.Read() +9
       System.Data.SqlClient.SqlCommand.CompleteExecuteScalar(SqlDataReader ds, Boolean returnSqlValue) +39
       System.Data.SqlClient.SqlCommand.ExecuteScalar() +148
       SubSonic.SqlDataProvider.ExecuteScalar(QueryCommand qry) +209
       SubSonic.DataService.ExecuteScalar(QueryCommand cmd) +37
       SubSonic.ActiveRecord`1.Save(String userName) +120
       SubSonic.ActiveRecord`1.Save() +31
       Checkout.btnCheckout_Click(Object sender, EventArgs e) in F:\Inetpub\wwwroot\Outman Knife\Checkout.aspx.vb:91
       System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
       System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
       System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102
    


    Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.42

    Best regards,

    Jeremy Hodges | Windows Server 2003 MCP
    Managed Microsoft hosting: www.orcsweb.com
  • Re: Cannot insert the value NULL into column 'OrderID' -- BUT IT IS NOT NULL!

    04-02-2007, 6:53 AM
    • All-Star
      61,491 point All-Star
    • TATWORTH
    • Member since 02-04-2003, 8:34 AM
    • England
    • Posts 12,064
    • TrustedFriends-MVPs
    Please post the code of the Save method of your Contact class, the stored procedure that it calls and the table definition.
    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
  • Re: Cannot insert the value NULL into column 'OrderID' -- BUT IT IS NOT NULL!

    04-02-2007, 2:53 PM
    • Member
      140 point Member
    • madkidd
    • Member since 08-19-2005, 2:37 AM
    • Charlotte, NC
    • Posts 40

    I am using SubSonic, so I am not sure how I would do this. Here is the entire Contact class:

    1            Imports System
    2    Imports System.Text
    3    Imports System.Data
    4    Imports System.Data.SqlClient
    5    Imports System.Data.Common
    6    Imports System.Collections
    7    Imports System.Collections.Generic
    8    Imports System.Configuration
    9    Imports System.Xml
    10   Imports System.Xml.Serialization
    11   Imports SubSonic
    12   
    13   
    14   
    15   ''' <summary>
    16   ''' Strongly-typed collection for the Contact class.
    17   ''' </summary>
    18   <Serializable> _
    19   Public Partial Class ContactCollection
    20       Inherits ActiveList(Of Contact)
    21   
    22       Private wheres As List(Of Where) = New List(Of Where)()
    23       Private betweens As List(Of BetweenAnd) = New List(Of BetweenAnd)()
    24       Private orderBy As SubSonic.OrderBy
    25       Public Function OrderByAsc(ByVal columnName As String) As ContactCollection
    26           Me.orderBy = SubSonic.OrderBy.Asc(columnName)
    27           Return Me
    28       End Function
    29       Public Function OrderByDesc(ByVal columnName As String) As ContactCollection
    30           Me.orderBy = SubSonic.OrderBy.Desc(columnName)
    31           Return Me
    32       End Function
    33       Public Function WhereDatesBetween(ByVal columnName As String, ByVal dateStart As DateTime, ByVal dateEnd As DateTime) As ContactCollection
    34           Return Me
    35   
    36       End Function
    37   
    38       Public Function Where(ByVal where_Renamed As Where) As  ContactCollection
    39           wheres.Add(where_Renamed)
    40           Return Me
    41       End Function
    42       Public Function Where(ByVal columnName As String, ByVal value As Object) As ContactCollection
    43           Dim where_Renamed As Where = New Where()
    44           where_Renamed.ColumnName = columnName
    45           where_Renamed.ParameterValue = value
    46           Me.Where(where_Renamed)
    47           Return Me
    48       End Function
    49       Public Function Where(ByVal columnName As String, ByVal comp As Comparison, ByVal value As Object) As ContactCollection
    50           Dim where_Renamed As Where = New Where()
    51           where_Renamed.ColumnName = columnName
    52           where_Renamed.Comparison = comp
    53           where_Renamed.ParameterValue = value
    54           Me.Where(where_Renamed)
    55           Return Me
    56   
    57       End Function
    58       Public Function BetweenAnd(ByVal columnName As String, ByVal dateStart As DateTime, ByVal dateEnd As DateTime) As ContactCollection
    59           Dim between As BetweenAnd = New BetweenAnd()
    60           between.ColumnName = columnName
    61           between.StartDate = dateStart
    62           between.EndDate = dateEnd
    63           betweens.Add(between)
    64           Return Me
    65       End Function
    66       Public Overloads Function Load() As ContactCollection
    67   
    68           Dim qry As Query = New Query("Contact")
    69           For Each where As Where In wheres
    70               qry.AddWhere(where)
    71           Next
    72           For Each between As BetweenAnd In betweens
    73               qry.AddBetweenAnd(between)
    74           Next
    75   
    76           If Not orderBy Is Nothing Then
    77               qry.OrderBy = orderBy
    78           End If
    79   
    80           Dim rdr As IDataReader = qry.ExecuteReader()
    81           Me.Load(rdr)
    82           rdr.Close()
    83   
    84           Return Me
    85       End Function
    86   	
    87   	Public Overloads Function Load(ByVal qry As Query) As ContactCollection
    88   
    89           Dim rdr As IDataReader = qry.ExecuteReader()
    90           Me.Load(rdr)
    91           rdr.Close()
    92   
    93           Return Me
    94       End Function
    95   
    96       Public Sub New()
    97   
    98   
    99       End Sub
    100  
    101  End Class
    102  
    103  ''' <summary>
    104  ''' This is an ActiveRecord class which wraps the Contact table.
    105  ''' </summary>
    106  <Serializable> _
    107  Public Partial Class Contact
    108      Inherits ActiveRecord(Of Contact)
    109  
    110      #Region "Default Settings"
    111      Private Sub SetSQLProps()
    112          If Schema Is Nothing Then
    113              Schema = Query.BuildTableSchema("Contact")
    114          End If
    115      End Sub
    116      #End Region
    117  
    118      #Region "Schema Accessor"
    119      Public Shared Function GetTableSchema() As TableSchema.Table
    120          Dim item As Contact = New Contact()
    121          Return Contact.Schema
    122      End Function
    123      #End Region
    124  
    125          #Region "Query Accessor"
    126      Public Shared Function CreateQuery() As Query
    127          Return New Query("Contact")
    128      End Function
    129      #End Region
    130  
    131      #Region ".ctors"
    132      Public Sub New()
    133          SetSQLProps()
    134          SetDefaults()
    135          Me.MarkNew()
    136      End Sub
    137  
    138      Public Sub New(ByVal keyID As Object)
    139          SetSQLProps()
    140          MyBase.LoadByKey(keyID)
    141      End Sub
    142  
    143      Public Sub New(ByVal columnName As String, ByVal columnValue As Object)
    144          SetSQLProps()
    145          MyBase.LoadByParam(columnName,columnValue)
    146      End Sub
    147      #End Region
    148  
    149      #Region "Public Properties"
    150          <XmlAttribute("OrderID")> _
    151      Public Property OrderID As Guid
    152          Get
    153          Dim result As Object = Me.GetColumnValue("OrderID")
    154           Dim oOut As Guid=Guid.Empty
    155           Try
    156  oOut= New Guid(result.ToString())
    157  Catch
    158  End Try
    159           Return oOut
    160  
    161          End Get
    162          Set
    163              Me.MarkDirty()
    164              Me.SetColumnValue("OrderID", Value)
    165          End Set
    166      End Property
    167      <XmlAttribute("Type")> _
    168      Public Property Type As String
    169          Get
    170          Dim result As Object = Me.GetColumnValue("Type")
    171          Dim sOut As String
    172          If result Is Nothing Then : sOut = String.Empty
    173          Else : sOut = result.ToString()
    174          End If
    175          Return sOut
    176  
    177          End Get
    178          Set
    179              Me.MarkDirty()
    180              Me.SetColumnValue("Type", Value)
    181          End Set
    182      End Property
    183      <XmlAttribute("LastName")> _
    184      Public Property LastName As String
    185          Get
    186          Dim result As Object = Me.GetColumnValue("LastName")
    187          Dim sOut As String
    188          If result Is Nothing Then : sOut = String.Empty
    189          Else : sOut = result.ToString()
    190          End If
    191          Return sOut
    192  
    193          End Get
    194          Set
    195              Me.MarkDirty()
    196              Me.SetColumnValue("LastName", Value)
    197          End Set
    198      End Property
    199      <XmlAttribute("FirstName")> _
    200      Public Property FirstName As String
    201          Get
    202          Dim result As Object = Me.GetColumnValue("FirstName")
    203          Dim sOut As String
    204          If result Is Nothing Then : sOut = String.Empty
    205          Else : sOut = result.ToString()
    206          End If
    207          Return sOut
    208  
    209          End Get
    210          Set
    211              Me.MarkDirty()
    212              Me.SetColumnValue("FirstName", Value)
    213          End Set
    214      End Property
    215      <XmlAttribute("Company")> _
    216      Public Property Company As String
    217          Get
    218          Dim result As Object = Me.GetColumnValue("Company")
    219          Dim sOut As String
    220          If result Is Nothing Then : sOut = String.Empty
    221          Else : sOut = result.ToString()
    222          End If
    223          Return sOut
    224  
    225          End Get
    226          Set
    227              Me.MarkDirty()
    228              Me.SetColumnValue("Company", Value)
    229          End Set
    230      End Property
    231      <XmlAttribute("Address")> _
    232      Public Property Address As String
    233          Get
    234          Dim result As Object = Me.GetColumnValue("Address")
    235          Dim sOut As String
    236          If result Is Nothing Then : sOut = String.Empty
    237          Else : sOut = result.ToString()
    238          End If
    239          Return sOut
    240  
    241          End Get
    242          Set
    243              Me.MarkDirty()
    244              Me.SetColumnValue("Address", Value)
    245          End Set
    246      End Property
    247      <XmlAttribute("City")> _
    248      Public Property City As String
    249          Get
    250          Dim result As Object = Me.GetColumnValue("City")
    251          Dim sOut As String
    252          If result Is Nothing Then : sOut = String.Empty
    253          Else : sOut = result.ToString()
    254          End If
    255          Return sOut
    256  
    257          End Get
    258          Set
    259              Me.MarkDirty()
    260              Me.SetColumnValue("City", Value)
    261          End Set
    262      End Property
    263      <XmlAttribute("StateID")> _
    264      Public Property StateID As Integer
    265          Get
    266          Dim result As Object = Me.GetColumnValue("StateID")
    267          Dim oOut As Integer = 0
    268          Try
    269  oOut = Integer.Parse(result.ToString())
    270  Catch
    271  End Try
    272          Return oOut
    273  
    274          End Get
    275          Set
    276              Me.MarkDirty()
    277              Me.SetColumnValue("StateID", Value)
    278          End Set
    279      End Property
    280      <XmlAttribute("ZipCode")> _
    281      Public Property ZipCode As Integer
    282          Get
    283          Dim result As Object = Me.GetColumnValue("ZipCode")
    284          Dim oOut As Integer = 0
    285          Try
    286  oOut = Integer.Parse(result.ToString())
    287  Catch
    288  End Try
    289          Return oOut
    290  
    291          End Get
    292          Set
    293              Me.MarkDirty()
    294              Me.SetColumnValue("ZipCode", Value)
    295          End Set
    296      End Property
    297      <XmlAttribute("Phone")> _
    298      Public Property Phone As String
    299          Get
    300          Dim result As Object = Me.GetColumnValue("Phone")
    301          Dim sOut As String
    302          If result Is Nothing Then : sOut = String.Empty
    303          Else : sOut = result.ToString()
    304          End If
    305          Return sOut
    306  
    307          End Get
    308          Set
    309              Me.MarkDirty()
    310              Me.SetColumnValue("Phone", Value)
    311          End Set
    312      End Property
    313      <XmlAttribute("Email")> _
    314      Public Property Email As String
    315          Get
    316          Dim result As Object = Me.GetColumnValue("Email")
    317          Dim sOut As String
    318          If result Is Nothing Then : sOut = String.Empty
    319          Else : sOut = result.ToString()
    320          End If
    321          Return sOut
    322  
    323          End Get
    324          Set
    325              Me.MarkDirty()
    326              Me.SetColumnValue("Email", Value)
    327          End Set
    328      End Property
    329  
    330      #End Region
    331  
    332     #Region "ObjectDataSource support"
    333               ''' <summary>
    334            ''' Inserts a record, can be used with the ObjectDataSource
    335           ''' </summary>
    336            Public Shared Sub Insert(ByVal OrderID As Guid,ByVal Type As String,ByVal LastName As String,ByVal FirstName As String,ByVal Company As String,ByVal Address As String,ByVal City As String,ByVal StateID As Integer,ByVal ZipCode As Integer,ByVal Phone As String,ByVal Email As String)
    337                   Dim item As Contact = New Contact()
    338                   		item.OrderID = OrderID
    339  		item.Type = Type
    340  		item.LastName = LastName
    341  		item.FirstName = FirstName
    342  		item.Company = Company
    343  		item.Address = Address
    344  		item.City = City
    345  		item.StateID = StateID
    346  		item.ZipCode = ZipCode
    347  		item.Phone = Phone
    348  		item.Email = Email
    349  
    350                   item.Save(System.Web.HttpContext.Current.User.Identity.Name)
    351            End Sub
    352  
    353               ''' <summary>
    354            ''' Updates a record, can be used with the ObjectDataSource
    355           ''' </summary>
    356            Public Shared Sub Update(ByVal OrderID As Guid,ByVal Type As String,ByVal LastName As String,ByVal FirstName As String,ByVal Company As String,ByVal Address As String,ByVal City As String,ByVal StateID As Integer,ByVal ZipCode As Integer,ByVal Phone As String,ByVal Email As String)
    357                   Dim item As Contact = New Contact()
    358                   		item.OrderID = OrderID
    359  		item.Type = Type
    360  		item.LastName = LastName
    361  		item.FirstName = FirstName
    362  		item.Company = Company
    363  		item.Address = Address
    364  		item.City = City
    365  		item.StateID = StateID
    366  		item.ZipCode = ZipCode
    367  		item.Phone = Phone
    368  		item.Email = Email
    369  
    370                   item.IsNew = False
    371                   item.Save(System.Web.HttpContext.Current.User.Identity.Name)
    372            End Sub
    373  
    374     #End Region
    375  
    376     #Region "Columns Struct"
    377      Public Structure Columns
    378          		Public Shared OrderID As String
    379  		Public Shared Type As String
    380  		Public Shared LastName As String
    381  		Public Shared FirstName As String
    382  		Public Shared Company As String
    383  		Public Shared Address As String
    384  		Public Shared City As String
    385  		Public Shared StateID As String
    386  		Public Shared ZipCode As String
    387  		Public Shared Phone As String
    388  		Public Shared Email As String
    389  Private x As Integer
    390  
    391  
    392          Shared Sub New()
    393          OrderID = "OrderID"
    394  Type = "Type"
    395  LastName = "LastName"
    396  FirstName = "FirstName"
    397  Company = "Company"
    398  Address = "Address"
    399  City = "City"
    400  StateID = "StateID"
    401  ZipCode = "ZipCode"
    402  Phone = "Phone"
    403  Email = "Email"
    404  
    405          End Sub
    406      End Structure
    407     #End Region
    408  
    409  End Class
    410  
    411          
    
     
    Best regards,

    Jeremy Hodges | Windows Server 2003 MCP
    Managed Microsoft hosting: www.orcsweb.com
  • Re: Cannot insert the value NULL into column 'OrderID' -- BUT IT IS NOT NULL!

    04-03-2007, 7:28 AM
    • All-Star
      61,491 point All-Star
    • TATWORTH
    • Member since 02-04-2003, 8:34 AM
    • England
    • Posts 12,064
    • TrustedFriends-MVPs
    It looks like the save method is down yet another level. Search on item.Save in the class you just posted, highlight where the Save, right-click and goto to the definition. I need to see the save and the stored procedure it calls.
    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
  • Re: Cannot insert the value NULL into column 'OrderID' -- BUT IT IS NOT NULL!

    04-03-2007, 8:44 PM
    • Member
      140 point Member
    • madkidd
    • Member since 08-19-2005, 2:37 AM
    • Charlotte, NC
    • Posts 40

    Hopefully this has what you are looking for:

    1    using System;
    2    using System.Data;
    3    using SubSonic.Utilities;
    4    using System.Text;
    5    namespace SubSonic
    6    {
    7    
    8        public abstract class ActiveRecord : AbstractRecord where T : AbstractRecord, new()
    9        {
    10           public ActiveRecord()
    11           {
    12               MarkNew();
    13           }
    14           
    15           #region CommandMethods
    16   
    17           /// <summary>
    18           /// Made Public for use with transactions
    19           /// </summary>
    20           /// <param name="userName"></param>
    21           /// <returns></returns>
    22           public QueryCommand GetInsertCommand(string userName)
    23           {
    24               Query q = new Query(table);
    25               q.QueryType = QueryType.Insert;
    26               QueryCommand cmd = new QueryCommand(DataService.GetSql(q));
    27   
    28               //loop the Columns and addin the params
    29   
    30               foreach (TableSchema.TableColumn column in table.Columns)
    31               {
    32                   if (!column.AutoIncrement)
    33                   {
    34                       object oVal;
    35                       if (Utility.IsMatch(column.ColumnName, ReservedColumnName.CREATED_BY) || Utility.IsMatch(column.ColumnName, ReservedColumnName.MODIFIED_BY))
    36                       {
    37                           oVal = userName;
    38                       }
    39                       else if (Utility.IsMatch(column.ColumnName, ReservedColumnName.CREATED_ON) || Utility.IsMatch(column.ColumnName, ReservedColumnName.MODIFIED_ON))
    40                       {
    41                           oVal = DateTime.Now;
    42                       }
    43                       else
    44                       {
    45                           oVal = GetColumnValue(column.ColumnName);
    46   
    47                           //if the value is a boolean, it can be read improperly
    48                           //reset to 0 or 1
    49                           if (oVal != null)
    50                           {
    51                               if (Utility.IsMatch(oVal.ToString(), "false"))
    52                               {
    53                                   oVal = 0;
    54                               }
    55                               else if (Utility.IsMatch(oVal.ToString(), "true"))
    56                               {
    57                                   oVal = 1;
    58                               }
    59                           }
    60                       }
    61                       if (oVal == null)
    62                       {
    63                           oVal = DBNull.Value;
    64                       }
    65                       cmd.Parameters.Add("@" + column.ColumnName, oVal, column.DataType);
    66                   }
    67               }
    68               return cmd;
    69           }
    70           
    71           public QueryCommand GetUpdateCommand(string userName)
    72           {
    73               Query q = new Query(table);
    74               q.QueryType = QueryType.Update;
    75               QueryCommand cmd = new QueryCommand(DataService.GetSql(q));
    76   
    77               //loop the Columns and addin the params
    78               foreach (TableSchema.TableColumn column in table.Columns)
    79               {
    80                   object oVal;
    81                   if (Utility.IsMatch(column.ColumnName, ReservedColumnName.MODIFIED_BY))
    82                   {
    83                       oVal = userName;
    84                   }
    85                   else if (Utility.IsMatch(column.ColumnName, ReservedColumnName.MODIFIED_ON))
    86                   {
    87                       oVal = DateTime.Now;
    88                   }
    89                   else
    90                   {
    91                       oVal = GetColumnValue(column.ColumnName);
    92                   }
    93                   if (oVal == null)
    94                   {
    95                       oVal = DBNull.Value;
    96                   }
    97                   cmd.Parameters.Add("@" + column.ColumnName, oVal,column.DataType);
    98               }
    99               return cmd;
    100          }
    101          
    102          public static QueryCommand GetDeleteCommand(object keyID)
    103          {
    104              Query q = new Query(table);
    105              q.QueryType = QueryType.Delete;
    106              q.AddWhere(table.PrimaryKey.ColumnName, keyID);
    107  
    108              return DataService.BuildCommand(q);
    109          }
    110          
    111          public static QueryCommand GetDeleteCommand(string columnName, object oValue)
    112          {
    113              Query q = new Query(table);
    114              q.QueryType = QueryType.Delete;
    115              q.AddWhere(columnName, oValue);
    116  
    117              return DataService.BuildCommand(q);
    118          }
    119  
    120          #endregion
    121  
    122         
    123          #region Persistence
    124  
    125          protected virtual void PreUpdate()
    126          {
    127          }
    128  
    129          protected virtual void PostUpdate()
    130          {
    131          }
    132  
    133          /// <summary>
    134          /// Saves this object's state to the selected Database.
    135          /// </summary>
    136          public void Save()
    137          {
    138              Save(String.Empty);
    139          }
    140  
    141          /// <summary>
    142          /// Saves this object's state to the selected Database.
    143          /// </summary>
    144          /// <param name="userID"></param>
    145          public void Save(int userID)
    146          {
    147              Save(userID.ToString());
    148          }
    149  
    150          /// <summary>
    151          /// Saves this object's state to the selected Database.
    152          /// </summary>
    153          /// <param name="userID"></param>
    154          public void Save(Guid userID)
    155          {
    156              string sUserID = string.Empty;
    157              if (userID!= null)
    158                  sUserID = userID.ToString();
    159  
    160              Save(sUserID);
    161          }
    162  
    163          /// <summary>
    164          /// Saves this object's state to the selected Database.
    165          /// </summary>
    166          /// <param name="userName"></param>
    167          public void Save(string userName)
    168          {
    169              PreUpdate();
    170  
    171              QueryCommand cmd;
    172              if (IsNew)
    173              {
    174                  cmd = GetInsertCommand(userName);
    175              }
    176              else
    177              {
    178                  cmd = GetUpdateCommand(userName);
    179              }
    180  
    181              //reset the Primary Key with the id passed back by the operation
    182              object pkVal = DataService.ExecuteScalar(cmd);
    183              
    184              //set the primaryKey, only if an auto-increment
    185              //if (table.PrimaryKey.AutoIncrement)
    186              // HACK: GUID fix
    187              if (table.PrimaryKey.AutoIncrement || table.PrimaryKey.DataType == DbType.Guid)
    188              {
    189                  try {
    190                      SetPrimaryKey(pkVal);
    191                  } catch {
    192  
    193                      //this will happen if there is no PK defined on a table. Catch this and notify
    194                      throw new Exception("No Primary Key is defined for this table. A primary key is required to use SubSonic");
    195                  }
    196              }
    197              
    198              //set this object as old
    199              MarkOld();
    200              isDirty = false;
    201              PostUpdate();
    202              
    203          }
    204  
    205          /// <summary>
    206          /// If the record contains Deleted or IsDeleted flag columns, sets them to true. If not, invokes Destroy()
    207          /// </summary>
    208          /// <returns>Number of rows affected by the operation</returns>
    209          public static int Delete(object keyID) 
    210          {
    211              return DeleteByParameter(BaseSchema.PrimaryKey.ColumnName, keyID, null);
    212          }
    213  
    214          /// <summary>
    215          /// If the record contains Deleted or IsDeleted flag columns, sets them to true. If not, invokes Destroy()
    216          /// </summary>
    217          /// <param name="columnName">The name of the column that whose value will be evaluated for deletion</param>
    218          /// <param name="oValue">The value that will be compared against columnName to determine deletion</param>
    219          /// <returns>Number of rows affected by the operation</returns>
    220          public static int Delete(string columnName, object oValue)
    221          {
    222              return DeleteByParameter(columnName, oValue, null);
    223          }
    224          
    225          /// <summary>
    226          /// If the record contains Deleted or IsDeleted flag columns, sets them to true. If not, invokes Destroy()
    227          /// </summary>
    228          /// <param name="columnName">The name of the column that whose value will be evaluated for deletion</param>
    229          /// <param name="oValue">The value that will be compared against columnName to determine deletion</param>
    230          /// <param name="userName">The userName that the record will be updated with. Only relevant if the record contains Deleted or IsDeleted properties</param>
    231          /// <returns>Number of rows affected by the operation</returns>
    232          public static int Delete(string columnName, object oValue, string userName)
    233          {
    234              return DeleteByParameter(columnName, oValue, userName);
    235          }
    236  
    237          /// <summary>
    238          /// If the record contains Deleted or IsDeleted flag columns, sets them to true. If not, invokes Destroy()
    239          /// </summary>
    240          /// <param name="columnName">The name of the column that whose value will be evaluated for deletion</param>
    241          /// <param name="oValue">The value that will be compared against columnName to determine deletion</param>
    242          /// <param name="userName">The userName that the record will be updated with. Only relevant if the record contains Deleted or IsDeleted properties</param>
    243          /// <returns>Number of rows affected by the operation</returns>
    244          private static int DeleteByParameter(string columnName, object oValue, string userName)
    245          {
    246              int iOut = 0;
    247  
    248              bool containsDeleted = BaseSchema.Columns.Contains(ReservedColumnName.DELETED);
    249              bool containsIsDeleted = BaseSchema.Columns.Contains(ReservedColumnName.IS_DELETED);
    250              bool containsModifiedBy = BaseSchema.Columns.Contains(ReservedColumnName.MODIFIED_BY);
    251              bool containsModifiedOn = BaseSchema.Columns.Contains(ReservedColumnName.MODIFIED_ON);
    252              if (containsDeleted || containsIsDeleted)
    253              {
    254                  //update the column and set deleted=true;
    255                  //new T();
    256                  Query qry = new Query(BaseSchema);
    257                  if (containsDeleted)
    258                  {
    259                      qry.AddUpdateSetting(ReservedColumnName.DELETED, true);
    260                  }
    261                  
    262                  if (containsIsDeleted)
    263                  {
    264                      qry.AddUpdateSetting(ReservedColumnName.IS_DELETED, true);
    265                  }
    266  
    267                  if (containsModifiedBy && !String.IsNullOrEmpty(userName))
    268                  {
    269                      qry.AddUpdateSetting(ReservedColumnName.MODIFIED_BY, userName);
    270                  }
    271  
    272                  if (containsModifiedOn)
    273                  {
    274                      qry.AddUpdateSetting(ReservedColumnName.MODIFIED_ON, DateTime.Now);
    275                  }
    276                  qry.AddWhere(columnName, oValue);
    277                  qry.Execute();
    278              }
    279              else
    280              {
    281                  iOut = DestroyByParameter(columnName, oValue);
    282              }
    283              return iOut;
    284          }
    285  
    286          /// <summary>
    287          /// Deletes the record in the table, even if it contains Deleted or IsDeleted flag columns
    288          /// </summary>
    289          /// <returns>Number of rows affected by the operation</returns>
    290          public static int Destroy(object keyID)
    291          {
    292              return DestroyByParameter(BaseSchema.PrimaryKey.ColumnName, keyID);
    293          }
    294  
    295          /// <summary>
    296          /// Deletes the record in the table, even if it contains Deleted or IsDeleted flag columns
    297          /// </summary>
    298          /// <param name="columnName">The name of the column that whose value will be evaluated for deletion</param>
    299          /// <param name="oValue">The value that will be compared against columnName to determine deletion</param>
    300          /// <returns>Number of rows affected by the operation</returns>
    301          public static int Destroy(string columnName, object oValue)
    302          {
    303              return DestroyByParameter(columnName, oValue);
    304          }
    305  
    306          /// <summary>
    307          /// Deletes the record in the table, even if it contains Deleted or IsDeleted flag columns
    308          /// </summary>
    309          /// <param name="columnName">The name of the column that whose value will be evaluated for deletion</param>
    310          /// <param name="oValue">The value that will be compared against columnName to determine deletion</param>
    311          /// <returns>Number of rows affected by the operation</returns>
    312          private static int DestroyByParameter(string columnName, object oValue)
    313          {
    314              QueryCommand cmd = GetDeleteCommand(columnName, oValue);
    315              return DataService.ExecuteQuery(cmd);
    316          }
    317          
    318          #endregion
    319  
    320          #region Object Overrides
    321          //public string Inspect() {
    322          //    return Inspect(true);
    323          //}
    324          //public string Inspect(bool useHtml) {
    325          //    System.Text.StringBuilder sb = new StringBuilder();
    326          //    string sOut = "";
    327          //    if (useHtml) {
    328          //        sb.Append("<table><tr><td colspan=2><h3>" + this.TableName + " Inspection</h3></td></tr>");
    329  
    330          //        foreach (TableSchema.TableColumn col in table.Columns) {
    331          //            sb.Append("<tr><td><b>" + col.ColumnName + "</b></td><td>" + this.GetColumnValue(col.ColumnName).ToString() + "</td></tr>");
    332  
    333          //        }
    334          //        sb.Append("</table>");
    335          //        sOut = sb.ToString();
    336          //    } else {
    337          //        sb.Append("#################" + this.TableName + " Inspection ####################\r\n");
    338  
    339          //        foreach (TableSchema.TableColumn col in table.Columns) {
    340          //            sb.Append(col.ColumnName + ": " + this.GetColumnValue(col.ColumnName).ToString() + "\r\n");
    341  
    342          //        }
    343          //        sb.Append("#############################################################################\r\n");
    344          //        sOut = sb.ToString();
    345  
    346          //    }
    347          //    return sOut;
    348          //}
    349          #endregion
    350      }
    351  }
    
     
    Best regards,

    Jeremy Hodges | Windows Server 2003 MCP
    Managed Microsoft hosting: www.orcsweb.com
  • Re: Cannot insert the value NULL into column 'OrderID' -- BUT IT IS NOT NULL!

    04-04-2007, 12:53 AM
    • Member
      706 point Member
    • busyweb
    • Member since 10-28-2002, 10:01 PM
    • Posts 121

    Hello,

    problem seems to me is OrderID, as trace said.

    is Save function destroy all properties(including orderid) of the Object? (i don't see on above code, but suspicious)

    try to this ??

    first create a new guid:

    dim orderID as guid = new system.guid.newguid()

    for others create new guid from orderID, each may need to have new guid with same value

    obj.OrderID = new guid(orderID.tobytearray)

    Not sure, but try it. (debug: and see if OrderID value stays after Save function call)

  • Re: Cannot insert the value NULL into column 'OrderID' -- BUT IT IS NOT NULL!

    04-04-2007, 1:04 AM
    • Member
      140 point Member
    • madkidd
    • Member since 08-19-2005, 2:37 AM
    • Charlotte, NC
    • Posts 40

    Same thing. And here is my updated code: 

        Protected Sub btnCheckout_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCheckout.Click
            Dim CartID As Integer = Session("CartID")
            Dim OrderID As Guid = System.Guid.NewGuid()
    
            Dim cOrder As OnlineOrder = New OnlineOrder
            cOrder.OrderID = New Guid(OrderID.ToByteArray)
            cOrder.CartID = CartID
            cOrder.Status = "PEND"
            cOrder.PromoCode = "NONE"
            cOrder.Timestamp = Date.Now
            cOrder.CardTypeID = ddCardType.SelectedValue
            cOrder.CardNumber = Outman.Utility.QuickEncrypt(CardNumber.Text.Trim)
            cOrder.CardExp = ExpirationDate.Text
            cOrder.CardVerification = Outman.Utility.QuickEncrypt(CardVerification.Text)
            cOrder.Save()
    
            Dim sContact As Contact = New Contact()
            sContact.OrderID = New Guid(OrderID.ToByteArray)
            sContact.Type = "SHIP"
            sContact.FirstName = sFirstName.Text.Trim
            sContact.LastName = sLastName.Text.Trim
            sContact.Company = sCompany.Text.Trim
            sContact.Address = sAddress.Text.Trim
            sContact.City = sCity.Text.Trim
            sContact.StateID = sState.SelectedValue
            sContact.ZipCode = sZipCode.Text.Trim
            sContact.Phone = sPhone.Text.Trim
            sContact.Email = sEmail.Text.Trim
            sContact.Save()
    
            Dim bContact As Contact = New Contact()
            bContact.OrderID = New Guid(OrderID.ToByteArray)
            bContact.Type = "BILL"
            bContact.FirstName = bFirstName.Text.Trim
            bContact.LastName = bLastName.Text.Trim
            bContact.Company = bCompany.Text.Trim
            bContact.Address = bAddress.Text.Trim
            bContact.City = bCity.Text.Trim
            bContact.StateID = bState.SelectedValue
            bContact.ZipCode = bZipCode.Text.Trim
            bContact.Phone = bPhone.Text.Trim
            bContact.Email = bEmail.Text.Trim
            bContact.Save()
    
            Dim cCart As Cart = New Cart(CartID)
            cCart.Status = "PEND"
            cCart.Save()
    
            Session.Add("CartID", Outman.ShoppingCart.Create)
    
            CartDisplay.Visible = False
            CheckoutComplete.Visible = True
    
        End Sub
     

    Best regards,

    Jeremy Hodges | Windows Server 2003 MCP
    Managed Microsoft hosting: www.orcsweb.com
  • Re: Cannot insert the value NULL into column 'OrderID' -- BUT IT IS NOT NULL!

    04-04-2007, 1:47 AM
    • Member
      706 point Member
    • busyweb
    • Member since 10-28-2002, 10:01 PM
    • Posts 121

    Hello,

    what/where is the function GetColumnValue, SetColumnValue ???

    Public Property OrderID As Guid
    152          Get
    153          Dim
    result As Object = Me.GetColumnValue("OrderID")
    154           Dim oOut As Guid=Guid.Empty 155           Try
    156  oOut= New Guid(result.ToString())
    157  Catch
    158  End Try
    159           Return
    oOut
    160 
    161          End Get
    162          Set
    163              Me
    .MarkDirty()
    164              Me.SetColumnValue("OrderID", Value)
    165          End Set
    166      End Property

  • Re: Cannot insert the value NULL into column 'OrderID' -- BUT IT IS NOT NULL!

    10-31-2007, 7:13 PM
    • Member
      204 point Member
    • Stevishere
    • Member since 05-22-2007, 1:43 PM
    • Ann Arbor, MI
    • Posts 189

    Did you ever get this worked out?

    I am having the same issue!  I must have overlooked this post before I made mine a few minutes ago.

    Did you try manually entering data through the table in SSMS?  I found it had nothing to do with classes or page code.  I cannot even enter it manually!

    PLease advise if you found an answer.

     

    Thanks!

    Steve Holzer
    Steve@eM8s.net
    www.eM8s.net

    Why don't computers do what I want them to do instead of....what I tell them to do!?!?!?

    Click "Mark as Answer" on the post that helped you to help future readers.

Page 1 of 1 (9 items)