Click here to Skip to main content
15,887,338 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello all.

I'm using the ODBC Microsoft Text Driver to load a csv file into the dataset to be displayed in the datagridview, the DGV has no columns in at present. I can load the file successfully but I encounter 2 problems:

a) The 2 columns load fine but a third 'noname' column is also loaded - why does this happen?

b) When I do an update using a DataAdapter, I get this exception:

"ERROR [HYS22] [Microsoft][ODBC Text Driver] The INSERT INTO statement contains the following unknown field name: 'NoName'. Make sure you have typed the name correctly, and try the operation again."

Does anybody know why this occurs?

Many thanks,
Charles

*** Code below ***
void RefreshData()
      {
          OdbcConnection conn = new OdbcConnection(@"Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=c:\;");

          OdbcCommand comm = new OdbcCommand("Select * FROM test.csv", conn);
          OdbcDataAdapter adapter = new OdbcDataAdapter(comm);
          DataSet ds = new DataSet();
          adapter.Fill(ds);
         // ds.Tables[0].Columns.Remove("noname");
          dataGridView1.DataSource = ds.Tables[0];
      }

      private void button1_Click(object sender, EventArgs e)
      {
          OdbcConnection conn = new OdbcConnection(@"Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=c:\;");
          string qry = "Select * FROM test.csv";
          OdbcDataAdapter da = new OdbcDataAdapter();
          da.SelectCommand = new OdbcCommand(qry, conn);

          OdbcCommandBuilder cb = new OdbcCommandBuilder(da);

          DataSet ds = new DataSet();
          da.Fill(ds);

          //ds.Tables[0].Columns.Remove("noname");

          DataTable dt = ds.Tables[0];

          // Add a row
          DataRow newRow = dt.NewRow();
          newRow[0] = this.textBox1.Text;
          newRow[1] = int.Parse(this.textBox2.Text); ;
          dt.Rows.Add(newRow);

          da.Update(ds.Tables[0]);

          conn.Close();

          this.Refresh();
*** csv Data ***
empName,salary,
charles,4324343,
andrew,31343970,
freddy,998788966,
loop,8878743,
Posted
Updated 11-May-11 22:37pm
v3

When it comes to SQL queries you should never use SELECT * FROM test.csv, rather name the columns you want to extract by name. Thus if you do this

C#
void RefreshData()
        {
            OdbcConnection conn = new OdbcConnection(@"Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=C:\;");
            //Get specific columns
            OdbcCommand comm = new OdbcCommand("Select empName, salary FROM test.csv", conn);
            OdbcDataAdapter adapter = new OdbcDataAdapter(comm);
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];
        }


then you should be OK.

Hope this helps
 
Share this answer
 
Comments
Sokka_sta 12-May-11 4:59am    
Wayne, you are the man! thank you very much.
Wayne Gaylard 12-May-11 5:14am    
Pleasure!
here is your modifed update statement

C++
da.Update(ds.Tables[0].DefaultView.ToTable(true, new string[] { "empName", "salary" }));



try with this....
 
Share this answer
 

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