i can't find the code to read for particular info from textfile. is that even possible?

eg. [data store in text file]
01/11/2009 12:30:22,1
01/11/2009 12:35:22,2
01/11/2009 12:55:22,1
01/11/2009 13:30:22,0
01/11/2009 13:59:22,1
01/11/2009 14:30:22,0
02/11/2009 15:30:22,1
02/11/2009 15:45:22,0
02/11/2009 16:30:22,1
02/11/2009 16:55:22,0

and i'm calling the date 01/11/2009

so the system will read all data in 01/11/2009
how do i go about doing that and than after read i need to display the data on to the grid
with separation of
date one column time one column

Dani AI

Generated

A simple, robust option is to stream the file, parse each CSV line into a timestamp and value, filter by the date portion, then load matching rows into a DataTable and bind that to your grid. That keeps memory use low for moderate files and avoids the extra step of importing to SQL unless you need query/aggregation scale. 's in-memory lookup idea is workable for small files; 's bulk-import approach is the right scale-up when the file is large or you need ad-hoc queries.

using System;
using System.Data;
using System.Globalization;
using System.IO;

public DataTable LoadForDate(string filePath, DateTime targetDate)
{
    var table = new DataTable();
    table.Columns.Add("Date", typeof(DateTime));
    table.Columns.Add("Time", typeof(string));
    table.Columns.Add("Value", typeof(int));

    const string fmt = "dd/MM/yyyy HH:mm:ss";
    foreach (var line in File.ReadLines(filePath))
    {
        if (string.IsNullOrWhiteSpace(line)) continue;
        var parts = line.Split(new[] {','}, 2);
        if (parts.Length < 2) continue;
        if (!DateTime.TryParseExact(parts[0].Trim(), fmt, CultureInfo.InvariantCulture,
                                   DateTimeStyles.None, out DateTime ts)) continue;
        if (ts.Date != targetDate.Date) continue;
        int.TryParse(parts[1].Trim(), out int val);
        table.Rows.Add(ts.Date, ts.ToString("HH:mm:ss"), val);
    }
    return table;
}

Troubleshooting: when parsing fails, check the exact input format and culture (use an array of formats with TryParseExact if inputs vary). In ASP.NET use Server.MapPath or App_Data and ensure file read permissions; use FileShare if another process writes the file. For very large files, load into a database and filter with SQL for best performance. See File.ReadLines and DateTime.TryParseExact for details: File.ReadLines DateTime.TryParseExact.

Recommended Answers

All 3 Replies

hope to find solution as soon as possible

Try something like "Read the textfile and push into some hashtable and search using the datekey".

1.Save ur test file in to the local hard disk whether d: or e:

2.Using this code and execute in the Sql server 2005

create table #testDate(myDate varchar(50),MyNo int)

Bulk insert #testDate from 'd:\testdate.txt' WITH
(
FIELDTERMINATOR =',',
ROWTERMINATOR ='\n'
)

select substring(mydate,0,11) MyDate,myno from #testdate

3. Now u can get wat u want...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.