Hi!
I receive this message "Not updated".
Please have a look if there is a wrong code somewhere. Because me I didnt see any wrong here:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace Entrada
{
public partial class SaidaViatura : Form
{
OleDbConnection conn;
OleDbCommand comm;
string connstr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\lunarentacar\lunarentacar\\app_data\office.mdb;Persist Security Info=False";
public SaidaViatura()
{
InitializeComponent();
}
private void cbxCars(object sender, EventArgs e)
{
{
conn = new OleDbConnection(connstr);
comm = new OleDbCommand();
conn.Open();
OleDbParameter chaparia = new OleDbParameter("@chaparia", SqlDbType.VarChar);
comm.Connection = conn;
comm.CommandText = "UPDATE cars SET chaparia = @chaparia WHERE (code= @code)";
try
{
comm.ExecuteNonQuery();
//dgvSaidaviatura.CurrentCell.Value = dgvSaidaviatura.Rows[dgvSaidaviatura.Rows.Count - 1].Cells[0].Value;
MessageBox.Show("Update");
}
catch (Exception)
{
MessageBox.Show("Not updated");
}
finally
{
conn.Close();
}
}
Mfwamba TshimangaPosted Nov 12, 2014, 8:50 AM
VulpesPosted Nov 7, 2014, 3:16 AM
However, you should then add them to the parameters collection in the same order as they appear in your SELECT, UPDATE, INSERT or DELETE statement and, instead of using their names, indicate their presence by question marks within this statement.
The reason is that the OleDb provider identifies parameters by position rather than by name.
So it replaces the first question mark with the first parameter in the collection, the second question mark with the second parameter in the collection and so on.
Should you ever use ODBC then it's the same with that.
Just a peculiarity of the way these data providers work :)
Mfwamba TshimangaPosted Nov 6, 2014, 7:31 PM
VulpesPosted Nov 6, 2014, 5:53 PM
Mfwamba TshimangaPosted Nov 6, 2014, 4:38 PM
Please see what I do. Its showing the message "updated". But when I open my database I realize that nothing was updated. Please can you have a look:
private void cbxCars_SelectedIndexChanged(object sender, EventArgs e)
{
{
conn = new OleDbConnection(connstr);
comm = new OleDbCommand();
conn.Open();
OleDbParameter code = new OleDbParameter("@code", SqlDbType.VarChar);
OleDbParameter chaparia = new OleDbParameter("@chaparia", SqlDbType.VarChar);
OleDbParameter ultimokm = new OleDbParameter("@ultimokm", SqlDbType.NChar);
comm.Parameters.Add(code);
comm.Parameters.Add(chaparia);
comm.Parameters.Add(ultimokm);
code.Value = txtcodeSaidaviatura.Text;
chaparia.Value = cbxChapariaSaidaviatura.Text;
ultimokm.Value = txtUltimokmSaidaviatura.Text;
comm.Connection = conn;
comm.CommandText = "UPDATE saidaviatura SET chaparia = @chaparia, ultimokm = @ultimokm WHERE (code = @code)";
try
{
comm.ExecuteNonQuery();
//dgvSaidaviatura.CurrentCell.Value = dgvSaidaviatura.Rows[dgvSaidaviatura.Rows.Count - 1].Cells[0].Value;
MessageBox.Show("Updated");
}
catch (Exception)
{
MessageBox.Show("Not updated");
}
finally
{
conn.Close();
}
VulpesPosted Nov 6, 2014, 11:09 AM