Monday, March 26, 2012

Validating data.

Hi everybody, this is my first post in this group.
I'm working with an Access DB and i have the following code to validate
fields with unique indexes (no duplicates):
'Field is a DataColumn
If Field.Unique Then
If Field.Table.Rows.Contains(NewData) Then
Return "Error: This field can't have duplicate values."
End If
End If
The problem that i found is that apparently the property 'Unique' works only
for Primary Keys fields.
So, how do i make a similar validation but with fields with unique indexes
only (no Primary Key)?
The other doubt that i have is that the property 'DataColumn.DefaultValue'
doesn't set itself to anything when i get the values from a field wich has a
default value.
Note: I fill a DataSet with Fill and FillSchema methods in order to do the
tests.
Thanks.> The problem that i found is that apparently the property 'Unique' works
> only
> for Primary Keys fields.
<-- Nope, UniqueConstraint doesn't have to be a primary key. Esp. in a
dataset, which is completely disconnected from the d/b, a primary key even
can be specified as UniqueConstraint in a dataset.
> The other doubt that i have is that the property 'DataColumn.DefaultValue'
> doesn't set itself to anything when i get the values from a field wich has
> a
> default value.
Fill will fill the data only, not schema. You'll have to set DefaultValue
yourself. FillSchema might or might not fill DefaultValue, and that is
specific to the .NET data provider you are using.
- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik
"Anibal" <nospam@.sinbasura.com> wrote in message
news:%23oMIb1a7EHA.3416@.TK2MSFTNGP09.phx.gbl...
> Hi everybody, this is my first post in this group.
> I'm working with an Access DB and i have the following code to validate
> fields with unique indexes (no duplicates):
> 'Field is a DataColumn
> If Field.Unique Then
> If Field.Table.Rows.Contains(NewData) Then
> Return "Error: This field can't have duplicate values."
> End If
> End If
> The problem that i found is that apparently the property 'Unique' works
> only
> for Primary Keys fields.
> So, how do i make a similar validation but with fields with unique
> indexes
> only (no Primary Key)?
> The other doubt that i have is that the property 'DataColumn.DefaultValue'
> doesn't set itself to anything when i get the values from a field wich has
> a
> default value.
> Note: I fill a DataSet with Fill and FillSchema methods in order to do the
> tests.
> Thanks.
>
>
Hello, first thanks for the answer,
Second;
> <-- Nope, UniqueConstraint doesn't have to be a primary key. Esp. in a
> dataset, which is completely disconnected from the d/b, a primary key even
> can be specified as UniqueConstraint in a dataset.
But then i don't ger it. I am using an Access database and for some "unique"
fields (indexed and without duplicates) that are not primary keys, the
'Unique' property is set to 'False' at run-time when it should be set to
'True' when using my validation code (posted in the firt message).
Why?
> FillSchema might or might not fill DefaultValue, and that is specific to
> the .NET data provider you are using.
Well, i am using a simple OleDb connection to an Access 2000 databas. So,
the 'DefaultValue' property doesn't set itself in this context?
Thank you again.
"Sahil Malik" <contactmethrumyblog@.nospam.com> escribió en el mensaje
news:%23q0LSzw7EHA.3856@.tk2msftngp13.phx.gbl...
>> The problem that i found is that apparently the property 'Unique' works
>> only
>> for Primary Keys fields.
> <-- Nope, UniqueConstraint doesn't have to be a primary key. Esp. in a
> dataset, which is completely disconnected from the d/b, a primary key even
> can be specified as UniqueConstraint in a dataset.
>> The other doubt that i have is that the property
>> 'DataColumn.DefaultValue'
>> doesn't set itself to anything when i get the values from a field wich
>> has a
>> default value.
> Fill will fill the data only, not schema. You'll have to set DefaultValue
> yourself. FillSchema might or might not fill DefaultValue, and that is
> specific to the .NET data provider you are using.
> - Sahil Malik
> http://dotnetjunkies.com/weblog/sahilmalik
>
> "Anibal" <nospam@.sinbasura.com> wrote in message
> news:%23oMIb1a7EHA.3416@.TK2MSFTNGP09.phx.gbl...
>> Hi everybody, this is my first post in this group.
>> I'm working with an Access DB and i have the following code to validate
>> fields with unique indexes (no duplicates):
>> 'Field is a DataColumn
>> If Field.Unique Then
>> If Field.Table.Rows.Contains(NewData) Then
>> Return "Error: This field can't have duplicate values."
>> End If
>> End If
>> The problem that i found is that apparently the property 'Unique' works
>> only
>> for Primary Keys fields.
>> So, how do i make a similar validation but with fields with unique
>> indexes
>> only (no Primary Key)?
>> The other doubt that i have is that the property
>> 'DataColumn.DefaultValue'
>> doesn't set itself to anything when i get the values from a field wich
>> has a
>> default value.
>> Note: I fill a DataSet with Fill and FillSchema methods in order to do
>> the
>> tests.
>> Thanks.
>>
>
Anibal,
UniqueConstraint is something that is in ADO.NET, and that has no direct
connection/relation with setting a column's Indexed property to Yes (No
Duplicates).
Picture it this way, a datatable lives under System.Data (and not under
System.Data.OleDb). You use the same System.Data.DataTable for
SqlClient/OracleClient/OleDb/anything else; It is common to all
databases/.net providers, and logically so, it has no connection with what
you might set in your access database. It is *completely* disconnected.
In other words, setting a column's indexed properties to Yes (No Duplicates)
is a completely different animal, totally disconnected, from specifying
UniqueConstraint on a DataColumn.
You might ask WHY was ADO.NET designed this way? Well because DataSets and
DataTables are supposed to work in a disconnected fashion. They might be
filled from sources other than databases. They might need to work between a
webservice and a database. You might even write a ADO.NET provider on an
MSMQ queue as demonstrated in chapter #15 of my book. Being completely
disconnected is the only way to get all this flexibility, not to mention
other benefits of complete disconenctedness like betters scalability,
maintainability, pluggability, state management etc. So disconnected is
awesome. And for the connected scenarios sans ADO 2.x, you still have
datareaders or direct execution of commands (ExecuteQuery et.al.).
- Sahil Malik
http://www.dotnetjunkies.com/weblog/sahilmalik
"Anibal" <nospam@.sinbasura.com> wrote in message
news:OSf%23TC27EHA.2788@.TK2MSFTNGP15.phx.gbl...
> Hello, first thanks for the answer,
> Second;
>> <-- Nope, UniqueConstraint doesn't have to be a primary key. Esp. in a
>> dataset, which is completely disconnected from the d/b, a primary key
>> even can be specified as UniqueConstraint in a dataset.
> But then i don't ger it. I am using an Access database and for some
> "unique" fields (indexed and without duplicates) that are not primary
> keys, the 'Unique' property is set to 'False' at run-time when it should
> be set to 'True' when using my validation code (posted in the firt
> message).
> Why?
>> FillSchema might or might not fill DefaultValue, and that is specific to
>> the .NET data provider you are using.
> Well, i am using a simple OleDb connection to an Access 2000 databas. So,
> the 'DefaultValue' property doesn't set itself in this context?
> Thank you again.
>
> "Sahil Malik" <contactmethrumyblog@.nospam.com> escribió en el mensaje
> news:%23q0LSzw7EHA.3856@.tk2msftngp13.phx.gbl...
>> The problem that i found is that apparently the property 'Unique' works
>> only
>> for Primary Keys fields.
>> <-- Nope, UniqueConstraint doesn't have to be a primary key. Esp. in a
>> dataset, which is completely disconnected from the d/b, a primary key
>> even can be specified as UniqueConstraint in a dataset.
>> The other doubt that i have is that the property
>> 'DataColumn.DefaultValue'
>> doesn't set itself to anything when i get the values from a field wich
>> has a
>> default value.
>> Fill will fill the data only, not schema. You'll have to set DefaultValue
>> yourself. FillSchema might or might not fill DefaultValue, and that is
>> specific to the .NET data provider you are using.
>> - Sahil Malik
>> http://dotnetjunkies.com/weblog/sahilmalik
>>
>> "Anibal" <nospam@.sinbasura.com> wrote in message
>> news:%23oMIb1a7EHA.3416@.TK2MSFTNGP09.phx.gbl...
>> Hi everybody, this is my first post in this group.
>> I'm working with an Access DB and i have the following code to validate
>> fields with unique indexes (no duplicates):
>> 'Field is a DataColumn
>> If Field.Unique Then
>> If Field.Table.Rows.Contains(NewData) Then
>> Return "Error: This field can't have duplicate values."
>> End If
>> End If
>> The problem that i found is that apparently the property 'Unique' works
>> only
>> for Primary Keys fields.
>> So, how do i make a similar validation but with fields with unique
>> indexes
>> only (no Primary Key)?
>> The other doubt that i have is that the property
>> 'DataColumn.DefaultValue'
>> doesn't set itself to anything when i get the values from a field wich
>> has a
>> default value.
>> Note: I fill a DataSet with Fill and FillSchema methods in order to do
>> the
>> tests.
>> Thanks.
>>
>>
>
Ok, so i will have to define and set a UniqueConstraint object and then add
it to the ConstrainsCollections of a data table for the fields that i want.
But if the 'Unique' property of DataColumn is set automaticaly to 'TRUE' at
run-time for a primary key field of a DataTable (if there is one), then
there has to be some connection/relation between what i have in the database
and what i put in a DataTable in ADO.NET.
I mean, ADO.NET knows that this field has a restriction, but i don't put
that restriction by VB.NET code, i put it in Access (in this case) and
ADO.NET is identifing this restriction as a 'UniqueContrain', if there is no
relation how do ADO.NET do this?
"Sahil Malik" <contactmethrumyblog@.nospam.com> escribió en el mensaje
news:%23nyAfE47EHA.2788@.TK2MSFTNGP15.phx.gbl...
> Anibal,
> UniqueConstraint is something that is in ADO.NET, and that has no direct
> connection/relation with setting a column's Indexed property to Yes (No
> Duplicates).
> Picture it this way, a datatable lives under System.Data (and not under
> System.Data.OleDb). You use the same System.Data.DataTable for
> SqlClient/OracleClient/OleDb/anything else; It is common to all
> databases/.net providers, and logically so, it has no connection with what
> you might set in your access database. It is *completely* disconnected.
> In other words, setting a column's indexed properties to Yes (No
> Duplicates) is a completely different animal, totally disconnected, from
> specifying UniqueConstraint on a DataColumn.
> You might ask WHY was ADO.NET designed this way? Well because DataSets and
> DataTables are supposed to work in a disconnected fashion. They might be
> filled from sources other than databases. They might need to work between
> a webservice and a database. You might even write a ADO.NET provider on an
> MSMQ queue as demonstrated in chapter #15 of my book. Being completely
> disconnected is the only way to get all this flexibility, not to mention
> other benefits of complete disconenctedness like betters scalability,
> maintainability, pluggability, state management etc. So disconnected is
> awesome. And for the connected scenarios sans ADO 2.x, you still have
> datareaders or direct execution of commands (ExecuteQuery et.al.).
> - Sahil Malik
> http://www.dotnetjunkies.com/weblog/sahilmalik
>
> "Anibal" <nospam@.sinbasura.com> wrote in message
> news:OSf%23TC27EHA.2788@.TK2MSFTNGP15.phx.gbl...
>> Hello, first thanks for the answer,
>> Second;
>> <-- Nope, UniqueConstraint doesn't have to be a primary key. Esp. in a
>> dataset, which is completely disconnected from the d/b, a primary key
>> even can be specified as UniqueConstraint in a dataset.
>> But then i don't ger it. I am using an Access database and for some
>> "unique" fields (indexed and without duplicates) that are not primary
>> keys, the 'Unique' property is set to 'False' at run-time when it should
>> be set to 'True' when using my validation code (posted in the firt
>> message).
>> Why?
>> FillSchema might or might not fill DefaultValue, and that is specific to
>> the .NET data provider you are using.
>> Well, i am using a simple OleDb connection to an Access 2000 databas. So,
>> the 'DefaultValue' property doesn't set itself in this context?
>> Thank you again.
>>
>> "Sahil Malik" <contactmethrumyblog@.nospam.com> escribió en el mensaje
>> news:%23q0LSzw7EHA.3856@.tk2msftngp13.phx.gbl...
>> The problem that i found is that apparently the property 'Unique' works
>> only
>> for Primary Keys fields.
>> <-- Nope, UniqueConstraint doesn't have to be a primary key. Esp. in a
>> dataset, which is completely disconnected from the d/b, a primary key
>> even can be specified as UniqueConstraint in a dataset.
>> The other doubt that i have is that the property
>> 'DataColumn.DefaultValue'
>> doesn't set itself to anything when i get the values from a field wich
>> has a
>> default value.
>> Fill will fill the data only, not schema. You'll have to set
>> DefaultValue yourself. FillSchema might or might not fill DefaultValue,
>> and that is specific to the .NET data provider you are using.
>> - Sahil Malik
>> http://dotnetjunkies.com/weblog/sahilmalik
>>
>> "Anibal" <nospam@.sinbasura.com> wrote in message
>> news:%23oMIb1a7EHA.3416@.TK2MSFTNGP09.phx.gbl...
>> Hi everybody, this is my first post in this group.
>> I'm working with an Access DB and i have the following code to validate
>> fields with unique indexes (no duplicates):
>> 'Field is a DataColumn
>> If Field.Unique Then
>> If Field.Table.Rows.Contains(NewData) Then
>> Return "Error: This field can't have duplicate values."
>> End If
>> End If
>> The problem that i found is that apparently the property 'Unique' works
>> only
>> for Primary Keys fields.
>> So, how do i make a similar validation but with fields with unique
>> indexes
>> only (no Primary Key)?
>> The other doubt that i have is that the property
>> 'DataColumn.DefaultValue'
>> doesn't set itself to anything when i get the values from a field wich
>> has a
>> default value.
>> Note: I fill a DataSet with Fill and FillSchema methods in order to do
>> the
>> tests.
>> Thanks.
>>
>>
>>
>
Are you using FillSchema somewhere? That might be the connection.
- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik
"Anibal" <nospam@.sinbasura.com> wrote in message
news:uAudlY87EHA.3616@.TK2MSFTNGP11.phx.gbl...
> Ok, so i will have to define and set a UniqueConstraint object and then
> add it to the ConstrainsCollections of a data table for the fields that i
> want.
> But if the 'Unique' property of DataColumn is set automaticaly to 'TRUE'
> at run-time for a primary key field of a DataTable (if there is one), then
> there has to be some connection/relation between what i have in the
> database and what i put in a DataTable in ADO.NET.
> I mean, ADO.NET knows that this field has a restriction, but i don't put
> that restriction by VB.NET code, i put it in Access (in this case) and
> ADO.NET is identifing this restriction as a 'UniqueContrain', if there is
> no relation how do ADO.NET do this?
>
> "Sahil Malik" <contactmethrumyblog@.nospam.com> escribió en el mensaje
> news:%23nyAfE47EHA.2788@.TK2MSFTNGP15.phx.gbl...
>> Anibal,
>> UniqueConstraint is something that is in ADO.NET, and that has no direct
>> connection/relation with setting a column's Indexed property to Yes (No
>> Duplicates).
>> Picture it this way, a datatable lives under System.Data (and not under
>> System.Data.OleDb). You use the same System.Data.DataTable for
>> SqlClient/OracleClient/OleDb/anything else; It is common to all
>> databases/.net providers, and logically so, it has no connection with
>> what you might set in your access database. It is *completely*
>> disconnected.
>> In other words, setting a column's indexed properties to Yes (No
>> Duplicates) is a completely different animal, totally disconnected, from
>> specifying UniqueConstraint on a DataColumn.
>> You might ask WHY was ADO.NET designed this way? Well because DataSets
>> and DataTables are supposed to work in a disconnected fashion. They might
>> be filled from sources other than databases. They might need to work
>> between a webservice and a database. You might even write a ADO.NET
>> provider on an MSMQ queue as demonstrated in chapter #15 of my book.
>> Being completely disconnected is the only way to get all this
>> flexibility, not to mention other benefits of complete disconenctedness
>> like betters scalability, maintainability, pluggability, state management
>> etc. So disconnected is awesome. And for the connected scenarios sans ADO
>> 2.x, you still have datareaders or direct execution of commands
>> (ExecuteQuery et.al.).
>> - Sahil Malik
>> http://www.dotnetjunkies.com/weblog/sahilmalik
>>
>> "Anibal" <nospam@.sinbasura.com> wrote in message
>> news:OSf%23TC27EHA.2788@.TK2MSFTNGP15.phx.gbl...
>> Hello, first thanks for the answer,
>> Second;
>> <-- Nope, UniqueConstraint doesn't have to be a primary key. Esp. in a
>> dataset, which is completely disconnected from the d/b, a primary key
>> even can be specified as UniqueConstraint in a dataset.
>> But then i don't ger it. I am using an Access database and for some
>> "unique" fields (indexed and without duplicates) that are not primary
>> keys, the 'Unique' property is set to 'False' at run-time when it should
>> be set to 'True' when using my validation code (posted in the firt
>> message).
>> Why?
>> FillSchema might or might not fill DefaultValue, and that is specific
>> to the .NET data provider you are using.
>> Well, i am using a simple OleDb connection to an Access 2000 databas.
>> So, the 'DefaultValue' property doesn't set itself in this context?
>> Thank you again.
>>
>> "Sahil Malik" <contactmethrumyblog@.nospam.com> escribió en el mensaje
>> news:%23q0LSzw7EHA.3856@.tk2msftngp13.phx.gbl...
>> The problem that i found is that apparently the property 'Unique'
>> works only
>> for Primary Keys fields.
>> <-- Nope, UniqueConstraint doesn't have to be a primary key. Esp. in a
>> dataset, which is completely disconnected from the d/b, a primary key
>> even can be specified as UniqueConstraint in a dataset.
>> The other doubt that i have is that the property
>> 'DataColumn.DefaultValue'
>> doesn't set itself to anything when i get the values from a field wich
>> has a
>> default value.
>> Fill will fill the data only, not schema. You'll have to set
>> DefaultValue yourself. FillSchema might or might not fill DefaultValue,
>> and that is specific to the .NET data provider you are using.
>> - Sahil Malik
>> http://dotnetjunkies.com/weblog/sahilmalik
>>
>> "Anibal" <nospam@.sinbasura.com> wrote in message
>> news:%23oMIb1a7EHA.3416@.TK2MSFTNGP09.phx.gbl...
>> Hi everybody, this is my first post in this group.
>> I'm working with an Access DB and i have the following code to
>> validate
>> fields with unique indexes (no duplicates):
>> 'Field is a DataColumn
>> If Field.Unique Then
>> If Field.Table.Rows.Contains(NewData) Then
>> Return "Error: This field can't have duplicate values."
>> End If
>> End If
>> The problem that i found is that apparently the property 'Unique'
>> works only
>> for Primary Keys fields.
>> So, how do i make a similar validation but with fields with unique
>> indexes
>> only (no Primary Key)?
>> The other doubt that i have is that the property
>> 'DataColumn.DefaultValue'
>> doesn't set itself to anything when i get the values from a field wich
>> has a
>> default value.
>> Note: I fill a DataSet with Fill and FillSchema methods in order to do
>> the
>> tests.
>> Thanks.
>>
>>
>>
>>
>
Yes, I am using FillSchema.
"Sahil Malik" <contactmethrumyblog@.nospam.com> escribió en el mensaje
news:utdzZU%237EHA.2124@.TK2MSFTNGP15.phx.gbl...
> Are you using FillSchema somewhere? That might be the connection.
> - Sahil Malik
> http://dotnetjunkies.com/weblog/sahilmalik
>
> "Anibal" <nospam@.sinbasura.com> wrote in message
> news:uAudlY87EHA.3616@.TK2MSFTNGP11.phx.gbl...
>> Ok, so i will have to define and set a UniqueConstraint object and then
>> add it to the ConstrainsCollections of a data table for the fields that i
>> want.
>> But if the 'Unique' property of DataColumn is set automaticaly to 'TRUE'
>> at run-time for a primary key field of a DataTable (if there is one),
>> then there has to be some connection/relation between what i have in the
>> database and what i put in a DataTable in ADO.NET.
>> I mean, ADO.NET knows that this field has a restriction, but i don't put
>> that restriction by VB.NET code, i put it in Access (in this case) and
>> ADO.NET is identifing this restriction as a 'UniqueContrain', if there is
>> no relation how do ADO.NET do this?
>>
>> "Sahil Malik" <contactmethrumyblog@.nospam.com> escribió en el mensaje
>> news:%23nyAfE47EHA.2788@.TK2MSFTNGP15.phx.gbl...
>> Anibal,
>> UniqueConstraint is something that is in ADO.NET, and that has no direct
>> connection/relation with setting a column's Indexed property to Yes (No
>> Duplicates).
>> Picture it this way, a datatable lives under System.Data (and not under
>> System.Data.OleDb). You use the same System.Data.DataTable for
>> SqlClient/OracleClient/OleDb/anything else; It is common to all
>> databases/.net providers, and logically so, it has no connection with
>> what you might set in your access database. It is *completely*
>> disconnected.
>> In other words, setting a column's indexed properties to Yes (No
>> Duplicates) is a completely different animal, totally disconnected, from
>> specifying UniqueConstraint on a DataColumn.
>> You might ask WHY was ADO.NET designed this way? Well because DataSets
>> and DataTables are supposed to work in a disconnected fashion. They
>> might be filled from sources other than databases. They might need to
>> work between a webservice and a database. You might even write a ADO.NET
>> provider on an MSMQ queue as demonstrated in chapter #15 of my book.
>> Being completely disconnected is the only way to get all this
>> flexibility, not to mention other benefits of complete disconenctedness
>> like betters scalability, maintainability, pluggability, state
>> management etc. So disconnected is awesome. And for the connected
>> scenarios sans ADO 2.x, you still have datareaders or direct execution
>> of commands (ExecuteQuery et.al.).
>> - Sahil Malik
>> http://www.dotnetjunkies.com/weblog/sahilmalik
>>
>> "Anibal" <nospam@.sinbasura.com> wrote in message
>> news:OSf%23TC27EHA.2788@.TK2MSFTNGP15.phx.gbl...
>> Hello, first thanks for the answer,
>> Second;
>> <-- Nope, UniqueConstraint doesn't have to be a primary key. Esp. in
>> a dataset, which is completely disconnected from the d/b, a primary
>> key even can be specified as UniqueConstraint in a dataset.
>> But then i don't ger it. I am using an Access database and for some
>> "unique" fields (indexed and without duplicates) that are not primary
>> keys, the 'Unique' property is set to 'False' at run-time when it
>> should be set to 'True' when using my validation code (posted in the
>> firt message).
>> Why?
>> FillSchema might or might not fill DefaultValue, and that is specific
>> to the .NET data provider you are using.
>> Well, i am using a simple OleDb connection to an Access 2000 databas.
>> So, the 'DefaultValue' property doesn't set itself in this context?
>> Thank you again.
>>
>> "Sahil Malik" <contactmethrumyblog@.nospam.com> escribió en el mensaje
>> news:%23q0LSzw7EHA.3856@.tk2msftngp13.phx.gbl...
>> The problem that i found is that apparently the property 'Unique'
>> works only
>> for Primary Keys fields.
>> <-- Nope, UniqueConstraint doesn't have to be a primary key. Esp. in
>> a dataset, which is completely disconnected from the d/b, a primary
>> key even can be specified as UniqueConstraint in a dataset.
>> The other doubt that i have is that the property
>> 'DataColumn.DefaultValue'
>> doesn't set itself to anything when i get the values from a field
>> wich has a
>> default value.
>> Fill will fill the data only, not schema. You'll have to set
>> DefaultValue yourself. FillSchema might or might not fill
>> DefaultValue, and that is specific to the .NET data provider you are
>> using.
>> - Sahil Malik
>> http://dotnetjunkies.com/weblog/sahilmalik
>>
>> "Anibal" <nospam@.sinbasura.com> wrote in message
>> news:%23oMIb1a7EHA.3416@.TK2MSFTNGP09.phx.gbl...
>> Hi everybody, this is my first post in this group.
>> I'm working with an Access DB and i have the following code to
>> validate
>> fields with unique indexes (no duplicates):
>> 'Field is a DataColumn
>> If Field.Unique Then
>> If Field.Table.Rows.Contains(NewData) Then
>> Return "Error: This field can't have duplicate values."
>> End If
>> End If
>> The problem that i found is that apparently the property 'Unique'
>> works only
>> for Primary Keys fields.
>> So, how do i make a similar validation but with fields with unique
>> indexes
>> only (no Primary Key)?
>> The other doubt that i have is that the property
>> 'DataColumn.DefaultValue'
>> doesn't set itself to anything when i get the values from a field
>> wich has a
>> default value.
>> Note: I fill a DataSet with Fill and FillSchema methods in order to
>> do the
>> tests.
>> Thanks.
>>
>>
>>
>>
>>
>
FillSchema is what is filling the UniqueConstraint per the Unique set in
your d/b.
- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik
"Anibal" <nospam@.sinbasura.com> wrote in message
news:%233PiUiD8EHA.2124@.TK2MSFTNGP15.phx.gbl...
> Yes, I am using FillSchema.
>
> "Sahil Malik" <contactmethrumyblog@.nospam.com> escribió en el mensaje
> news:utdzZU%237EHA.2124@.TK2MSFTNGP15.phx.gbl...
>> Are you using FillSchema somewhere? That might be the connection.
>> - Sahil Malik
>> http://dotnetjunkies.com/weblog/sahilmalik
>>
>> "Anibal" <nospam@.sinbasura.com> wrote in message
>> news:uAudlY87EHA.3616@.TK2MSFTNGP11.phx.gbl...
>> Ok, so i will have to define and set a UniqueConstraint object and then
>> add it to the ConstrainsCollections of a data table for the fields that
>> i want.
>> But if the 'Unique' property of DataColumn is set automaticaly to
>> 'TRUE' at run-time for a primary key field of a DataTable (if there is
>> one), then there has to be some connection/relation between what i have
>> in the database and what i put in a DataTable in ADO.NET.
>> I mean, ADO.NET knows that this field has a restriction, but i don't put
>> that restriction by VB.NET code, i put it in Access (in this case) and
>> ADO.NET is identifing this restriction as a 'UniqueContrain', if there
>> is no relation how do ADO.NET do this?
>>
>> "Sahil Malik" <contactmethrumyblog@.nospam.com> escribió en el mensaje
>> news:%23nyAfE47EHA.2788@.TK2MSFTNGP15.phx.gbl...
>> Anibal,
>> UniqueConstraint is something that is in ADO.NET, and that has no
>> direct connection/relation with setting a column's Indexed property to
>> Yes (No Duplicates).
>> Picture it this way, a datatable lives under System.Data (and not under
>> System.Data.OleDb). You use the same System.Data.DataTable for
>> SqlClient/OracleClient/OleDb/anything else; It is common to all
>> databases/.net providers, and logically so, it has no connection with
>> what you might set in your access database. It is *completely*
>> disconnected.
>> In other words, setting a column's indexed properties to Yes (No
>> Duplicates) is a completely different animal, totally disconnected,
>> from specifying UniqueConstraint on a DataColumn.
>> You might ask WHY was ADO.NET designed this way? Well because DataSets
>> and DataTables are supposed to work in a disconnected fashion. They
>> might be filled from sources other than databases. They might need to
>> work between a webservice and a database. You might even write a
>> ADO.NET provider on an MSMQ queue as demonstrated in chapter #15 of my
>> book. Being completely disconnected is the only way to get all this
>> flexibility, not to mention other benefits of complete disconenctedness
>> like betters scalability, maintainability, pluggability, state
>> management etc. So disconnected is awesome. And for the connected
>> scenarios sans ADO 2.x, you still have datareaders or direct execution
>> of commands (ExecuteQuery et.al.).
>> - Sahil Malik
>> http://www.dotnetjunkies.com/weblog/sahilmalik
>>
>> "Anibal" <nospam@.sinbasura.com> wrote in message
>> news:OSf%23TC27EHA.2788@.TK2MSFTNGP15.phx.gbl...
>> Hello, first thanks for the answer,
>> Second;
>> <-- Nope, UniqueConstraint doesn't have to be a primary key. Esp. in
>> a dataset, which is completely disconnected from the d/b, a primary
>> key even can be specified as UniqueConstraint in a dataset.
>> But then i don't ger it. I am using an Access database and for some
>> "unique" fields (indexed and without duplicates) that are not primary
>> keys, the 'Unique' property is set to 'False' at run-time when it
>> should be set to 'True' when using my validation code (posted in the
>> firt message).
>> Why?
>> FillSchema might or might not fill DefaultValue, and that is specific
>> to the .NET data provider you are using.
>> Well, i am using a simple OleDb connection to an Access 2000 databas.
>> So, the 'DefaultValue' property doesn't set itself in this context?
>> Thank you again.
>>
>> "Sahil Malik" <contactmethrumyblog@.nospam.com> escribió en el mensaje
>> news:%23q0LSzw7EHA.3856@.tk2msftngp13.phx.gbl...
>>> The problem that i found is that apparently the property 'Unique'
>>> works only
>>> for Primary Keys fields.
>> <-- Nope, UniqueConstraint doesn't have to be a primary key. Esp. in
>> a dataset, which is completely disconnected from the d/b, a primary
>> key even can be specified as UniqueConstraint in a dataset.
>>> The other doubt that i have is that the property
>>> 'DataColumn.DefaultValue'
>>> doesn't set itself to anything when i get the values from a field
>>> wich has a
>>> default value.
>> Fill will fill the data only, not schema. You'll have to set
>> DefaultValue yourself. FillSchema might or might not fill
>> DefaultValue, and that is specific to the .NET data provider you are
>> using.
>> - Sahil Malik
>> http://dotnetjunkies.com/weblog/sahilmalik
>>
>> "Anibal" <nospam@.sinbasura.com> wrote in message
>> news:%23oMIb1a7EHA.3416@.TK2MSFTNGP09.phx.gbl...
>>> Hi everybody, this is my first post in this group.
>>>
>>> I'm working with an Access DB and i have the following code to
>>> validate
>>> fields with unique indexes (no duplicates):
>>> 'Field is a DataColumn
>>> If Field.Unique Then
>>> If Field.Table.Rows.Contains(NewData) Then
>>> Return "Error: This field can't have duplicate values."
>>> End If
>>> End If
>>>
>>> The problem that i found is that apparently the property 'Unique'
>>> works only
>>> for Primary Keys fields.
>>> So, how do i make a similar validation but with fields with unique
>>> indexes
>>> only (no Primary Key)?
>>>
>>> The other doubt that i have is that the property
>>> 'DataColumn.DefaultValue'
>>> doesn't set itself to anything when i get the values from a field
>>> wich has a
>>> default value.
>>>
>>> Note: I fill a DataSet with Fill and FillSchema methods in order to
>>> do the
>>> tests.
>>>
>>> Thanks.
>>>
>>>
>>>
>>
>>
>>
>>
>>
>
Thanks for all the explications, but i still don't know how to do what i
need; wich is how to know by code if a given field has a unique index (no
duplicates) not being a primary key.
Thanks you again.
"Sahil Malik" <contactmethrumyblog@.nospam.com> escribió en el mensaje
news:%23P7TKpL8EHA.2568@.TK2MSFTNGP11.phx.gbl...
> FillSchema is what is filling the UniqueConstraint per the Unique set in
> your d/b.
> - Sahil Malik
> http://dotnetjunkies.com/weblog/sahilmalik
>
> "Anibal" <nospam@.sinbasura.com> wrote in message
> news:%233PiUiD8EHA.2124@.TK2MSFTNGP15.phx.gbl...
>> Yes, I am using FillSchema.
>>
>> "Sahil Malik" <contactmethrumyblog@.nospam.com> escribió en el mensaje
>> news:utdzZU%237EHA.2124@.TK2MSFTNGP15.phx.gbl...
>> Are you using FillSchema somewhere? That might be the connection.
>> - Sahil Malik
>> http://dotnetjunkies.com/weblog/sahilmalik
>>
>> "Anibal" <nospam@.sinbasura.com> wrote in message
>> news:uAudlY87EHA.3616@.TK2MSFTNGP11.phx.gbl...
>> Ok, so i will have to define and set a UniqueConstraint object and then
>> add it to the ConstrainsCollections of a data table for the fields that
>> i want.
>> But if the 'Unique' property of DataColumn is set automaticaly to
>> 'TRUE' at run-time for a primary key field of a DataTable (if there is
>> one), then there has to be some connection/relation between what i have
>> in the database and what i put in a DataTable in ADO.NET.
>> I mean, ADO.NET knows that this field has a restriction, but i don't
>> put that restriction by VB.NET code, i put it in Access (in this case)
>> and ADO.NET is identifing this restriction as a 'UniqueContrain', if
>> there is no relation how do ADO.NET do this?
>>
>> "Sahil Malik" <contactmethrumyblog@.nospam.com> escribió en el mensaje
>> news:%23nyAfE47EHA.2788@.TK2MSFTNGP15.phx.gbl...
>> Anibal,
>> UniqueConstraint is something that is in ADO.NET, and that has no
>> direct connection/relation with setting a column's Indexed property to
>> Yes (No Duplicates).
>> Picture it this way, a datatable lives under System.Data (and not
>> under System.Data.OleDb). You use the same System.Data.DataTable for
>> SqlClient/OracleClient/OleDb/anything else; It is common to all
>> databases/.net providers, and logically so, it has no connection with
>> what you might set in your access database. It is *completely*
>> disconnected.
>> In other words, setting a column's indexed properties to Yes (No
>> Duplicates) is a completely different animal, totally disconnected,
>> from specifying UniqueConstraint on a DataColumn.
>> You might ask WHY was ADO.NET designed this way? Well because DataSets
>> and DataTables are supposed to work in a disconnected fashion. They
>> might be filled from sources other than databases. They might need to
>> work between a webservice and a database. You might even write a
>> ADO.NET provider on an MSMQ queue as demonstrated in chapter #15 of my
>> book. Being completely disconnected is the only way to get all this
>> flexibility, not to mention other benefits of complete
>> disconenctedness like betters scalability, maintainability,
>> pluggability, state management etc. So disconnected is awesome. And
>> for the connected scenarios sans ADO 2.x, you still have datareaders
>> or direct execution of commands (ExecuteQuery et.al.).
>> - Sahil Malik
>> http://www.dotnetjunkies.com/weblog/sahilmalik
>>
>> "Anibal" <nospam@.sinbasura.com> wrote in message
>> news:OSf%23TC27EHA.2788@.TK2MSFTNGP15.phx.gbl...
>> Hello, first thanks for the answer,
>> Second;
>>> <-- Nope, UniqueConstraint doesn't have to be a primary key. Esp.
>>> in a dataset, which is completely disconnected from the d/b, a
>>> primary key even can be specified as UniqueConstraint in a dataset.
>> But then i don't ger it. I am using an Access database and for some
>> "unique" fields (indexed and without duplicates) that are not primary
>> keys, the 'Unique' property is set to 'False' at run-time when it
>> should be set to 'True' when using my validation code (posted in the
>> firt message).
>> Why?
>>> FillSchema might or might not fill DefaultValue, and that is
>>> specific to the .NET data provider you are using.
>> Well, i am using a simple OleDb connection to an Access 2000 databas.
>> So, the 'DefaultValue' property doesn't set itself in this context?
>> Thank you again.
>>
>> "Sahil Malik" <contactmethrumyblog@.nospam.com> escribió en el mensaje
>> news:%23q0LSzw7EHA.3856@.tk2msftngp13.phx.gbl...
>>> The problem that i found is that apparently the property 'Unique'
>>> works only
>>> for Primary Keys fields.
>>>
>>> <-- Nope, UniqueConstraint doesn't have to be a primary key. Esp.
>>> in a dataset, which is completely disconnected from the d/b, a
>>> primary key even can be specified as UniqueConstraint in a dataset.
>>>
>>> The other doubt that i have is that the property
>>> 'DataColumn.DefaultValue'
>>> doesn't set itself to anything when i get the values from a field
>>> wich has a
>>> default value.
>>>
>>> Fill will fill the data only, not schema. You'll have to set
>>> DefaultValue yourself. FillSchema might or might not fill
>>> DefaultValue, and that is specific to the .NET data provider you are
>>> using.
>>>
>>> - Sahil Malik
>>> http://dotnetjunkies.com/weblog/sahilmalik
>>>
>>>
>>>
>>> "Anibal" <nospam@.sinbasura.com> wrote in message
>>> news:%23oMIb1a7EHA.3416@.TK2MSFTNGP09.phx.gbl...
>>> Hi everybody, this is my first post in this group.
>>>
>>> I'm working with an Access DB and i have the following code to
>>> validate
>>> fields with unique indexes (no duplicates):
>>> 'Field is a DataColumn
>>> If Field.Unique Then
>>> If Field.Table.Rows.Contains(NewData) Then
>>> Return "Error: This field can't have duplicate
>>> values."
>>> End If
>>> End If
>>>
>>> The problem that i found is that apparently the property 'Unique'
>>> works only
>>> for Primary Keys fields.
>>> So, how do i make a similar validation but with fields with unique
>>> indexes
>>> only (no Primary Key)?
>>>
>>> The other doubt that i have is that the property
>>> 'DataColumn.DefaultValue'
>>> doesn't set itself to anything when i get the values from a field
>>> wich has a
>>> default value.
>>>
>>> Note: I fill a DataSet with Fill and FillSchema methods in order to
>>> do the
>>> tests.
>>>
>>> Thanks.
>>>
>>>
>>>
>>>
>>>
>>
>>
>>
>>
>>
>
Anibal,
Here is the code -- I tried this in .NET 2.0 and it worked fine for me
(populated the UniqueConstraint on a non primary key column with Indexed set
to Yes with no duplicates).
OleDbConnection conn= new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Documents
and Settings\\Sahil Malik\\Desktop\\db1.mdb;Persist Security Info=False") ;
DataSet ds = new DataSet() ;
OleDbCommand cmd = conn.CreateCommand() ;
cmd.CommandText = "Select * from Table1" ;
OleDbDataAdapter olda = new OleDbDataAdapter() ;
olda.SelectCommand = cmd ;
olda.FillSchema(ds, SchemaType.Mapped) ;
Console.WriteLine(ds.Tables[0].Columns[1].Unique); // Prints true
I don't have .NET 1.1 installed anywhere but the code should be the same.
I can't attach the db I used because the size limits exceed what I can post,
but the db had one table called Table1, with two columsn
One - text
Two - number - indexed with no duplis.
There were no primary keys.
- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik
"Anibal" <nospam@.sinbasura.com> wrote in message
news:%23P8v4VR8EHA.4004@.tk2msftngp13.phx.gbl...
> Thanks for all the explications, but i still don't know how to do what i
> need; wich is how to know by code if a given field has a unique index (no
> duplicates) not being a primary key.
> Thanks you again.
>
> "Sahil Malik" <contactmethrumyblog@.nospam.com> escribió en el mensaje
> news:%23P7TKpL8EHA.2568@.TK2MSFTNGP11.phx.gbl...
>> FillSchema is what is filling the UniqueConstraint per the Unique set in
>> your d/b.
>> - Sahil Malik
>> http://dotnetjunkies.com/weblog/sahilmalik
>>
>> "Anibal" <nospam@.sinbasura.com> wrote in message
>> news:%233PiUiD8EHA.2124@.TK2MSFTNGP15.phx.gbl...
>> Yes, I am using FillSchema.
>>
>> "Sahil Malik" <contactmethrumyblog@.nospam.com> escribió en el mensaje
>> news:utdzZU%237EHA.2124@.TK2MSFTNGP15.phx.gbl...
>> Are you using FillSchema somewhere? That might be the connection.
>> - Sahil Malik
>> http://dotnetjunkies.com/weblog/sahilmalik
>>
>> "Anibal" <nospam@.sinbasura.com> wrote in message
>> news:uAudlY87EHA.3616@.TK2MSFTNGP11.phx.gbl...
>> Ok, so i will have to define and set a UniqueConstraint object and
>> then
>> add it to the ConstrainsCollections of a data table for the fields
>> that
>> i want.
>> But if the 'Unique' property of DataColumn is set automaticaly to
>> 'TRUE' at run-time for a primary key field of a DataTable (if there is
>> one), then there has to be some connection/relation between what i
>> have
>> in the database and what i put in a DataTable in ADO.NET.
>> I mean, ADO.NET knows that this field has a restriction, but i don't
>> put that restriction by VB.NET code, i put it in Access (in this case)
>> and ADO.NET is identifing this restriction as a 'UniqueContrain', if
>> there is no relation how do ADO.NET do this?
>>
>> "Sahil Malik" <contactmethrumyblog@.nospam.com> escribió en el mensaje
>> news:%23nyAfE47EHA.2788@.TK2MSFTNGP15.phx.gbl...
>> Anibal,
>> UniqueConstraint is something that is in ADO.NET, and that has no
>> direct connection/relation with setting a column's Indexed property
>> to
>> Yes (No Duplicates).
>> Picture it this way, a datatable lives under System.Data (and not
>> under System.Data.OleDb). You use the same System.Data.DataTable for
>> SqlClient/OracleClient/OleDb/anything else; It is common to all
>> databases/.net providers, and logically so, it has no connection with
>> what you might set in your access database. It is *completely*
>> disconnected.
>> In other words, setting a column's indexed properties to Yes (No
>> Duplicates) is a completely different animal, totally disconnected,
>> from specifying UniqueConstraint on a DataColumn.
>> You might ask WHY was ADO.NET designed this way? Well because
>> DataSets
>> and DataTables are supposed to work in a disconnected fashion. They
>> might be filled from sources other than databases. They might need to
>> work between a webservice and a database. You might even write a
>> ADO.NET provider on an MSMQ queue as demonstrated in chapter #15 of
>> my
>> book. Being completely disconnected is the only way to get all this
>> flexibility, not to mention other benefits of complete
>> disconenctedness like betters scalability, maintainability,
>> pluggability, state management etc. So disconnected is awesome. And
>> for the connected scenarios sans ADO 2.x, you still have datareaders
>> or direct execution of commands (ExecuteQuery et.al.).
>> - Sahil Malik
>> http://www.dotnetjunkies.com/weblog/sahilmalik
>>
>> "Anibal" <nospam@.sinbasura.com> wrote in message
>> news:OSf%23TC27EHA.2788@.TK2MSFTNGP15.phx.gbl...
>>> Hello, first thanks for the answer,
>>> Second;
>>>
>>> <-- Nope, UniqueConstraint doesn't have to be a primary key. Esp.
>>> in a dataset, which is completely disconnected from the d/b, a
>>> primary key even can be specified as UniqueConstraint in a dataset.
>>>
>>> But then i don't ger it. I am using an Access database and for some
>>> "unique" fields (indexed and without duplicates) that are not
>>> primary
>>> keys, the 'Unique' property is set to 'False' at run-time when it
>>> should be set to 'True' when using my validation code (posted in the
>>> firt message).
>>> Why?
>>>
>>> FillSchema might or might not fill DefaultValue, and that is
>>> specific to the .NET data provider you are using.
>>>
>>> Well, i am using a simple OleDb connection to an Access 2000
>>> databas.
>>> So, the 'DefaultValue' property doesn't set itself in this context?
>>>
>>> Thank you again.
>>>
>>>
>>> "Sahil Malik" <contactmethrumyblog@.nospam.com> escribió en el
>>> mensaje
>>> news:%23q0LSzw7EHA.3856@.tk2msftngp13.phx.gbl...
>>> The problem that i found is that apparently the property 'Unique'
>>> works only
>>> for Primary Keys fields.
>>>
>>> <-- Nope, UniqueConstraint doesn't have to be a primary key. Esp.
>>> in a dataset, which is completely disconnected from the d/b, a
>>> primary key even can be specified as UniqueConstraint in a dataset.
>>>
>>> The other doubt that i have is that the property
>>> 'DataColumn.DefaultValue'
>>> doesn't set itself to anything when i get the values from a field
>>> wich has a
>>> default value.
>>>
>>> Fill will fill the data only, not schema. You'll have to set
>>> DefaultValue yourself. FillSchema might or might not fill
>>> DefaultValue, and that is specific to the .NET data provider you
>>> are
>>> using.
>>>
>>> - Sahil Malik
>>> http://dotnetjunkies.com/weblog/sahilmalik
>>>
>>>
>>>
>>> "Anibal" <nospam@.sinbasura.com> wrote in message
>>> news:%23oMIb1a7EHA.3416@.TK2MSFTNGP09.phx.gbl...
>>> Hi everybody, this is my first post in this group.
>>>
>>> I'm working with an Access DB and i have the following code to
>>> validate
>>> fields with unique indexes (no duplicates):
>>> 'Field is a DataColumn
>>> If Field.Unique Then
>>> If Field.Table.Rows.Contains(NewData) Then
>>> Return "Error: This field can't have duplicate
>>> values."
>>> End If
>>> End If
>>>
>>> The problem that i found is that apparently the property 'Unique'
>>> works only
>>> for Primary Keys fields.
>>> So, how do i make a similar validation but with fields with
>>> unique
>>> indexes
>>> only (no Primary Key)?
>>>
>>> The other doubt that i have is that the property
>>> 'DataColumn.DefaultValue'
>>> doesn't set itself to anything when i get the values from a field
>>> wich has a
>>> default value.
>>>
>>> Note: I fill a DataSet with Fill and FillSchema methods in order
>>> to
>>> do the
>>> tests.
>>>
>>> Thanks.
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>
>>
>>
>>
>>
>
Ok, it works,
Thanks you very much for your time.
"Sahil Malik" <contactmethrumyblog@.nospam.com> escribió en el mensaje
news:u9KALkR8EHA.2600@.TK2MSFTNGP09.phx.gbl...
> Anibal,
> Here is the code -- I tried this in .NET 2.0 and it worked fine for me
> (populated the UniqueConstraint on a non primary key column with Indexed
> set
> to Yes with no duplicates).
> OleDbConnection conn= new
> OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
> Source=C:\\Documents
> and Settings\\Sahil Malik\\Desktop\\db1.mdb;Persist Security Info=False")
> ;
> DataSet ds = new DataSet() ;
> OleDbCommand cmd = conn.CreateCommand() ;
> cmd.CommandText = "Select * from Table1" ;
> OleDbDataAdapter olda = new OleDbDataAdapter() ;
> olda.SelectCommand = cmd ;
> olda.FillSchema(ds, SchemaType.Mapped) ;
> Console.WriteLine(ds.Tables[0].Columns[1].Unique); // Prints true
> I don't have .NET 1.1 installed anywhere but the code should be the same.
> I can't attach the db I used because the size limits exceed what I can
> post, but the db had one table called Table1, with two columsn
> One - text
> Two - number - indexed with no duplis.
> There were no primary keys.
> - Sahil Malik
> http://dotnetjunkies.com/weblog/sahilmalik
>
>
> "Anibal" <nospam@.sinbasura.com> wrote in message
> news:%23P8v4VR8EHA.4004@.tk2msftngp13.phx.gbl...
>> Thanks for all the explications, but i still don't know how to do what i
>> need; wich is how to know by code if a given field has a unique index (no
>> duplicates) not being a primary key.
>> Thanks you again.
>>
>> "Sahil Malik" <contactmethrumyblog@.nospam.com> escribió en el mensaje
>> news:%23P7TKpL8EHA.2568@.TK2MSFTNGP11.phx.gbl...
>> FillSchema is what is filling the UniqueConstraint per the Unique set in
>> your d/b.
>> - Sahil Malik
>> http://dotnetjunkies.com/weblog/sahilmalik
>>
>> "Anibal" <nospam@.sinbasura.com> wrote in message
>> news:%233PiUiD8EHA.2124@.TK2MSFTNGP15.phx.gbl...
>> Yes, I am using FillSchema.
>>
>> "Sahil Malik" <contactmethrumyblog@.nospam.com> escribió en el mensaje
>> news:utdzZU%237EHA.2124@.TK2MSFTNGP15.phx.gbl...
>> Are you using FillSchema somewhere? That might be the connection.
>> - Sahil Malik
>> http://dotnetjunkies.com/weblog/sahilmalik
>>
>> "Anibal" <nospam@.sinbasura.com> wrote in message
>> news:uAudlY87EHA.3616@.TK2MSFTNGP11.phx.gbl...
>> Ok, so i will have to define and set a UniqueConstraint object and
>> then
>> add it to the ConstrainsCollections of a data table for the fields
>> that
>> i want.
>> But if the 'Unique' property of DataColumn is set automaticaly to
>> 'TRUE' at run-time for a primary key field of a DataTable (if there
>> is
>> one), then there has to be some connection/relation between what i
>> have
>> in the database and what i put in a DataTable in ADO.NET.
>> I mean, ADO.NET knows that this field has a restriction, but i don't
>> put that restriction by VB.NET code, i put it in Access (in this
>> case)
>> and ADO.NET is identifing this restriction as a 'UniqueContrain', if
>> there is no relation how do ADO.NET do this?
>>
>> "Sahil Malik" <contactmethrumyblog@.nospam.com> escribió en el mensaje
>> news:%23nyAfE47EHA.2788@.TK2MSFTNGP15.phx.gbl...
>>> Anibal,
>>>
>>> UniqueConstraint is something that is in ADO.NET, and that has no
>>> direct connection/relation with setting a column's Indexed property
>>> to
>>> Yes (No Duplicates).
>>>
>>> Picture it this way, a datatable lives under System.Data (and not
>>> under System.Data.OleDb). You use the same System.Data.DataTable for
>>> SqlClient/OracleClient/OleDb/anything else; It is common to all
>>> databases/.net providers, and logically so, it has no connection
>>> with
>>> what you might set in your access database. It is *completely*
>>> disconnected.
>>>
>>> In other words, setting a column's indexed properties to Yes (No
>>> Duplicates) is a completely different animal, totally disconnected,
>>> from specifying UniqueConstraint on a DataColumn.
>>>
>>> You might ask WHY was ADO.NET designed this way? Well because
>>> DataSets
>>> and DataTables are supposed to work in a disconnected fashion. They
>>> might be filled from sources other than databases. They might need
>>> to
>>> work between a webservice and a database. You might even write a
>>> ADO.NET provider on an MSMQ queue as demonstrated in chapter #15 of
>>> my
>>> book. Being completely disconnected is the only way to get all this
>>> flexibility, not to mention other benefits of complete
>>> disconenctedness like betters scalability, maintainability,
>>> pluggability, state management etc. So disconnected is awesome. And
>>> for the connected scenarios sans ADO 2.x, you still have datareaders
>>> or direct execution of commands (ExecuteQuery et.al.).
>>>
>>> - Sahil Malik
>>> http://www.dotnetjunkies.com/weblog/sahilmalik
>>>
>>>
>>> "Anibal" <nospam@.sinbasura.com> wrote in message
>>> news:OSf%23TC27EHA.2788@.TK2MSFTNGP15.phx.gbl...
>>> Hello, first thanks for the answer,
>>> Second;
>>>
>>> <-- Nope, UniqueConstraint doesn't have to be a primary key. Esp.
>>> in a dataset, which is completely disconnected from the d/b, a
>>> primary key even can be specified as UniqueConstraint in a
>>> dataset.
>>>
>>> But then i don't ger it. I am using an Access database and for some
>>> "unique" fields (indexed and without duplicates) that are not
>>> primary
>>> keys, the 'Unique' property is set to 'False' at run-time when it
>>> should be set to 'True' when using my validation code (posted in
>>> the
>>> firt message).
>>> Why?
>>>
>>> FillSchema might or might not fill DefaultValue, and that is
>>> specific to the .NET data provider you are using.
>>>
>>> Well, i am using a simple OleDb connection to an Access 2000
>>> databas.
>>> So, the 'DefaultValue' property doesn't set itself in this context?
>>>
>>> Thank you again.
>>>
>>>
>>> "Sahil Malik" <contactmethrumyblog@.nospam.com> escribió en el
>>> mensaje
>>> news:%23q0LSzw7EHA.3856@.tk2msftngp13.phx.gbl...
>>>> The problem that i found is that apparently the property 'Unique'
>>>> works only
>>>> for Primary Keys fields.
>>>
>>> <-- Nope, UniqueConstraint doesn't have to be a primary key. Esp.
>>> in a dataset, which is completely disconnected from the d/b, a
>>> primary key even can be specified as UniqueConstraint in a
>>> dataset.
>>>
>>>> The other doubt that i have is that the property
>>>> 'DataColumn.DefaultValue'
>>>> doesn't set itself to anything when i get the values from a field
>>>> wich has a
>>>> default value.
>>>
>>> Fill will fill the data only, not schema. You'll have to set
>>> DefaultValue yourself. FillSchema might or might not fill
>>> DefaultValue, and that is specific to the .NET data provider you
>>> are
>>> using.
>>>
>>> - Sahil Malik
>>> http://dotnetjunkies.com/weblog/sahilmalik
>>>
>>>
>>>
>>> "Anibal" <nospam@.sinbasura.com> wrote in message
>>> news:%23oMIb1a7EHA.3416@.TK2MSFTNGP09.phx.gbl...
>>>> Hi everybody, this is my first post in this group.
>>>>
>>>> I'm working with an Access DB and i have the following code to
>>>> validate
>>>> fields with unique indexes (no duplicates):
>>>> 'Field is a DataColumn
>>>> If Field.Unique Then
>>>> If Field.Table.Rows.Contains(NewData) Then
>>>> Return "Error: This field can't have duplicate
>>>> values."
>>>> End If
>>>> End If
>>>>
>>>> The problem that i found is that apparently the property 'Unique'
>>>> works only
>>>> for Primary Keys fields.
>>>> So, how do i make a similar validation but with fields with
>>>> unique
>>>> indexes
>>>> only (no Primary Key)?
>>>>
>>>> The other doubt that i have is that the property
>>>> 'DataColumn.DefaultValue'
>>>> doesn't set itself to anything when i get the values from a field
>>>> wich has a
>>>> default value.
>>>>
>>>> Note: I fill a DataSet with Fill and FillSchema methods in order
>>>> to
>>>> do the
>>>> tests.
>>>>
>>>> Thanks.
>>>>
>>>>
>>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>
>>
>>
>>
>>
>
>
WHEW !!
:-)
Anyway, the theory we discussed before this code is important too.
- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik
"Anibal" <nospam@.sinbasura.com> wrote in message
news:en2p7FT8EHA.4004@.tk2msftngp13.phx.gbl...
> Ok, it works,
> Thanks you very much for your time.
> "Sahil Malik" <contactmethrumyblog@.nospam.com> escribió en el mensaje
> news:u9KALkR8EHA.2600@.TK2MSFTNGP09.phx.gbl...
>> Anibal,
>> Here is the code -- I tried this in .NET 2.0 and it worked fine for me
>> (populated the UniqueConstraint on a non primary key column with Indexed
>> set
>> to Yes with no duplicates).
>> OleDbConnection conn= new
>> OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
>> Source=C:\\Documents
>> and Settings\\Sahil Malik\\Desktop\\db1.mdb;Persist Security Info=False")
>> ;
>> DataSet ds = new DataSet() ;
>> OleDbCommand cmd = conn.CreateCommand() ;
>> cmd.CommandText = "Select * from Table1" ;
>> OleDbDataAdapter olda = new OleDbDataAdapter() ;
>> olda.SelectCommand = cmd ;
>> olda.FillSchema(ds, SchemaType.Mapped) ;
>> Console.WriteLine(ds.Tables[0].Columns[1].Unique); // Prints true
>> I don't have .NET 1.1 installed anywhere but the code should be the same.
>> I can't attach the db I used because the size limits exceed what I can
>> post, but the db had one table called Table1, with two columsn
>> One - text
>> Two - number - indexed with no duplis.
>> There were no primary keys.
>> - Sahil Malik
>> http://dotnetjunkies.com/weblog/sahilmalik
>>
>>
>> "Anibal" <nospam@.sinbasura.com> wrote in message
>> news:%23P8v4VR8EHA.4004@.tk2msftngp13.phx.gbl...
>> Thanks for all the explications, but i still don't know how to do what i
>> need; wich is how to know by code if a given field has a unique index
>> (no
>> duplicates) not being a primary key.
>> Thanks you again.
>>
>> "Sahil Malik" <contactmethrumyblog@.nospam.com> escribió en el mensaje
>> news:%23P7TKpL8EHA.2568@.TK2MSFTNGP11.phx.gbl...
>> FillSchema is what is filling the UniqueConstraint per the Unique set
>> in
>> your d/b.
>> - Sahil Malik
>> http://dotnetjunkies.com/weblog/sahilmalik
>>
>> "Anibal" <nospam@.sinbasura.com> wrote in message
>> news:%233PiUiD8EHA.2124@.TK2MSFTNGP15.phx.gbl...
>> Yes, I am using FillSchema.
>>
>> "Sahil Malik" <contactmethrumyblog@.nospam.com> escribió en el mensaje
>> news:utdzZU%237EHA.2124@.TK2MSFTNGP15.phx.gbl...
>> Are you using FillSchema somewhere? That might be the connection.
>> - Sahil Malik
>> http://dotnetjunkies.com/weblog/sahilmalik
>>
>> "Anibal" <nospam@.sinbasura.com> wrote in message
>> news:uAudlY87EHA.3616@.TK2MSFTNGP11.phx.gbl...
>>> Ok, so i will have to define and set a UniqueConstraint object and
>>> then
>>> add it to the ConstrainsCollections of a data table for the fields
>>> that
>>> i want.
>>> But if the 'Unique' property of DataColumn is set automaticaly to
>>> 'TRUE' at run-time for a primary key field of a DataTable (if there
>>> is
>>> one), then there has to be some connection/relation between what i
>>> have
>>> in the database and what i put in a DataTable in ADO.NET.
>>> I mean, ADO.NET knows that this field has a restriction, but i don't
>>> put that restriction by VB.NET code, i put it in Access (in this
>>> case)
>>> and ADO.NET is identifing this restriction as a 'UniqueContrain', if
>>> there is no relation how do ADO.NET do this?
>>>
>>>
>>> "Sahil Malik" <contactmethrumyblog@.nospam.com> escribió en el
>>> mensaje
>>> news:%23nyAfE47EHA.2788@.TK2MSFTNGP15.phx.gbl...
>>> Anibal,
>>>
>>> UniqueConstraint is something that is in ADO.NET, and that has no
>>> direct connection/relation with setting a column's Indexed property
>>> to
>>> Yes (No Duplicates).
>>>
>>> Picture it this way, a datatable lives under System.Data (and not
>>> under System.Data.OleDb). You use the same System.Data.DataTable
>>> for
>>> SqlClient/OracleClient/OleDb/anything else; It is common to all
>>> databases/.net providers, and logically so, it has no connection
>>> with
>>> what you might set in your access database. It is *completely*
>>> disconnected.
>>>
>>> In other words, setting a column's indexed properties to Yes (No
>>> Duplicates) is a completely different animal, totally disconnected,
>>> from specifying UniqueConstraint on a DataColumn.
>>>
>>> You might ask WHY was ADO.NET designed this way? Well because
>>> DataSets
>>> and DataTables are supposed to work in a disconnected fashion. They
>>> might be filled from sources other than databases. They might need
>>> to
>>> work between a webservice and a database. You might even write a
>>> ADO.NET provider on an MSMQ queue as demonstrated in chapter #15 of
>>> my
>>> book. Being completely disconnected is the only way to get all this
>>> flexibility, not to mention other benefits of complete
>>> disconenctedness like betters scalability, maintainability,
>>> pluggability, state management etc. So disconnected is awesome. And
>>> for the connected scenarios sans ADO 2.x, you still have
>>> datareaders
>>> or direct execution of commands (ExecuteQuery et.al.).
>>>
>>> - Sahil Malik
>>> http://www.dotnetjunkies.com/weblog/sahilmalik
>>>
>>>
>>> "Anibal" <nospam@.sinbasura.com> wrote in message
>>> news:OSf%23TC27EHA.2788@.TK2MSFTNGP15.phx.gbl...
>>> Hello, first thanks for the answer,
>>> Second;
>>>
>>>> <-- Nope, UniqueConstraint doesn't have to be a primary key.
>>>> Esp.
>>>> in a dataset, which is completely disconnected from the d/b, a
>>>> primary key even can be specified as UniqueConstraint in a
>>>> dataset.
>>>
>>> But then i don't ger it. I am using an Access database and for
>>> some
>>> "unique" fields (indexed and without duplicates) that are not
>>> primary
>>> keys, the 'Unique' property is set to 'False' at run-time when it
>>> should be set to 'True' when using my validation code (posted in
>>> the
>>> firt message).
>>> Why?
>>>
>>>> FillSchema might or might not fill DefaultValue, and that is
>>>> specific to the .NET data provider you are using.
>>>
>>> Well, i am using a simple OleDb connection to an Access 2000
>>> databas.
>>> So, the 'DefaultValue' property doesn't set itself in this
>>> context?
>>>
>>> Thank you again.
>>>
>>>
>>> "Sahil Malik" <contactmethrumyblog@.nospam.com> escribió en el
>>> mensaje
>>> news:%23q0LSzw7EHA.3856@.tk2msftngp13.phx.gbl...
>>>> The problem that i found is that apparently the property
>>>> 'Unique'
>>>> works only
>>>> for Primary Keys fields.
>>>>
>>>> <-- Nope, UniqueConstraint doesn't have to be a primary key.
>>>> Esp.
>>>> in a dataset, which is completely disconnected from the d/b, a
>>>> primary key even can be specified as UniqueConstraint in a
>>>> dataset.
>>>>
>>>> The other doubt that i have is that the property
>>>> 'DataColumn.DefaultValue'
>>>> doesn't set itself to anything when i get the values from a
>>>> field
>>>> wich has a
>>>> default value.
>>>>
>>>> Fill will fill the data only, not schema. You'll have to set
>>>> DefaultValue yourself. FillSchema might or might not fill
>>>> DefaultValue, and that is specific to the .NET data provider you
>>>> are
>>>> using.
>>>>
>>>> - Sahil Malik
>>>> http://dotnetjunkies.com/weblog/sahilmalik
>>>>
>>>>
>>>>
>>>> "Anibal" <nospam@.sinbasura.com> wrote in message
>>>> news:%23oMIb1a7EHA.3416@.TK2MSFTNGP09.phx.gbl...
>>>> Hi everybody, this is my first post in this group.
>>>>
>>>> I'm working with an Access DB and i have the following code to
>>>> validate
>>>> fields with unique indexes (no duplicates):
>>>> 'Field is a DataColumn
>>>> If Field.Unique Then
>>>> If Field.Table.Rows.Contains(NewData) Then
>>>> Return "Error: This field can't have duplicate
>>>> values."
>>>> End If
>>>> End If
>>>>
>>>> The problem that i found is that apparently the property
>>>> 'Unique'
>>>> works only
>>>> for Primary Keys fields.
>>>> So, how do i make a similar validation but with fields with
>>>> unique
>>>> indexes
>>>> only (no Primary Key)?
>>>>
>>>> The other doubt that i have is that the property
>>>> 'DataColumn.DefaultValue'
>>>> doesn't set itself to anything when i get the values from a
>>>> field
>>>> wich has a
>>>> default value.
>>>>
>>>> Note: I fill a DataSet with Fill and FillSchema methods in order
>>>> to
>>>> do the
>>>> tests.
>>>>
>>>> Thanks.
>>>>
>>>>
>>>>
>>>>
>>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>
>>
>>
>>
>>
>

No comments:

Post a Comment