I've tried to establish a connection between my MS Access database and my c# application and I am not understanding why I am getting this error when the filename is correct.
Any
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'sQTDBDataSet.tblEquipment' table. You can move, or remove it, as needed.
this.tblEquipmentTableAdapter.Fill(this.sQTDBDataSet.tblEquipment);
OleDbConnection connection = new OleDbConnection();
connection.ConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Owner\Documents\Visual Studio 2015\Projects\Application\Application\bin\Debug\SQTDB.accdb;Persist Security Info=False;";
connection.Open();
checkConnectionLabel.Text = "Connected.";
connection.Close();
}
Related
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();
}
}
I am trying to make a simple connection to oracle db with asp .net website to populate a gridview. but error is showing like this "Additional information: ORA-12154: TNS:could not resolve the connect identifier specified"
The same is working properly in vs 2008 but in vs 2015 it is giving error.Error appears at con.open();.Any Idea would be appreciated.
c#
public partial class _Default : System.Web.UI.Page
{
//connection file for data base connection
String connectionString = ConfigurationManager.ConnectionStrings["conndbprodnew"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
OracleConnection Con = new OracleConnection(connectionString);
Con.Open();
DataTable tab = new DataTable();
OracleDataAdapter da = new OracleDataAdapter("select * from Par_conn", Con);
da.Fill(tab);
GridView1.DataSource = tab;
GridView1.DataBind();
Con.Close();
Con.Dispose();
}
}
web config
<connectionStrings>
<add name="conndbprodnew" connectionString="Data Source=Dev;User ID=dew;Password=mes;Unicode=True" providerName="System.Data.OracleClient"/>
</connectionStrings>
hello am working on back up access database using C#, i have four groups which is access configuration , database selection, database backup and database restore.
so on data configuration i have data source textbox user id textbox and password textbox on database selection i have database combbox so that i can select one so i wrote this code
public partial class Form11 : Form
{
private OleDbConnection conn;
private OleDbCommand command;
private OleDbDataReader reader;
string ole = "";
string connectionString = "";
public Form11()
{
InitializeComponent();
}
private void BtnConnect_Click(object sender, EventArgs e)
{
try
{
conn.ConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;" +
#"Data Source = "+txtDataSource.Text+"; User Id="+txtUserId.Text+"; Password="+txtPassword.Text+"";
conn = new OleDbConnection(connectionString);
conn.Open();
ole = "EXEC sp_databases";
command = new OleDbCommand(ole, conn);
reader = command.ExecuteReader();
cmbDatabases.Items.Clear();
while (reader.Read())
{
cmbDatabases.Items.Add(reader[0].ToString());
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
the problem is i keep seeing object reference not set to an instance of an object, here i use access database but on SQL i didn't have such problem, please help me out with this thing.
thank you.
Conn is an object and is not instantiated yet when you are using conn.ConnectionString property
just flip the order this 2 lines
try
{
conn = new OleDbConnection();
conn.ConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;" +
#"Data Source = "+txtDataSource.Text+"; User Id="+txtUserId.Text+"; Password="+txtPassword.Text+"";
In your code, you are using your connection before instantiating your connection object. You need this first:
conn = new OleDbConnection();
The error should have referenced a line in your code that you were getting this problem - often relatively easy to track back from there to see what is null.
I am trying to export a CSV file to MySQL using C# but failing every time I do that. I am getting an error as unable to connect to any of the specified MySQL hosts. Please help me.
public partial class Form1 : Form
{
string connectionStringMySql = "Server=000.000.000.00; Database=test; Uid=test; Pwd=tester;";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string strCSVFile = "C:\\Users\\IT-Administrator\\Desktop\\Exploded.csv";
MySqlConnection conn1 = new MySqlConnection(connectionStringMysql);
string strLoadData = "LOAD DATA LOCAL INFILE '" + strCSVFile + "' INTO TABLE synctest.tickets FIELDS TERMINATED BY '|' LINES TERMINATED BY '\\n' ignore 1 lines;";
MySqlCommand cmd1 = new MySqlCommand(strLoadData, conn1);
cmd1.CommandTimeout = 6000;
cmd1.Connection = conn1;
conn1.Open();
cmd1.Prepare();
cmd1.ExecuteNonQuery();
conn1.Close();
}
}
Local infile must be on the mysql server, if it was a remote host it will fail.
on remote host directory must be writable by mysql user
I strongly recommend do not use infile bulk loads and dumps unless it was a server migration
sorry the poor english.
I am very new to C# and trying to develop a tool to re-index selected tables of a sql server database so the user don't have to go into database and run any commands.
namespace DBTool
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
public DataSet GetTableNames()
{
try
{
//Local variables
DataSet resultSet = new DataSet();
//Get the connection string from the config file
string connectionString = ConfigurationManager.ConnectionStrings["DBTool.Properties.Settings.ConStr"].ConnectionString;
//Create a new database connection
SqlConnection connection = new SqlConnection(connectionString);
//Set the stored procedure 'sproc_Get_Tables_Names' as the command to be executed
using (SqlCommand cmd = new SqlCommand("sproc_Get_Tables_Names", connection))
{
//Setup the command object
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 0;
//Open the connection
if (connection.State != ConnectionState.Open)
{
connection.Open();
}
//Execute the query and fill out the dataset
adapter.Fill(resultSet);
//Close the connection
connection.Close();
//Return the result of the stored procedure
return resultSet;
}
}
catch (Exception ex)
{
MessageBox.Show("An error occurred due to the following exception: " + ex.ToString() + ".","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
return null;
}
}
private void button1_Click(object sender, EventArgs e)
{
DataSet set = GetTableNames();
dataGridView1.DataSource = set.Tables[0];
}
private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
{
}
}
}
I ran your code against one of my databases (with a different stored proc) and it works. Are you getting any exception? Try setting timeout on the connection string to something low and running again.
The fact that the code works implies that SQL Server/Network Connectivity/Permissions are an issue, and the timeout is so long that you never get an exception back.
Some things to try:
Does the stored proc execute ok on the server?
If so, how much data is it returning? Are you returning a ridiculous
amount of data and it's taking an age?
Does the user supplied by the connection string have permissions to
execute the stored proc?
Can you make a connection to the SQL Server using SQL Management
Studio using the same credentials?