My situation is once I press button, the code will retrieve the EquipmentBrand
and EquipmentType
from MS Access with respect to the SerialNumber
. I am able to retrieve the serial number through the class oledb
and made it auto complete. However, now I am having issue retrieving equipment brand and type. I have tried two codes but both have different errors.Here is my access screenshot: MS Access screenshot
For first error, I have changed my platform to x86.
First error: Could not find installable Isam at line connection.Open();
private void Form1_Load(object sender, EventArgs e)
{
oledb da = new oledb();
textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
textBox1.AutoCompleteCustomSource = da.Loadserialnum();
}
private void button2_Click(object sender, EventArgs e)
{
var connection = new OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0;Data Source = C:\\Users\\equipment.accdb");
var command = connection.CreateCommand();
{
command.Parameters.AddWithValue("SerialNumber", (textBox1.Text));
connection.Open(); //error here
var reader = command.ExecuteReader();
while (reader.Read())
{
textBox2.Text = reader["EquipmentBrand"].ToString();
textBox3.Text = reader["EquipmentType"].ToString();
}
}
}
For second error, Ive changed Data Source
(another way that works for other who had my 1st problem), but a new error occurs: Command text was not set for command object at line command.ExecuteReader
Hope to get some help thanks!
Since OleDbConnection.CreateCommand()
returns an instance of OleDbCommand
, you need to set CommandText
property to a query string you want to execute before opening the connection, otherwise it will throw Command text was not set for command object error. Below is an example to use CommandText
property:
private void button2_Click(object sender, EventArgs e)
{
var connection = new OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0;Data Source = C:\\Users\\equipment.accdb");
var command = connection.CreateCommand();
// query string example
command.CommandText = "SELECT * FROM TableName WHERE SerialNumber = ?";
command.Parameters.AddWithValue("SerialNumber", (textBox1.Text));
connection.Open(); // open the connection
var reader = command.ExecuteReader();
while (reader.Read())
{
textBox2.Text = reader["EquipmentBrand"].ToString();
textBox3.Text = reader["EquipmentType"].ToString();
}
}