1
In my project I have a rule that is not to save and not to show in Grid duplicated data. For example, in the system I read a XML and with the exception of tags in a table in my database so that this information is shown in a Grid.
Only every time I do that reading, the data is saved twice. I mean, if I’ve read it once and it’s been saved, for example, the name 'John' in this first reading, if I read it again XML, the name is saved again, and so on, how many times I read the XML.
Then I made a logic not to show in Grid these duplicated data, which is this:
if (Convert.ToString(GetValueOrDefault(elmCertidao.Element("nome_tag_1"))) == objeto.Propriedades["Coluna1"].Valor &&
Convert.ToString(GetValueOrDefault(elmCertidao.Element("nome_tag_2"))) == objeto.Propriedades["Coluna2"].Valor &&
Convert.ToString(GetValueOrDefault(elmCertidao.Element("nome_tag_3"))) == objeto.Propriedades["Coluna3"].Valor)
continue;
That is, if there is any data in these tags who are already in the Grid, these data are not shown...
So far beauty, but it turns out that duplicated data is saved in the database table, and that’s not what I want.
I want you not to show in Grid or save duplicate data.
This question here from Sopt give me an idea of what to do. But if I put the fields as Unique, every time I read the XML, and the inconsistency is verified, will give error in SQL Server
saying that the data already exists.
Is there any way I can do this check and not show error, nor SQL Server
nor in the C#
? That is, saving only once the die ?
Why read more than once the xml?
– ramaral
Because in the
XML
may contain a tag that groups records and repeats it. Type an NF-e(Here in Brazil). It has a tag that groups several others with records, and this tag that it groups repeats several times. And if I need to read this XML more than once, I don’t want to save data that has already been saved.– Érik Thiago
To avoid repetitions use Unique. Before recording, read to check if the record already exists or record and do the catch error. If the possibility of repeats is large use the first, if small use the second.
– ramaral
Is there any way to do this via c# and without showing error ? Or only this way ?
– Érik Thiago
Both forms are via C# and neither shows the error.
– ramaral
It would have to put in an answer an example ?
– Érik Thiago
You enter the read data from xml at some Collection before recording in the bank?
– ramaral
I populate in an object that represents my table in the database, show the data in Grid and then saved in the bank.
– Érik Thiago
This object is a
List<T>
where T represents a record of the table?– ramaral
Exactly that! But in case you can do it in a generic way, I adapt my reality.
– Érik Thiago
To give you an answer I need to know which property of your class must be unique.
– ramaral
The Coluna1, Coluna2 and Coluna3 that are there in the if. That is, person’s name, father’s name and mother’s name.
– Érik Thiago
@Érikthiago, I think you should treat at the time of recording. A simple check routine to know if there is or not in the BD and then release the recording routine or not. As said the extension.
– pnet
@pnet you would have an example of code that does this kind of routine?
– Érik Thiago