tags:

views:

45

answers:

2

I'm using following Code for Exporting CSV file contents to DataGridView in C# My Windows application is successfully running. but Not display even Datagridview or anything in Output...

I Don't know where is the exact problem.

My aim is to Display contents of .csv file in datagridview. i hv stored the .csv file in My C Drive.. as specified in path..

can anybody help me.. It's very very Urgent.. for my friend...

Using System.data.Odbc;

namespace finaltry { public partial class Form1 : Form {

    private void Form1_Load(object sender, System.EventArgs e)
    {


        string  ConnectionString = @"Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=c:\;Extensions=asc,csv,tab,txt";

        OdbcConnection conn = new OdbcConnection(ConnectionString);
        try
        {
            conn.Open();
            OdbcDataAdapter da = new OdbcDataAdapter("Select * FROM SharedIncidents.csv", conn);
            DataSet ds = new DataSet();
            da.Fill(ds, "SharedIncidents");
            dataGridView1.DataSource = ds.DefaultViewManager;
            conn.Close(); 

        }
        catch (System.Exception)
        {

            MessageBox.Show("error is" );



        }

    }
}

}

My application is running successfuly... bt not showing anything in datagridview.. can any1 tell me where is the exact prb.. ??

A: 

Please Try with this Sample Code,This works fine, i can be able to bind the csv content to Gridview control.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CSVSample.aspx.cs" Inherits="CSVSample" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView runat="server" ID="gv_csvupload">
    </asp:GridView>
    </div>
    </form>
</body>
</html>

CodeBehind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;
public partial class CSVSample : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "E:\\FolderName\\" + ";Extended Properties='text;HDR=Yes'";
        //sample.csv is the csv which i used for my demo
        string CommandText = "select * from Sample.csv";
        OleDbConnection conn = new OleDbConnection(ConnectionString);
        conn.Open();
        OleDbDataAdapter da=new OleDbDataAdapter(CommandText,conn);
        DataSet Sample=new DataSet();
        da.Fill(Sample);
        conn.Close();
        conn.Dispose();
        gv_csvupload.DataSource = Sample;
        gv_csvupload.DataBind();                
    }
}
Ramakrishnan
I didn't find DataBind() Method.. I'm using Visual studio 2005 C#..
A: 

As @Ramakrishnan mentioned in his sample... I think the only thing you are missing is the

dataGridView1.DataBind();

clause... That basically pushes the data into the grid for display.

DRapp