I have written this code, it works perfectly but it wont edit (update) the selected data. have I missed something.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
namespace Registry
{
public partial class Form10 : Form
{
private OleDbConnection connection = new OleDbConnection();
public Form10()
{
InitializeComponent();
connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\fredhope\Desktop\Registry Access\registry.accdb;
Persist Security Info=False;";
load_table();
}
void load_table()
{
try
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
// string query = "select * from registry";
string query = "select ID,siku,sfnumber,shelf,raw,column from registry";
command.CommandText = query;
OleDbDataAdapter da = new OleDbDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string query = "UPDATE [registry] SET [siku]='" + dateTimePicker1.Text + "',[shelf]='" + txt_shelf.Text + "',[raw]='" +
txt_raw.Text + "',[column]='" + txt_column.Text + "' where [sfnumber]='" + txt_sfnumber + "'";
MessageBox.Show(query);
command.CommandText = query;
command.ExecuteNonQuery();
MessageBox.Show("Data Edited Successful");
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
}
}
private void button2_Click(object sender, EventArgs e)
{
Form4 m = new Form4();
m.Show();
Form10 f = new Form10();
this.Visible = false;
this.Hide();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string query = "select * from [registry] where [sfnumber]='"+comboBox1.Text+"'";
command.CommandText = query;
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
// dateTimePicker1.Text = reader["siku"].ToString();
// txt_sfnumber.Text = reader["sfnumber"].ToString();
// txt_casezone.Text = reader["casezone"].ToString();
// txt_casetype.Text = reader["casetype"].ToString();
// txt_appellant.Text = reader["appellant"].ToString();
// txt_respondant.Text = reader["respondant"].ToString();
// txt_shelf.Text = reader["shelf"].ToString();
// txt_raw.Text = reader["raw"].ToString();
// txt_column.Text = reader["column"].ToString();
//other way
string dateTimePicker1 = reader["siku"].ToString();
string sfnumber = reader["sfnumber"].ToString();
string casezone = reader["casezone"].ToString();
string casetype = reader["casetype"].ToString();
string appellant = reader["appellant"].ToString();
string respondant = reader["respondant"].ToString();
string shelf = reader["shelf"].ToString();
string raw = reader["raw"].ToString();
string column = reader["column"].ToString();
}
connection.Close();
}
catch(Exception ex)
{
MessageBox.Show("Error " + ex);
}
}
}
}