Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am using the following code to see the adapter values on console.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using Excel = Microsoft.Office.Interop.Excel;
using System.IO;
using System.Collections;

namespace ConsoleApplication36
{
    class Program
    {

        public static OleDbDataAdapter CreateCustomerAdapter(OleDbConnection connection)
        {
            OleDbDataAdapter adapter = new OleDbDataAdapter();
            OleDbCommand command;

            // Create the SelectCommand.
            command = new OleDbCommand("SELECT * FROM Customers " +
                "WHERE Country = ? AND City = ?", connection);

            command.Parameters.Add("Country", OleDbType.VarChar, 15);
            command.Parameters.Add("City", OleDbType.VarChar, 15);

            adapter.SelectCommand = command;

            // Create the InsertCommand.
            command = new OleDbCommand(
                "INSERT INTO Customers (CustomerID, CompanyName) " +
                "VALUES (?, ?)", connection);

            command.Parameters.Add(
                "CustomerID", OleDbType.Char, 5, "CustomerID");
            command.Parameters.Add(
                "CompanyName", OleDbType.VarChar, 40, "CompanyName");

            adapter.InsertCommand = command;
            return adapter;
        }


        static void Main()
        {
            var p = new Program();
            p.Bar();
        }

        void Bar()
        {
            OleDbConnection connection = null;
            //DataSet ds = new DataSet();
            OleDbDataAdapter adapter1 = new OleDbDataAdapter();

            adapter1 = CreateCustomerAdapter(connection);
            Console.WriteLine(adapter1);
            Console.ReadKey();

        }
    }
}


Output:
System.Data.Oledb.OledbDataAdapter

How can I see data inside the adapter
Posted

1 solution

Change your code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using Excel = Microsoft.Office.Interop.Excel;
using System.IO;
using System.Collections;
 
namespace ConsoleApplication36
{
    class Program
    {
 
        public static OleDbDataAdapter CreateCustomerAdapter(OleDbConnection connection)
        {
            OleDbDataAdapter adapter = new OleDbDataAdapter();
            OleDbCommand command;
 
            // Create the SelectCommand.
            command = new OleDbCommand("SELECT * FROM Customers " +
                "WHERE Country = ? AND City = ?", connection);
 
            command.Parameters.Add("Country", OleDbType.VarChar, 15);
            command.Parameters.Add("City", OleDbType.VarChar, 15);
 
            adapter.SelectCommand = command;
 
            // Create the InsertCommand.
            command = new OleDbCommand(
                "INSERT INTO Customers (CustomerID, CompanyName) " +
                "VALUES (?, ?)", connection);
 
            command.Parameters.Add(
                "CustomerID", OleDbType.Char, 5, "CustomerID");
            command.Parameters.Add(
                "CompanyName", OleDbType.VarChar, 40, "CompanyName");
 
            adapter.InsertCommand = command;
            return adapter;
        }
 

        static void Main()
        {
            var p = new Program();
            p.Bar();
        }
 
        void Bar()
        {
	    DataTable t = new DataTable();
            OleDbConnection connection = null;
            //DataSet ds = new DataSet();
            OleDbDataAdapter adapter1 = new OleDbDataAdapter();
 
            adapter1 = CreateCustomerAdapter(connection);
	    adapter1.Fill(t);
            PrintDataTable(t);
            Console.ReadKey();
 
        }
        void PrintDataTable(DataTable dt)
        {
		foreach (DataRow row in dt.Rows)
	{
     	Console.WriteLine();
     		for(int x = 0; x < dt.Columns.Count; x++)
     		{
          	Console.Write(row[x].ToString() + " | ");
     		}
	}
        }
    }
}
 
Share this answer
 
Comments
Maciej Los 29-Nov-13 2:54am    
Nice ;)
+5!
Suvabrata Roy 29-Nov-13 4:19am    
Thanks...
Member 10408451 29-Nov-13 3:05am    
Hi,
I am still getting a exception error at this line adapter1.Fill(t); like below
Fill:SelectComand.Connection property has to be initialized
Suvabrata Roy 29-Nov-13 4:15am    
Becz last one is insert Command, it should be Select
Member 10408451 29-Nov-13 7:07am    
Hi,
there are so many types of select commands in c# like

OleDbDataAdapter command = new OleDbDataAdapter("Select * From [" + listSheetNames[0] + "]", connection);

command = new OleDbCommand("SELECT * FROM Customers " +"WHERE Country = ? AND City = ?", connection);

But where they are defined???

where is customer???

How it is retrieving data from customer

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