Hello
Ive downloaded this project to try to acces my Access database:
I can fill all fields and i can access my database.. and read everything.
Nice!
Next step.. implement this into my own program.
This things will work:
- access database
- read column headers
But if i try to read the data ill get this error:
Object reference not set to an instance of an object.
On this line:
MessageBox.Show(reader.GetValue(0).ToString());
My code is:
This is the original code from the original project:
Does anybody see what i am doing wrong ?
Ive downloaded this project to try to acces my Access database:
Code:
http://www.codeproject.com/cs/database/csharpaccessdb.asp
I can fill all fields and i can access my database.. and read everything.
Nice!
Next step.. implement this into my own program.
This things will work:
- access database
- read column headers
But if i try to read the data ill get this error:
Object reference not set to an instance of an object.
On this line:
MessageBox.Show(reader.GetValue(0).ToString());
My code is:
C#:
public void read(){
IniFile ini = new IniFile("test.ini");
database = ini.IniReadValue("Info", "Database");
if (dbAccess != null)
{
if (dbAccess.IsOpen == true)
{
dbAccess.Close();
}
}
dbAccess = new GenericOLEDBClass();
dbAccess.SelectCommand = "select * from Bedrijven";
dbAccess.Open(Provider, UserID, Password, database, Mode);
if (dbAccess.ExecuteCommand() == true)
{
int nCount = dbAccess.GetReader.FieldCount;
//ListViewItem lvItem = new ListViewItem();
for (int i = 0; i < nCount; i++)
{
ColumnHeader header = new ColumnHeader();
header.Width = 100;
header.Text = dbAccess.GetReader.GetName(i);
//listView1.Columns.Add(header);
/// Store the header names in a collection
//string temp = dbAccess.GetReader.GetName(i);
}
while (dbAccess.GetReader.Read() == true)
{
//lvItem = listView1.Items.Add(reader.GetValue(0).ToString());
MessageBox.Show(reader.GetValue(0).ToString());
for (int i = 1; i < nCount; i++)
{
int IID;
MessageBox.Show(dbAccess.GetReader.GetValue(1).ToString());
Int32.TryParse(dbAccess.GetReader.GetValue(0).ToString(), out IID);
cust[cust.Length + 1] = new Class_customers(IID, dbAccess.GetReader.GetValue(1).ToString(), dbAccess.GetReader.GetValue(2).ToString(), dbAccess.GetReader.GetValue(3).ToString(), dbAccess.GetReader.GetValue(4).ToString(), dbAccess.GetReader.GetValue(5).ToString(), dbAccess.GetReader.GetValue(6).ToString(), dbAccess.GetReader.GetValue(8).ToString(), dbAccess.GetReader.GetValue(8).ToString(), dbAccess.GetReader.GetValue(9).ToString(), dbAccess.GetReader.GetValue(10).ToString(), dbAccess.GetReader.GetValue(11).ToString(), dbAccess.GetReader.GetValue(12).ToString(), dbAccess.GetReader.GetValue(13).ToString(), dbAccess.GetReader.GetValue(14).ToString());
}
}
}
else
{
MessageBox.Show(dbAccess.ErrorMessage);
}
}
This is the original code from the original project:
C#:
public void DisplayList( OleDbDataReader reader )
{
listView1.Clear();
int nCount = reader.FieldCount;
/// build the headers first
for( int i=0; i<nCount; i++ )
{
ColumnHeader header = new ColumnHeader();
header.Width = 100;
header.Text = reader.GetName( i );
listView1.Columns.Add( header );
/// Store the header names in a collection
stringCol.Add( reader.GetName( i ) );
}
// now add the data
ListViewItem lvItem = new ListViewItem();
while( reader.Read() == true )
{
lvItem = listView1.Items.Add( reader.GetValue( 0 ).ToString() );
for( int i=1; i<nCount; i++ )
{
MessageBox.Show(reader.GetValue(i).ToString());
lvItem.SubItems.Add( reader.GetValue( i ).ToString() );
}
}
}
private void OnOpenDatabase(object sender, System.EventArgs e)
{
if( textBox1.Text == null )
{
MessageBox.Show( "You need to enter a database driver" );
return;
}
else
Provider = textBox1.Text;
if( textBox2.Text == null )
{
MessageBox.Show( "You need to enter a user id" );
return;
}
else
UserID = textBox2.Text;
Password = textBox3.Text;
/// the password can be blank so skip it
if( textBox4.Text == null )
{
MessageBox.Show( "You need to enter a database file" );
return;
}
else
DatabaseName = textBox4.Text;
if( dbAccess != null )
{
if( dbAccess.IsOpen == true )
{
dbAccess.Close();
}
}
dbAccess = new GenericOLEDBClass();
dbAccess.Open( Provider, UserID, Password, DatabaseName, Mode );
/// set the insert command and run it
if( SelectCommand == null )
{
MessageBox.Show( "you need to generate a select command first" );
return;
}
dbAccess.SelectCommand = SelectCommand;
if( dbAccess.ExecuteCommand() == true )
{
DisplayList( dbAccess.GetReader );
}
else
{
MessageBox.Show( dbAccess.ErrorMessage );
return;
}
}
Does anybody see what i am doing wrong ?
Last edited by a moderator: