-
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!
-
-
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
}
-
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);
-
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.
-
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?)
-
-
oh jesus.... now i feel like an absolute idiot..... Thanks bob