Thread: Creating a Data Adapter

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    5

    Creating a Data Adapter

    Hey All;

    I'm new to C# and Have been working on finishing this online course I've been taking. I am wondering if you can help me figure out what is wrong with this code, and how to fix it.

    This is what I have in my coding:

    public partial class fclsMain : Form
    {
    OleDbConnection m_cnADONetConnection = new OleDbConnection();
    m_daDataAdapter DataTable m_dtContacts = new DataTable();
    int m_rowPosition = 0;

    public fclsMain()
    {


    This is what its telling me to place it as

    You're going to create a module-level DataTable in your project. First, create the DataTable variable by adding the following statement on the line below the statement you entered previously to declare a new module-level m_daDataAdapter object:

    DataTable m_dtContacts = new DataTable();

    Please help!
    Last edited by Denis; 12-11-2007 at 06:50 PM. Reason: Code tags? This is exactly how my programming language is sitting exactly in the IDE.

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Please use code tags.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Code:
    public static void Main()
    {
         OleDbConnection m_cnADONetConnection = new OleDbConnection();
    
         m_cnADONetConnection.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source= c:\\test.mdb"; 
         m_cnADONetConnection.Open();
         string sql="Select * from TestTable";
         OleDbCommand  com=new OleDbCommand(sql,m_cnADONetConnection );
         OleDataAdapter m_daDataAdapter =new OleDbDataAdapter(com);
         DataTable m_dtContacts =new DataTable();
         m_daDataAdapter.Fill(m_dtContacts);
         m_cnADONetConnection .Close();
    // Additional processing such as displaying the records
    
    }
    Last edited by BobS0327; 12-11-2007 at 08:05 PM.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    5
    Code:
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Data.OleDb;
    
    namespace Database_Example
    {
    	/// <summary>
    	/// Description of MainForm.
    	/// </summary>
    	public partial class fclsMain : Form
    	{
    		OleDbConnection m_cnADONetConnection =new OleDbConnection();
    		DataTable m_dtContacts =new DataTable();
    		int m_rowPosition = 0;
    		
    		public fclsMain()
    		{
    			//
    			// The InitializeComponent() call is required for Windows Forms designer support.
    			//
    			InitializeComponent();
    			
    			//
    			// TODO: Add constructor code after the InitializeComponent() call.
    			//
    		}
    		
    		void FclsMainLoad(object sender, EventArgs e)
    		{
    			m_cnADONetConnection.ConnectionString =
    			@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Charlynne Guay\My Documents\Guild Items.xls";
    			OleDbDataAdapter m_daDataAdapter = new OleDbDataAdapter();
    			m_daDataAdapter = new OleDbDataAdapter("Select * From Contacts",m_cnADONetConnection);
    			OleDbCommandBuilder m_cbCommandBuilder = new OleDbCommandBuilder(m_daDataAdapter);
    			m_daDataAdapter.Fill(m_dtContacts);
    		}
    			
    		void FclsMainFormClosed(object sender, FormClosedEventArgs e)
    		{
    			m_cnADONetConnection.Close();
    		}
    		}
    		}
    Ok and this is what my course is telling me to do...... but I have no idea how to fix it. I'm getting an error on the color changed section

    Creating and Populating DataTables
    You're going to create a module-level DataTable in your project. First, create the DataTable variable by adding the following statement on the line below the statement you entered previously to declare a new module-level m_daDataAdapter object:

    Code:
    DataTable m_dtContacts = new DataTable();
    You are going to use an integer variable to keep track of the user's current position within the DataTable. To do this, add the following statement immediately below the statement you just entered to declare the new DataTable object:

    Code:
    int m_rowPosition = 0;
    Next, add the following statement to the Load event of the form, immediately following the statement that creates the CommandBuilder:

    Code:
    m_daDataAdapter.Fill(m_dtContacts);

  5. #5
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Please post the desription of the error such as the error number which is usually a CS#### number and what the compiler is complaining about.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    5
    Ok won't let me edit my last post so here it is:

    The compiler and error message is saying

    The type or namespace name DataTable could not be found (are you missing a using directive or an assembly reference?)

  7. #7
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    It's missing:

    Code:
    using System.Data;

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    5
    oh jesus.... now i feel like an absolute idiot..... Thanks bob

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. Reading a file with Courier New characters
    By Noam in forum C Programming
    Replies: 3
    Last Post: 07-07-2006, 09:29 AM
  3. Replies: 4
    Last Post: 06-14-2005, 05:45 AM
  4. C diamonds and perls :°)
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-16-2003, 10:19 PM
  5. C Programming Question
    By TK in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 07-04-2002, 07:11 PM