I'm hoping this is a simple goof I did on my end ...
I have a table in my database set up like so:
column name: widget_guid
data type: uniqueidentifier
allow nulls: false
default value: newid()
identity: false
row guid: true
When records are created (via LINQ to SQL) that the values in this field are formatted as a GUID but contain all 0's
My assumption was that when a new record was created, that a guid would be autogenerated for that column, much like an auto-incrementing row id. Is this not true? Any help would be greatly appreciated.
Thanks.
You need to check your properties on the GUID column - what you need to make sure is:
Auto Generated Values is set to True (so you basically tell Linq-to-SQL that the database will generate the value)
Auto-Sync should be set to OnInsert so that your C# object will be populated with the new value after you've called context.SubmitChanges()
With these two settings, you should get the expected behavior: no need to set the GUID on the client side, the database will generate a new value and insert it, and you'll get it back right after the call to .SubmitChanges()
In your dbml file, set the field to nullable.
If it is set to not-nullable, LINQ does not go as far as checking that the field has a default; it simply believes the field non-nullable and will send Guid.Empty causing the 0's.
Related
I am trying to set a default value of a field in an access table in Table Design. The test I entered is:
=DLookUp([Tbl::VII::A::03::b_stkIden].[stkTersNam]","[Tbl::VII::A::03::b_stkIden]","[stkIdx]= [stkIdenOld]") where
In the different attempts the text was typed (did not work), copied and pasted (did not work). Keeps saying Default value or Validation rule is invalid.
stkIden is the source table containing the value I need
stkTersNam is the field which contains the value I need
stkIdx is the index field of my target table in which I want to insert the lookup value
stkIdenOld is the index field in the source table …stkIden.
It seems that default value attribute of tables in Access 2019 no longer accepts any references to any Table in the current database. It seems strange the Ms would remove this essential functionality. Is this just a quirk of the Jet Engine.
Please help. I am desperate. Should I consider abandoning Access for some other more user friendly database.
Should not have 'where' at end of expression. Don't put parameter within quote marks - concatenate dynamic parameter. Missing quote mark for the field argument.
=DLookUp("[stkTersNam]", "[Tbl::VII::A::03::b_stkIden]", "[stkIdx]=" & [stkIdenOld])
If stkIdx is a text field, use apostrophe delimiters.
=DLookUp("[stkTersNam]", "[Tbl::VII::A::03::b_stkIden]", "[stkIdx]='" & [stkIdenOld] & "'")
However, this won't work as a DefaultValue (won't in any version of Access) and really makes no sense to pull related info via DefaultValue property anyhow. Options:
Query joining tables.
Multi-column combobox on form.
DLookup in textbox ControlSource.
Is it possible to set the value of a static field from a query in ADODB Recordset in VB6?
For example, say I have a recordset whose source is:
SELECT col1, col2, '' AS staticCol
FROM myTable
WHERE 1 = 2
and then I add a new record by calling myRS.AddNew.
In the new record, all three fields are NULL. I can set col1 and col2 with no issues:
myRS.Fields("col1").Value = "one"
myRS.Fields("col2").Value = "two"
But when I try to set staticCol, I get an error.
myRS.Fields("staticCol").Value = "three"
-->Run-time error '-2147217887 (80040e21)':
-->Multiple-step operation generated errors. Check each status value.
myRS.Fields("staticCol").Value = ""
-->Run-time error '-2147217887 (80040e21)':
-->Multiple-step operation generated errors. Check each status value.
Is there any way to make this work? I'd prefer to keep the active connection open if possible.
Thanks in advance!
The first two fields are mapped to fields in the database. You have a table called myTable, and the fields are mapped to col1 and col2. The third value, however, is specified as an empty string. Think of it as a Const in your SQL. You cannot assign a new value to a const: it is always the same value that it was before.
Even if you could do so: which field would it be mapped to? The database can't know where you want it to go. It thinks that you're trying to insert into a field and is reporting that it cannot perform that operation for you.
My VB6 is rusty, and I no longer own a copy, but I am wondering why you are trying to add a record in that fashion. It is better practice to write an INSERT stored procedure, and call it as you would any other procedure.
I want to implement a user defined type (table) in SQL Server (2008 R2) that is passed from a VB.NET application. This will be a generic type used in place of passing comma-delimited lists (at least initially it will be exclusively ID values).
If it is decided later that we need to modify the UDT, will we also need to change everywhere in the application code that we attempt to use the UDT? Or can we get around having to do such a thing by making a new column in the UDT nullable?
The concern is that if we want to add a new column to the table we will need to go back and change all the places this UDT is used in the application, whether those places will need the new column or not.
You should be able to add columns to the UDT as long as they DO allow NULL values.
As far as the application code, it will depend on what you are calling. If you want to add data to the new column, you will need to change the code where you are inserting rows. If you are retrieving the data, you will need to change the code to get the data from the new column.
Are you asking if changing the UDT on the server side will require immediate changes to your code? You should not need to. SELECT queries in VB require you to access each column specifically after each query so additional columns should not affect the code. Same with inserting rows: if you are using cmd.CommandText = "INSERT INTO table (field1, [field2, ... ]) VALUES (value1, [value2, ...])" then each column is specifically labeled and additional columns should not affect the application unless the no not allow NULL.
This question already has answers here:
How to restart counting from 1 after erasing table in MS Access?
(6 answers)
Closed 8 years ago.
I have a INSERT INTO ... SELECT statement that copies data from one table to another.
The thing though is, the AutoNumber column value in the second table started from the last number in the first one.
Meaning the count of first table is 2000, then, the second table started from 2001.
Using an Access database, how to reset this value?
You can execute an Access DDL statement from ADO to reset the autonumber seed value. Here is an example Immediate window session:
strDdl = "ALTER TABLE Dummy ALTER COLUMN ID COUNTER(1, 1);"
CurrentProject.Connection.Execute strDdl
The statement must be executed from ADO. It will fail if you try it with DAO (such as CurrentDb.Execute strDdl), or from the Access query designer. The example succeeded because CurrentProject.Connection is an ADO object.
The two values following COUNTER are seed and increment. So if I wanted the autonumber to start from 1000 and increment by 2, I could use COUNTER(1000, 2)
If the table contains data, the seed value must be greater than the maximum stored value. If the table is empty when you execute the statement, that will not be an issue.
Looks like your only option is to move the data into a new table. The following link has some information about how to do it based on your version of access.
Note: be careful if you have relationships to other tables as those would need to be recreated.
http://support.microsoft.com/kb/812718
I ran across this little tid bit of info on how to set the value of a Microsoft Access AutoNumber Field.
Setting the value of a Microsoft Access AutoNumber Field
Using an Append Query to Set the Initial Value of a Microsoft Access AutoNumber Field:
By using an append query, you can change the starting value of an AutoNumber field in a table
to a number other than 1.
Microsoft Access always numbers AutoNumber fields beginning with the number 1.
If the table has data in it already, the starting value of the autonumber will be higher
than the highest value already in the table. You cannot manually edit an AutoNumber
field or change its starting value.
Overview: Changing Initial Value of an AutoNumber Field
In order to force Microsoft Access to number an AutoNumber field with a number you choose,
follow these general steps below:
For a new table that contains no records, you can change the starting value of an AutoNumber
field that has its NewValues property set to Increment to a number other than 1. For a table
that contains records, you can also use this procedure to change the next value assigned in an
AutoNumber field to a new number.
1. Create a temporary table with just one field, a Number field; set its FieldSize
property to Long Integer and give it the same name as the AutoNumber field in the table
whose value you want to change.
2. In Datasheet view, enter a value in the Number field of the temporary table that is 1
less than the starting value you want for the AutoNumber field. For example, if you want
the AutoNumber field to start at 100, enter 99 in the Number field.
3. Create and run an append query to append the temporary table to the table whose
AutoNumber value you want to change.
Note: If your original table has a primary key, you must temporarily remove the primary key
before running the append query. Also, if your original table contains fields that have the
Required property set to Yes, the Indexed property set to Yes (No Duplicates), or field and/or
record ValidationRule property settings that prevent Null entries in fields, you must
temporarily disable these settings.
4. Delete the temporary table.
5. Delete the record added by the append query.
6. If you had to disable property settings in step 3, return them to their original
settings.
When you enter a record in the remaining table, Microsoft Access uses an AutoNumber field
value 1 greater than the value you entered in the temporary table.
Note: If you want to compact the database after changing the starting AutoNumber value, make
sure to add at least one record to the table first. If you don't, when you compact the database,
the AutoNumber value for the next record added will be reset to 1 more than the highest previous
value. For example, if there were no records in the table when you reset the starting value,
compacting would set the AutoNumber value for the next record added to 1; if there were records
in the table when you reset the starting value and the highest previous value was 50, compacting
would set the AutoNumber value for the next record added to 51.
It worked for me find. Just follow the instructions to the letter. Not like me skipping around though it. I found out the hard way to do exactly as it says. I hope it'll help you out if I read your question right. I restart the autonumber field to 4556363 with a table with 8500 records in it and it didn't alter anything, just the autonumber field. I hope this ain't to late to help. Steven
When using Linq-to-SQL, adding a column to an existing table, and setting a default value on that new column, it seems that Linq to SQL ignores the default value.
Has anyone else experienced this behaviour? Is there a way to fix it, so that Linq-to-SQL or SQL Server automatically sets the default value?
The column I added was of type Bit, with a default value set to 1.
I have now definitively fixed this by using an example from this blog.
partial void OnCreated() {
if (this.DateTimeCreated == null) {
this.DateTimeCreated = DateTime.Now;
}
}
I needed to pass this into a partial datacontext class, as the default one is automatically overwritten every time you change something in the dbml.
Set Auto Generated property to True.
Go into the designer and select "Auto-Sync" value of "OnInsert". This will sync the value when the record is inserted into the database.
I've seen this. What you can do is go into the l2sql designer, view properties for the table column that has a default value. There is a property "Auto generated value", set that to true.
This same value is set to true automatically for the identity column automatically, as in that case SQL Server is generating your row IDs.
I was faced the same problem of boolean data type with default as false and not updating that value
So when i see the database table, i was found there is not primary key was set, therefore when i was set the primary key it was updated find.
so check a table if it has the primary key or not.
If you ever need to change the value in your own code (eg. a type table value with a default) then don't use 'Auto Generated Value' or you'll get this later when you try updating it.
Value of member 'AlternativeQuoteStatus' of an object of type
'OrderDetail' changed. A member that is computed or generated by the
database cannot be changed.
I prefer to set such values when I create the object, with a fallback in OnCreated as described by others here.