Update problem.

Damage

Member
Joined
Sep 5, 2016
Messages
6
Location
Serbia
Programming Experience
3-5
I have problem with update button.
When i edit data and click save i get edited data in dataGridView but when i exit form and come back, data is the same like before editing.
Here is my code:

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 MySql.Data.MySqlClient;


namespace mitAdministrator
{
public partial class Katedre : Form
{
MySqlConnection connection = new MySqlConnection("SERVER=mysql.ivanpetric.org;DATABASE=sqlmasdata;UID=*******;PASSWORD=******;");


public Katedre()
{
InitializeComponent();
}


private void buttonKatedreExit_Click(object sender, EventArgs e)
{

this.Close();
}


private void Katedre_Load(object sender, EventArgs e)
{
//Disable textBox
textBoxIndentifikacioniBroj.Enabled = false;
textBoxImeKatedre.Enabled = false;
textBoxKatedraStartID.Visible = false;
buttonKatedreCancel.Enabled = false;
buttonKatedreSave.Enabled = false;



// Load Data Grid View
loadTable();
}



public void loadTable()
{


MySqlCommand cmdDataBase = new MySqlCommand(" select * from mitAdminPanelKatedre ;", connection);


try
{
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = cmdDataBase;
DataTable dbDataSet = new DataTable();
sda.Fill(dbDataSet);


mitAdminPanelKatedreBindingSource.DataSource = dbDataSet;
dataGridViewKatedre.DataSource = mitAdminPanelKatedreBindingSource;
sda.Update(dbDataSet);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}



private void buttonKatedreSave_Click(object sender, EventArgs e)
{
try
{
if (textBoxIndentifikacioniBroj.Text != "" && textBoxImeKatedre.Text != "")
{
connection.Open();


string query = "insert into mitAdminPanelKatedre (katedraID,katedraIme) values (@katedraID,@katedraIme)";


using (MySqlCommand cmd = new MySqlCommand(query, connection))
{
cmd.Parameters.AddWithValue("@katedraID", textBoxIndentifikacioniBroj.Text);
cmd.Parameters.AddWithValue("@katedraIme", textBoxImeKatedre.Text);
cmd.ExecuteNonQuery();
}


connection.Close();
MessageBox.Show("Katedra " + textBoxImeKatedre.Text + "je uspe?no dodata u bazu!", "Uspesno insertovanje podataka.", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}





// Lock textBox
textBoxIndentifikacioniBroj.Enabled = false;
textBoxImeKatedre.Enabled = false;


// Unlock button
buttonKatedreNext.Enabled = true;
buttonKatedrePrevious.Enabled = true;


buttonKatedreSave.Enabled = false;
buttonKatedreDelete.Enabled = true;


// Refresh Data Grid View
loadTable();
}


private void buttonKatedreNew_Click(object sender, EventArgs e)
{
textBoxIndentifikacioniBroj.Enabled = true;
textBoxImeKatedre.Enabled = true;


textBoxIndentifikacioniBroj.Clear();
textBoxImeKatedre.Clear();


buttonKatedreNext.Enabled = false;
buttonKatedrePrevious.Enabled = false;
buttonKatedreSave.Enabled = true;
buttonKatedreDelete.Enabled = false;
}


private void buttonKatedreNext_Click(object sender, EventArgs e)
{
mitAdminPanelKatedreBindingSource.MoveNext();
}


private void buttonKatedrePrevious_Click(object sender, EventArgs e)
{
mitAdminPanelKatedreBindingSource.MovePrevious();
}


private void buttonKatedreDelete_Click(object sender, EventArgs e)
{
string query = "delete from mitAdminPanelKatedre where katedraID='" + textBoxIndentifikacioniBroj.Text + "' and katedraIme='" +textBoxImeKatedre.Text+ "'";
MySqlCommand cmdDataBase = new MySqlCommand(query, connection);
MySqlDataReader myReader;


try
{
connection.Open();
myReader = cmdDataBase.ExecuteReader();
MessageBox.Show("Katedra " + textBoxImeKatedre.Text + "je uspe?no obrisana!", "Uspesno brisanje podataka.", MessageBoxButtons.OK, MessageBoxIcon.Information);
while (myReader.Read())
{
}


myReader.Close();
connection.Close();


}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
textBoxIndentifikacioniBroj.Clear();
textBoxImeKatedre.Clear();
}


loadTable();
}


private void buttonKatedreEdit_Click(object sender, EventArgs e)
{
textBoxIndentifikacioniBroj.Enabled = true;
textBoxImeKatedre.Enabled = true;


buttonKatedreDelete.Enabled = false;
buttonKatedreNew.Enabled = false;
buttonKatedreSave.Enabled = false;
buttonKatedreNext.Enabled = false;
buttonKatedrePrevious.Enabled = false;
dataGridViewKatedre.Enabled = false;


buttonKatedreCancel.Enabled = true;
}


private void buttonKatedreEditSave_Click(object sender, EventArgs e)
{
try
{
if (textBoxIndentifikacioniBroj.Text != "" && textBoxImeKatedre.Text != "")
{
connection.Open();

string query = "UPDATE mitAdminPanelKatedre SET katedraID='" + this.textBoxIndentifikacioniBroj.Text + "',katedraIme='" + this.textBoxImeKatedre.Text + "' WHERE katedraStartID='" + this.textBoxKatedraStartID.Text + "'";

using (MySqlCommand cmd = new MySqlCommand(query, connection))
{
cmd.ExecuteNonQuery();
}

connection.Close();
MessageBox.Show("Katedra " + textBoxImeKatedre.Text + "je uspe?no promenjena!", "Uspesno insertovanje podataka.", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (connection != null)
connection.Close();
}







textBoxIndentifikacioniBroj.Enabled = false;
textBoxImeKatedre.Enabled = false;


buttonKatedreDelete.Enabled = true;
buttonKatedreNew.Enabled = true;
buttonKatedreSave.Enabled = false;
buttonKatedreNext.Enabled = true;
buttonKatedrePrevious.Enabled = true;
dataGridViewKatedre.Enabled = true;


buttonKatedreCancel.Enabled = false;
}


private void buttonKatedreCancel_Click(object sender, EventArgs e)
{
textBoxIndentifikacioniBroj.Enabled = false;
textBoxImeKatedre.Enabled = false;


buttonKatedreDelete.Enabled = true;
buttonKatedreNew.Enabled = true;
buttonKatedreSave.Enabled = false;
buttonKatedreNext.Enabled = true;
buttonKatedrePrevious.Enabled = true;
dataGridViewKatedre.Enabled = true;


mitAdminPanelKatedreBindingSource.MoveFirst();
}
}
}
 

Latest posts

Back
Top Bottom