I don't know what am I doing wrong but I tried everything I found on net, the code doesn't give any errors but also doesn't update database.
UPDATED:I solved the problem with taking #no to [] brackets
Here is code :`
String query = "UPDATE Kitaplık SET kitapAdi=#ad,kitapYazari=#yazar,kitapYayinevi=#yayinevi,sayfaSayisi=#sayfaSayisi where no=#no";
con = new OleDbConnection("Provider=Microsoft.ACE.Oledb.12.0;Data Source=Kutuphane.accdb");
cmd = new OleDbCommand(query, con);
cmd.Parameters.AddWithValue("#ad", kitapAdiText.Text);
cmd.Parameters.AddWithValue("#yazar", kitapYazariText.Text);
cmd.Parameters.AddWithValue("#yayinevi", kitapYayıneviText.Text);
cmd.Parameters.AddWithValue("#sayfaSayisi", Convert.ToInt32(sayfaSayisiText.Text));
cmd.Parameters.AddWithValue("#no", Convert.ToInt32(noText.Text));
con.Open();
cmd.ExecuteNonQuery();
con.Close();
listBox1.Items.Clear();
bilgileriCek();
Thanks for the answers!
OP "solved the problem with taking (changing) #no to [] brackets."
Related
I'm trying to insert data into two tables via a WPF app with a button that saves the data, but I keep getting this error and I need some help. I've tried many videos, try to change the tables in the database but I can't get it to work but it works on the Viestit table, but not on Viestihistoria. The error is
System.Data.SqlClient.SqlException: 'Incorrect syntax near '#Viestinlahettaja'
Thank you in advance.
SqlConnection conn = new SqlConnection(#"Data Source=MYCOMPUTER\SQLEXPRESS;Initial Catalog=LomakeDB;User ID=***;Password=***");
conn.Open();
SqlCommand cmd1 = new SqlCommand("INSERT INTO Viestit(ID,Kokonimi,Viesti,Viestin_saaja) values(#ID,#Kokonimi,#Viesti,#Viestin_saaja)", conn);
cmd1.Parameters.AddWithValue("#ID", txtbox_ID.Text);
cmd1.Parameters.AddWithValue("#Kokonimi",txtbox_Nimi.Text);
cmd1.Parameters.AddWithValue("#Viesti", txtbox_Viesti.Text);
cmd1.Parameters.AddWithValue("#Viestin_saaja", txtbox_Viestinvastaanottaja.Text);
cmd1.ExecuteNonQuery();
cmd1.Parameters.Clear();
SqlCommand cmd2 = new SqlCommand("INSERT INTO Viestihistoria(ViestiID,Viesti,Viestin_lahettaja) values(#ViestiID,#Viesti2,#Viestinlahettaja", conn);
cmd2.Parameters.AddWithValue("#ViestiID", txtbox_ID.Text);
cmd2.Parameters.AddWithValue("#Viesti2", txtbox_Viesti.Text);
cmd2.Parameters.AddWithValue("#Viestinlahettaja", txtbox_Nimi.Text);
cmd2.ExecuteNonQuery();
cmd2.Parameters.Clear();
MessageBox.Show("Data lähetetty onnistuneesti!");
conn.Close();
You forgot to add closing ) at the end of this line in sql command text:
The corrected line:
SqlCommand cmd2 = new SqlCommand("INSERT INTO Viestihistoria(ViestiID,Viesti,Viestin_lahettaja) values(#ViestiID,#Viesti2,#Viestinlahettaja)", conn);
I've Edited this question to prevent asking another. I'm trying to update a datatable in MS Access using an UPDATE query and Parameters. The code doesn't give an error and runs fine, but it does't save the updated data and I can't work out why.
Here's the code I'm using :
if (Editing)
{
string Query = "UPDATE [Employee] SET PayrollNo=#PayrollNo, FirstName=#FirstName, LastName=#LastName, AnnualHolidayEntitlement=#AnnualHolidayEntitlement, DaysTakenToDate=#DaysTakenToDate WHERE PayrollNo = #SentPayroll";
string ConnString = "Provider = Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\HoliPlanData.accdb;Persist Security Info=False";
using (OleDbConnection conn = new OleDbConnection(ConnString))
using (OleDbCommand UpdateEmployee = new OleDbCommand(Query, conn))
{
conn.Open();
UpdateEmployee.Parameters.AddWithValue("#SentPayroll", OleDbType.Integer).Value = Convert.ToInt32(PassPayrollNo);
UpdateEmployee.Parameters.AddWithValue("#PayrollNo", OleDbType.Integer).Value = Convert.ToInt32(TxtPayrollNo.Text);
UpdateEmployee.Parameters.AddWithValue("#FirstName", OleDbType.VarChar).Value = TxtFirstName.Text;
UpdateEmployee.Parameters.AddWithValue("#LastName", OleDbType.VarChar).Value = TxtLastName.Text;
UpdateEmployee.Parameters.AddWithValue("#AnnualHolidayEntitlement", OleDbType.Integer).Value = Convert.ToInt32(TxtAHE.Text);
UpdateEmployee.Parameters.AddWithValue("#DaysTakenToDate", OleDbType.Integer).Value = Convert.ToInt32(TxtDTTD.Text);
UpdateEmployee.ExecuteScalar();
BtnSubmit.Enabled = false;
MessageBox.Show("Data Edited Successfully");
}
How can I change this code so that it works? Thanks in advance.
EDIT/ANSWER.
I didn't know and chances are if you're having problems with an UPDATE query you may not know it either. When adding parameters you need to add them in the same order they are called in the query. In my code the first parameter should be added last, since it is last in the stringQuery. I moved it and it worked fine!
Why does invoking my UPDATE query in an Access database via OleDb not work?
If anyone else has this issue with UPDATE queries, It took me too long to find this question, where someone clears it up great.
It seems like i do connect to my database judging by no error messege after executing the code but i'm unable to do INSERT INTO command even though it works if i copy the code to sql server command line client. What did i do wrong ?
This is my code
Maybe some code is off the bottom of the screen there but, you will need to execute the command. Something like:
cmd.ExecuteNonQuery();
Also, before doing that you should indicate that it's a text command
cmd.CommandType = System.Data.CommandType.Text;
Okay so i've actually solved it, thanks comment above for helping me at least slightly. After i posted your code it didn't work but when i moved it under the open like that :
private void Form1_Load(object sender, EventArgs e)
{
con = new MySql.Data.MySqlClient.MySqlConnection();
con.ConnectionString = myConnectionString;
MySqlCommand cmd = con.CreateCommand();
try
{
con.Open();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
MessageBox.Show(ex.Message);
}
cmd.CommandText = "INSERT INTO DaneRecepcjonistki VALUES('123', 'Kot', 'Pies');";
cmd.CommandType = System.Data.CommandType.Text;
cmd.ExecuteNonQuery();
} It works !
Thanks !
This is my code:
conn = new SqlConnection("Server=(localdb)\\v11.0;Integrated Security=true;AttachDbFileName=|DataDirectory|\\Users.mdf;MultipleActiveResultSets=True;");
conn.Open();
SqlCommand comm = new SqlCommand("update users set surname='simpson' where id=1", conn);
int i = comm.ExecuteNonQuery();
MessageBox.Show(i + "");
comm = new SqlCommand("select surname from users where id=1", conn);
SqlDataReader reader = comm.ExecuteReader();
if (reader.Read())
MessageBox.Show(reader[0] + "");
conn.Close();
The ExecuteNonQuery returns 1 to show the database has been updated and the second query confirms it. But when I open the database in visual studio 2013, there are no changes, database still the same
YOu mean, when you stop the program and Visual Studio throws away the copy of the database and resets it to the empty one then the data is gone? Yes, that is true.
Maybe you need to change your connectionstring, or refresh the database.
Apparently the database had been set to copy to the output folder each time
I am trying to access an Access 2003 database remotely from my ASP.NET application. My code is:
DataSet dsImportedData = new DataSet();
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection();
conn.ConnectionString = #"Provider=MS Remote;Remote Provider=Microsoft.Jet.OLEDB.4.0;Remote Server=http://myIp;Data source=C:\myDatabase.mdb;";
try
{
System.Data.OleDb.OleDbCommand command = conn.CreateCommand();
command.CommandText = "SELECT * FROM myTable";
conn.Open();
System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(command);
adapter.Fill(dsImportedData);
}
catch (Exception ex)
{
}
finally
{
conn.Close();
}
However, I am always getting an exception stating: {"[Microsoft][ODBC Microsoft Access Driver] Invalid SQL statement; expected 'DELETE', 'INSERT', 'PROCEDURE', 'SELECT', or 'UPDATE'."}
My command is basic, I have no idea what could be wrong with it. Did anyone confront with the same issue? Thanks!
Try this ....
String command = "SELECT * FROM myTable";
conn.Open();
System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(command, conn);
adapter.Fill(dsImportedData);
Apparently the error can be caused by the specified table not existing. Just a thought...
Another thought would be to remove the remoting complexity and try to get to the most simple working code, possibly with a new access database just to start to narrow down what is causing the problem.
If you set the command type to Stores procedure it works for me.
command.CommandType = System.Data.CommandType.StoredProcedure;