Link to home
Start Free TrialLog in
Avatar of btkrieger
btkriegerFlag for United States of America

asked on

Can you use a text file as a data source for a GridView in ASP

Hello,

I have a working ASP page that is pulling data from a SQL table into a Gridview with a select statement.  I would like to do the same thing using a text file vs the SQL table as my data source.  Is this possible to create a system DSN and connect to the text file to pull my data? If not can you point me into the right direction?

Current Web.cofig
<connectionStrings>
<add name="FBConnectionString" connectionString="Data Source=FORTISBLUE2008\SQLEXPRESS;Initial Catalog=YourLookup;User ID=blue;Password=blue;Connect Timeout=200" providerName="System.Data.SqlClient" /> --%>
</connectionStrings>

Current asp page:

 <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:FBConnectionString %>" SelectCommand="SELECT * FROM dbo.APInvoice WHERE VendorName LIKE @VName">
 <SelectParameters>
        <asp:ControlParameter ControlID="tbVName" Name="VName" PropertyName="Text" />
    </SelectParameters>
ASKER CERTIFIED SOLUTION
Avatar of Big Monty
Big Monty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
here's a more detailed answer on parsing out a text file and filling up a GridView:

Text File Contents:

1,1,4,2,"#",Description1
5,5,4,2,"#",Description2
3,3,6,3,"#",Description3
2,2,4,2,"#",Description4
4,5,4,2,"#",Description5

C# Code:

protected void readfile_Click(object sender, EventArgs e)
    {
        DataTable table = new DataTable();
        table.Columns.Add("Col1");
        table.Columns.Add("Col2");
        table.Columns.Add("Col3");
        table.Columns.Add("Col4");
        table.Columns.Add("Col5");
        table.Columns.Add("Col6");

        using (StreamReader sr = new StreamReader(@"c:\pathToFile\file.txt"))
        {
            while (!sr.EndOfStream)
            {
                string[] parts = sr.ReadLine().Split(',');
                table.Rows.Add(parts[0], parts[1], parts[2], parts[3], parts[4], parts[5]);
            }
        }
        MyGridView.DataSource = table;
        MyGridView.DataBind();
    }

Open in new window