soon
Hectopat
- Katılım
- 16 Şubat 2021
- Mesajlar
- 581
- Çözümler
- 3
Update işlemi yapamıyorum.
Eki Görüntüle 1641307
C#:
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 CrudApplication
{
public partial class Form1 : Form
{
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=CrudApp.mdb");
OleDbCommand cmd;
OleDbDataAdapter da;
public int id;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
showInformation();
}
public void showInformation()
{
try
{
conn.Open();
da = new OleDbDataAdapter("select * from Users", conn);
DataSet ds = new DataSet();
da.Fill(ds, "Users");
dataGridView1.DataSource = ds.Tables["Users"];
conn.Close();
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
private void buttonAdd_Click(object sender, EventArgs e)
{
try
{
conn.Open();
cmd = new OleDbCommand("insert into Users(uName,uSurname,uGender,uBorn,uPhone,uHobi) values(@name,@surname," +
"@gender,@born,@phone,@hobi)", conn);
cmd.Parameters.AddWithValue("@name", textBoxName.Text.ToString());
cmd.Parameters.AddWithValue("@surname", textBoxSurname.Text.ToString());
if (radioButtonMale.Checked == true)
cmd.Parameters.AddWithValue("@gender", radioButtonMale.Text.ToString());
else
cmd.Parameters.AddWithValue("@gender", radioButtonFemale.Text.ToString());
cmd.Parameters.AddWithValue("@born", dateTimePicker1.Value.ToString());
cmd.Parameters.AddWithValue("@phone", textBoxPhone.Text.ToString());
cmd.Parameters.AddWithValue("@hobi", comboBox1.SelectedItem.ToString());
cmd.ExecuteNonQuery();
conn.Close();
showInformation();
textBoxName.Clear();
textBoxSurname.Clear();
textBoxPhone.Clear();
radioButtonFemale.Checked = false;
radioButtonMale.Checked = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
private void buttonDelete_Click(object sender, EventArgs e)
{
try
{
conn.Open();
cmd = new OleDbCommand("delete from Users where ID=@id", conn);
cmd.Parameters.AddWithValue("@id", id);
cmd.ExecuteNonQuery();
conn.Close();
showInformation();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
private void buttonUpdate_Click(object sender, EventArgs e)
{
if (id != 0)
{
try
{
conn.Open();
string sql = "UPDATE Users SET uName = @name , uSurname = @surname,uGender = @gender,uBorn = @born,uPhone = @phone,uHobi = @hobi where ID=@id";
cmd = new OleDbCommand(sql, conn);
cmd.Parameters.AddWithValue("@id", id);
cmd.Parameters.AddWithValue("@name", textBoxName.Text.ToString());
cmd.Parameters.AddWithValue("@surname", textBoxSurname.Text.ToString());
if (radioButtonMale.Checked == true)
cmd.Parameters.AddWithValue("@gender", radioButtonMale.Text.ToString());
else
cmd.Parameters.AddWithValue("@gender", radioButtonFemale.Text.ToString());
cmd.Parameters.AddWithValue("@born", dateTimePicker1.Value.ToString());
cmd.Parameters.AddWithValue("@phone", textBoxPhone.Text.ToString());
cmd.Parameters.AddWithValue("@hobi", comboBox1.SelectedItem.ToString());
cmd.ExecuteNonQuery();
conn.Close();
showInformation();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
else
MessageBox.Show("Select record");
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
id = Convert.ToInt32(row.Cells[0].Value);
textBoxName.Text = Convert.ToString(row.Cells[1].Value);
textBoxSurname.Text = Convert.ToString(row.Cells[2].Value);
string gender = Convert.ToString(row.Cells[3].Value);
if (gender == "Male")
radioButtonMale.Checked = true;
else
radioButtonFemale.Checked = true;
dateTimePicker1.Value = Convert.ToDateTime(row.Cells[4].Value);
textBoxPhone.Text = Convert.ToString(row.Cells[5].Value);
comboBox1.Text = Convert.ToString(row.Cells[6].Value);
}
}
}