Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to convert text file, read the text file and loaded
them to variables.

now I want to see whether record exist before write again

Is this correct way see record exit
DataRow[] foundRows;
foundRows = this.timeattDataSet.Tables["Data"].Select("Token_no= @cToken and Date = @datetime and Time = @cTime");

Ajith

(I am new to c#)

What I have tried:

Token1 = str.ToString().Substring(0, 9).Trim();
cToken2 = '5' + cToken1.PadLeft(5, '0');
cToken = cToken2.Substring(0, 6);
cDate = str.ToString().Substring(10, 10);
cTime = str.ToString().Substring(21, 2) + str.ToString().Substring(24, 2);
cIdno = str.ToString().Substring(30, 1);
datetime = Convert.ToDateTime(cDate);

DataRow[] foundRows;
foundRows = this.timeattDataSet.Tables["Data"].Select("Token_no= @cToken and Date = @datetime and Time = @cTime");
Posted
Updated 11-May-17 2:08am
Comments
Michael_Davies 11-May-17 8:13am    
You have to construct your Select SQL with the data for the parameters embedded, your existing query uses parameter names (@cToken etc.) and there is no way of passing the values for the parameters to a Table.Select. This is okay for a table in memory as it is not prone to SQL injection, never to be used for queries to SQL server by command.

Example;

.Select("Token_no='"+ cToken +"' and Date = ...etc.

Note: as above you will also have to enclose the parameter data in single quotes for string fields.

1 solution

Use LINQ:
C#
bool rowExists = timeattDataSet.Tables["Data"].AsEnumerable()
    .Any(row => string.Equals(row.Field<string>("Token_no"), cToken, StringComparison.OrdinalIgnoreCase) && row.Field<DateTime>("Date") == datetime);
 
Share this answer
 
Comments
Member 12931315 11-May-17 8:50am    
Hi Richard
Thank you very much Richard it's working
only problem is it enable to write several record in while loop and
give break error any idea why is that ?

Thanks again

Ajith
Richard Deeming 11-May-17 9:03am    
What's the error?
Member 12931315 11-May-17 9:07am    
Managed Debugging Assistant 'ContextSwitchDeadlock' : 'The CLR has been unable to transition from COM context 0x1801160 to COM context 0x18010a8 for 60 seconds. The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long running operation without pumping Windows messages. This situation generally has a negative performance impact and may even lead to the application becoming non responsive or memory usage accumulating continually over time. To avoid this problem, all single threaded apartment (STA) threads should use pumping wait primitives (such as CoWaitForMultipleHandles) and routinely pump messages during long running operations.'
Richard Deeming 11-May-17 9:08am    
That error doesn't seem to be associated with the code you've posted.
Member 12931315 11-May-17 9:13am    
Hihope error list is not that long.
without checking row exist I can write over 30000 records
with check only 109 records written and break

Thanks in advance

Ajith

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900