I' using Visual Studio Enterprise 2015 WPF to do my Project and my database is a ms access file
I'm not sure why I'm having this error Can someone
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Additional information: No value given for one or more required parameters.
Here is my Code
OleDbConnection con = new OleDbConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["Connection"].ToString();
con.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "select `Name1`,`ID` from `Employee` WHERE `Name1` = Jacob ";
cmd.Connection = con;
OleDbDataReader rd = cmd.ExecuteReader();
grid1.ItemsSource = rd;
I have also tried
OleDbConnection con = new OleDbConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["Connection"].ToString();
con.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "select Name1,ID from [Employee] WHERE Name1 = Jacob ";
cmd.Connection = con;
OleDbDataReader rd = cmd.ExecuteReader();
grid1.ItemsSource = rd;
OleDbConnection con = new OleDbConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["Connection"].ToString();
con.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "select * from [Employee] WHERE Name1 = Jacob ";
cmd.Connection = con;
OleDbDataReader rd = cmd.ExecuteReader();
grid1.ItemsSource = rd;
But still the same Error
My Connection String
<connectionStrings>
<add name="Connection" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\wpfdb.accdb;Persist Security Info=False;"/>
In your command text lines, for example:
> cmd.CommandText = "select `Name1`,`ID` from `Employee` WHERE `Name1` =
> Jacob ";
Jacob needs to be wrapped in quotes, not column and table names:
cmd.CommandText = "select * Employee WHERE Name1 = 'Jacob' ";
Related
I trying to format the data that I'm extracting from the Access Database to look something like the format below from the database.
ID Name (Age)
Company Name
Handphone Number
NRIC
Is it possible to do this in a gridview or inside a rich textbox in Visual Studio WPF?
Currently my Code is:
OleDbConnection con = new OleDbConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["Connection"].ToString();
con.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "select ID,Name,Age,Company,Handphone,NRIC FROM Customers WHERE Name = #txtSearch";
cmd.Parameters.AddWithValue("#txtSearch", txtSearch.Text);
cmd.Connection = con;
OleDbDataReader rd = cmd.ExecuteReader();
grid1.ItemsSource = rd;
My code gets to conn.Open and gives me "Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll"
Here is the block of code:
_timer.Stop();
string path = #"C:\testlog.log";
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MYDB_Conn"].ConnectionString);
string query = "SELECT RawImportEnabled, ImportDayTimeStamp from Settings";
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open(); // Dies here
SqlDataReader rdr = cmd.ExecuteReader();
Order is missing ...first open connection then use sql command
_timer.Stop();
string path = #"C:\testlog.log";
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MYDB_Conn"].ConnectionString);
conn.Open(); // sholud be here
string query = "SELECT RawImportEnabled, ImportDayTimeStamp from Settings";
SqlCommand cmd = new SqlCommand(query, conn);
SqlDataReader rdr = cmd.ExecuteReader();
Besides your code is not formatted either..Format like this
string path = #"C:\testlog.log";
String connectionString = ConfigurationManager.ConnectionStrings["MYDB_Conn"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open(); // sholud be here
string query = "SELECT RawImportEnabled, ImportDayTimeStamp from Settings";
SqlCommand cmd = new SqlCommand(query, conn);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
String RawImportEnabled = Convert.ToString(reader["RawImportEnabled"]);
//Do some thing
}
}
This has some benefits like debugging the connection string by putting a breakpoints ,Disposal of connection without worrying for using statements etc
_timer.Stop();
string path = #"C:\testlog.log";
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MYDB_Conn"].ConnectionString);
string query = "SELECT RawImportEnabled, ImportDayTimeStamp from Settings";
conn.Open(); // Dies here again.
SqlCommand cmd = new SqlCommand(query, conn);
SqlDataReader rdr = cmd.ExecuteReader();
if (rdr.HasRows)
I'm trying to get values out of my database into my Listbox, I currently send all my results into a new object called Results
I want my listbox to show something like this:
Title(1)(enter)
Url(1)(enter)
Title(2)(enter)
Url(2)(enter)
and so on
It currently still gives an error at OleDbDataReader reader = command.ExecuteReader(); but I have no idea why.
This is the exact code
OleDbConnection connection = new OleDbConnection();
connection.ConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\martijn\Dropbox\Proftaak Periode 2 Identity\Database11.accdb;
Persist Security Info=False;";
connection.Open();
OleDbCommand cmd1 = new OleDbCommand();
cmd1.Connection = connection;
cmd1.CommandText = "SELECT ZoekcriteriaID from Zoekcriteria WHERE ZoekCriteria = '" + Convert.ToString(lbzoektermen.SelectedItem) + "';";
OleDbDataReader reader1 = cmd1.ExecuteReader();
if(reader1.Read())
{
resultaatid = Convert.ToInt32(reader1["ZoekcriteriaID"]);
}
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "SELECT Titel, Webadress from Resultaat WHERE ZoekriteriaID = '"+ resultaatid +"';";
OleDbDataReader reader = command.ExecuteReader();
lbresultaten.Items.Clear();
List<Results> resultaten = new List<Results>();
while(reader.Read())
{
Results result = new Results();
result.url = Convert.ToString(reader["Webadress"]);
result.titel = Convert.ToString(reader["Webadress"]);
resultaten.Add(result);
}
foreach(Results result in resultaten )
{
lbresultaten.Items.Add(result.titel);
lbresultaten.Items.Add(result.url);
}
I hope someone could help me,
Kind Regards,
Martijn
Your problem probably lays in your where clause:
SELECT ZoekcriteriaID from Zoekcriteria WHERE **ZoekCriteria**
It should be a column name, not a table name.
I'm trying to prevent SQL injections. Am I doing this right? (I'm using MS Access.) Should I still use sqlparameter?
OleDbParameter[] myparm = new OleDbParameter[2];
myparm[0] = new OleDbParameter("#UserID", UserName.Text);
myparm[1] = new OleDbParameter("#Password", encode);
string queryStr = "SELECT * FROM TMUser WHERE UserID=#UserID AND Password=#Password";
OleDbConnection conn = new OleDbConnection(_connStr);
OleDbCommand cmd = new OleDbCommand(queryStr, conn);
conn.Open();
OleDbDataReader dr = cmd.ExecuteReader();
Close!
string queryStr = "SELECT * FROM TMUser WHERE UserID=#UserID AND Password=#Password";
OleDbConnection conn = new OleDbConnection(_connStr);
OleDbCommand cmd = new OleDbCommand(queryStr, conn);
cmd.Parameters.AddWithValue("#UserID", UserName.Text);
cmd.Parameters.AddWithValue("#Password", encode);
The parameters are part of the command object and you use the Parameters.AddWithValue method to set the parameter values to what you have defined in the query string.
By the way, you should be using using statements to encapsulate some of your objects, here is what I typically do:
using (OleDbConnection conn = new OleDbConnection(_connStr))
using (OleDbCommand = conn.CreateCommand())
{
conn.Open();
cmd.CommandText = "SELECT ...";
cmd.Parameters.AddWithValue(...);
cmd.ExecuteReader();
//...
}
That way you don't have to worry about cleaning up resources if something goes wrong inside or closing the connection when you are done.
I am trying to connect to an oracle database using C#.
Here is my code:
OracleConnection conn = new OracleConnection(); // C#
conn.ConnectionString = oradb;
conn.Open();
string sql = " select department_name from departments where department_id = 10"; // C#
OracleCommand cmd = new OracleCommand();
cmd.CommandText = sql;
cmd.Connection = conn;
cmd.CommandType = CommandType.Text; ///this is the line that gives the error
What is the proper way to set the command type? Thank you.
Using Store Procedure:
using (OracleConnection conn = new OracleConnection( oradb ))
{
conn.Open();
OracleCommand cmd = new OracleCommand("StoreProcedureName", con);
cmd.CommandType = CommandType.StoredProcedure;
//specify command parameters
//and Direction
using(OracleDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
//string s = reader.GetInt32(0) + ", " + reader.GetInt32(1);
}
}
}
CommandType.Text: (not mandatory to specify CommandType).
using (OracleConnection conn = new OracleConnection( oradb ))
{
string sql = #"SELECT department_name FROM departments
WHERE department_id = #department_id";
conn.Open();
OracleCommand cmd = new OracleCommand(sql, conn);
//specify command parameters
cmd.Parameters.Add(new OracleParameter("#department_id", 10));
using(OracleDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
//string s = reader.GetString(0);
}
}
}
Make sure you toss each of these pieces in a using() statement, i.e.
using( OracleConnection conn = new OracleConnection( oradb ) )
{
conn.Open();
using( OracleCommand cmd = new OracleCommand( "sql here", conn ) )
{
//cmd.Execute(); cmd.ExecuteNonQuery();
}
}