Data from Access DB wont load into textbox - c#

I'm trying to load one string data from a local Access database, when I press a button, the data won't load and I'm getting the following error:
"An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Additional information: Data type mismatch in criteria expression."
string strProvider = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\training\Documents\Visual Studio 2013\Projects\WindowsFormsApplication1\WindowsFormsApplication1\LucruCilindrii.accdb";
OleDbConnection con = new OleDbConnection(strProvider);
con.Open();
string passingValue = "SELECT NumarDeOreLucrate FROM Ore_lucru_Cilindrii WHERE DiametruSuperior = " + textBox4.Text;
OleDbCommand cmmd = new OleDbCommand(passingValue, con);
OleDbDataReader reader1 = cmmd.ExecuteReader();
while (reader1.Read())
{
textBox1.Text = (reader1["NumarDeOreLucrate"].ToString());
}
con.Close();

Related

How to solve the connection when the login failed during C# programming in database?

I did some coding on connecting data using Azure database on Windows Form and when I tried to retrieve the data, I received the following error:
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll.
Also in addition, I received a Login failed for user ''. When I tried to retrieve the data and located at myConnection.Open();
private void btnRetrieve_Click(object sender, EventArgs e)
{
//Create a connection calling the App.config
string conn = ConfigurationManager.ConnectionStrings["NYPConnection"].ConnectionString;
//The query to use
string query = "SELECT * FROM Users";
SqlConnection myconnection = new SqlConnection(conn);
//Create a Data Adapter
SqlDataAdapter dadapter = new SqlDataAdapter(query, myconnection);
//Create the dataset
DataSet ds = new DataSet();
//Open the connection
******myconnection.Open();******
//Fill the Data Adapter
dadapter.Fill(ds, "Users");
myconnection.Close();
//Bind the datagridview with the data set
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "Users";
}
Print out the value of conn, using the following code:
string conn = ConfigurationManager.ConnectionStrings["NYPConnection"].ConnectionString;
Debug.WriteLine("conn= " + conn);
Have a look in the output window and you will probably find that conn is set to an empty string or maybe does not have the user name specified.

C# Query: 'System.InvalidOperationException' | Additional information: Connection must be valid and open

connectionString = #"server=localhost;database=" + lbDatabase.SelectedItem.ToString() + ";userid=root;password=;";
string query = "SELECT order_id FROM orders WHERE id = 1";
MySqlConnection connect = new MySqlConnection(connectionString);
MySqlCommand cmd = new MySqlCommand(query, connect);
reader = cmd.ExecuteReader();
Hello,
I have this problem when I run the following code. I get this not so very specific error:
An unhandled exception of type 'System.InvalidOperationException'
occurred in MySql.Data.dll
Additional information: Connection must be valid and open.
If anyone could help me out I would really appreciate it,
Thank you.
The syntax for MySql Connectionstring is as follows:
Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;
Note that you are using 'userid' and 'password' instead of 'uid' and 'pwd'.
Assuming that your Connection String is Right .. Try This
connectionString = #"server=localhost;database=" + lbDatabase.SelectedItem.ToString() + ";userid=root;password=;";
string query = "SELECT order_id FROM orders WHERE id = 1";
MySqlConnection connect = new MySqlConnection(connectionString);
MySqlCommand cmd = new MySqlCommand(query, connect);
connect.Open();
reader = cmd.ExecuteReader();
/*Do Whatever You Want To Do Here */
connect.Close();
You Can Refer this Link for Reference.
Exception: System.InvalidOperationException Trying to validate a login information
Hope This Helps :)

SQLException unhandled

I'm not sure whats wrong with my codes. When I run it, I get the error: SqlException was unhandled by user code. An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code
Additional information: Invalid object name 'ProductionPlanFabric'.
What does this mean?
Thank you :)
private void displayPendingFabric()
{
string strConn = ConfigurationManager.ConnectionStrings
["ZZFashionIMSConnectionString"].ToString();
SqlConnection conn = new SqlConnection(strConn);
SqlCommand cmd = new SqlCommand("SELECT ProductionPlanID, FashionStyleID, FabricID, WarehouseID, PPStatus, PPFabricReqd, PPFabricIssued FROM ProductionPlanFabric WHERE PPStatus = 0 OR PPStatus = 2", conn);
SqlDataAdapter daPPFabric = new SqlDataAdapter(cmd);
DataSet result = new DataSet();
conn.Open();
daPPFabric.Fill(result, "ProductionPlanFabric");
DataView dvPPFabric = result.Tables["ProductionPlanFabric"].DefaultView;
conn.Close();
gvPPFabric.DataSource = dvPPFabric;
gvPPFabric.DataBind();
}
Try this code:
conn.Open();
daPPFabric.Fill(result);
DataView dvPPFabric = result.Tables[0].DefaultView;
conn.Close();

Import Excel to Datagridview -

I want to Import Excel to Datagridview and after search found this: Import Excel to Datagridview
stirng file = "c:\myFile.xlsx";
DataGridView dgvIM;
private void Import()
{
String name = "Items";
String constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
file +
";Extended Properties='Excel 8.0;HDR=YES;';";
OleDbConnection con = new OleDbConnection(constr);
OleDbCommand oconn = new OleDbCommand("Select * From [" + name + "$]", con);
con.Open();
OleDbDataAdapter sda = new OleDbDataAdapter(oconn);
System.Data.DataTable data = new System.Data.DataTable();
sda.Fill(data);
dgvIM.DataSource = data;
}
And received this error:
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll Additional information: 'Items$' is not a valid name. Make sure that it does not include invalid characters or punctuation and that it is not too long.
What does it mean ?

unhandled exception Reading data from Any Excel

how can I go around this error
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Additional information: The Microsoft Jet database engine could not find the object Sheet1. Make sure the object exists and that you spell its name and the path name correctly"
on this code
using System.Data.OleDb;
String sConnectionString ="Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source= Book1;" + "Extended Properties=Excel 8.0;";
OleDbConnection objConn = new OleDbConnection(sConnectionString);
objConn.Open();
OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [Sheet1]",objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
objAdapter1.SelectCommand = objCmdSelect;
DataSet objDataset1 = new DataSet();
objAdapter1.Fill(objDataset1);
objConn.Close();
Try to use [Sheet1$] instead of [Sheet1].

Categories

Resources