Click here to Skip to main content
15,896,497 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 MySql.Data.MySqlClient;

namespace STIMKTIAA_Info
{
    public partial class frmUPMaintenances : Form
    {
        MySqlConnection con = new MySqlConnection();
        MySqlCommand cmd;
        MySqlCommandBuilder cdb;
        MySqlDataAdapter ad;
        DataSet ds;
        DataTable dt;
        
        
        public frmUPMaintenances()
        {
            InitializeComponent();
            cbxAccessType.Items.Add("ÄDMIN");
            cbxAccessType.Items.Add("STI ADMIN");
            cbxAccessType.Items.Add("ALUMNI");
        }

        private void frmUPMaintenances_Load(object sender, EventArgs e)
        {
            this.CancelButton = btnCancel;
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            con.ConnectionString = ("datasource = localhost;port=3306; Initial Catalog = 'alumni_info'; username =root; password=");
            string q = "SELECT * FROM `user_login`";
            con.Open();
            cmd = new MySqlCommand(q, con);
            ad = new MySqlDataAdapter(cmd);
            cdb = new MySqlCommandBuilder(ad);
            ds = new DataSet();
            ad.Fill(ds, "user_login");
            dt = ds.Tables["user_login"];
            con.Close();
            //btnAdd.Visible = false;
            dataGridView1.DataSource = ds.Tables["user_login"];
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

        }
             

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            string q = "UPDATE `user_login` SET USER_ID ='"+ txtUID.Text +"', LNAME ='" + txtLName.Text + "', FNAME ='" + txtFName.Text + "', MNAME ='" + txtMI.Text + "', POSITION ='" + txtPosition.Text + "', ACCESS_TYPE='" + cbxAccessType.SelectedItem + "', UNAME ='" + txtUName.Text + "', PASSWORD ='" + txtPwd.Text + "'";
            ExecuteQuery(q);
            ad.Update(dt);
            btnDelete.Enabled = false;
            btnUpdate.Enabled = true;
            //btnAdd.Visible = false;
        }

       
        public void openCon()
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
        }

        public void closeCon()
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }

        public void ExecuteQuery(string q)
        {
            try
            {
                con.Open();
                cmd = new MySqlCommand(q, con);
                if (cmd.ExecuteNonQuery() == 1)
                {
                    MessageBox.Show("Query EXECUTED!!");

                }
                else
                {
                    MessageBox.Show("Query UNSUCCESSFUL!");
                }
            }
            catch (Exception eh)
            {
                MessageBox.Show(eh.Message);
            }
            finally
            {
                con.Close();
            }

        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            string q = "DELETE FROM `user_login` WHERE USER_ID ='" + txtUID.Text + "'";
            ExecuteQuery(q);
            ad.Update(dt);
            txtLName.Text = " ";
            txtFName.Text = " ";
            txtMI.Text = " ";
            txtPosition.Text = " ";
            txtUName.Text = " ";
            txtPwd.Text = " ";
            //btnAdd.Visible = false;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtUID.Clear();
            txtLName.Clear();
            txtFName.Clear();
            txtMI.Clear();
            txtPosition.Clear();
            txtUName.Clear();
            txtPwd.Clear();
            cbxAccessType.Items.Clear();
            //btnAdd.Visible = false;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
            frmMainPage mp = new frmMainPage();
            mp.Show();
            //btnAdd.Visible = false;
        }

        private void cbxAccessType_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbxAccessType.Text = cbxAccessType.SelectedItem.ToString();
            string v;
            v = cbxAccessType.Text;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
           /* dataGridView1.DataSource = null;
            dataGridView1.Rows.Clear();
            dataGridView1.Refresh();*/
            DataGridViewRow dg = dataGridView1.SelectedRows[0];
            txtUID.Text = dg.Cells[0].Value.ToString();
            txtLName.Text = dg.Cells[1].Value.ToString();
            txtFName.Text = dg.Cells[2].Value.ToString();
            txtMI.Text = dg.Cells[3].Value.ToString();
            txtPosition.Text = dg.Cells[4].Value.ToString();
            cbxAccessType.SelectedText = dg.Cells[5].Value.ToString();
            txtUName.Text = dg.Cells[6].Value.ToString();
            txtPwd.Text = dg.Cells[7].Value.ToString();

        }


       

    }
}


this my code when the update button is on the loose all the data in datagridview changes to one particular item while access type column the data is missing

What I have tried:

I tried to rewrite the UPDATE sql command but it never runs again
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900