User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C# section within the Software Development category of DaniWeb, a massive community of 402,813 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,053 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C# advertiser: Programming Forums
Views: 331 | Replies: 3
Reply
Join Date: May 2008
Posts: 31
Reputation: harcaype is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
harcaype harcaype is offline Offline
Light Poster

deleted rows not updating

  #1  
Jul 15th, 2008
In relation to my previous post. I was able to delete the rows in the datagridview (thanks to ema005), but now I am having problems with the updating of it in the database. It's deleting in my datagridview alright. But not in my database. My codes are already going on spaghetti just trying to figure it out. Can you help me with this? Here's my code:

This is from my separate class I made:

class dbConn
    {
        public SqlConnection myConn = new SqlConnection();
        public SqlDataAdapter da;
        public SqlDataAdapter da2;
        public SqlDataAdapter da3;
        SqlCommand Command;
        SqlCommand aCommand;
        SqlCommand rdrCommand;
        SqlDataReader rdr;
        SqlCommandBuilder cmdBld;
        public DataTable dt = new DataTable();
        public DataTable dt2 = new DataTable();
        public DataTable dt3 = new DataTable();
        public DataTable dTable = new DataTable();
        public DataSet ds = new DataSet();
        public DataSet ds2 = new DataSet();
        public DataSet ds3 = new DataSet();
        int check;
        int iRowIndex = 0;
        List<string> strArray = new List<string>();

public void OpenConnect()
        {
            myConn = new SqlConnection(@"Data Source =.\sqlexpress; integrated security = true; Initial Catalog = mmdaserver;");
        }

        public void DisplayInfo()
        {
            OpenConnect();
            Command = new SqlCommand("DisplayRecords", myConn);
            Command.CommandType = CommandType.StoredProcedure;
            aCommand = new SqlCommand("AlarmList", myConn);
            aCommand.CommandType = CommandType.StoredProcedure;

            da = new SqlDataAdapter(Command);
            da.Fill(ds, "Records");
            da = new SqlDataAdapter(aCommand);
            da.Fill(ds2, "Records");

            da2 = new SqlDataAdapter("SELECT * FROM DriverInfo", myConn);
            da3 = new SqlDataAdapter("SELECT * FROM Violations", myConn);
            da2.Fill(dt);
            da2.Fill(ds, "DriverInfo");
            da3.Fill(dt);
            da3.Fill(ds, "Violations");
            myConn.Close();
        }
}

And this is my code from my Form1

dbConn dbc = new dbConn();
        BindingSource bSrc = new BindingSource();
        Login thisFirst = new Login();
        
        public Form1()
        {
            InitializeComponent();
            dbc.DisplayInfo();
            dataGridRecords.AllowUserToDeleteRows = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Start();
            groupBox1.Enabled = false;
            groupBox2.Enabled = false;
            groupBox3.Enabled = false;
            dataGridRecords.DataSource = null;
            dataGridAlarm.DataSource = null;
            dataGridViolations.DataSource = null;
            dataGridDriverInf.DataSource = null;
            thisFirst.ShowDialog();

            groupBox1.Enabled = true;
            groupBox2.Enabled = true;
            groupBox3.Enabled = true;
            RefreshGrid();
        }

        public void RefreshGrid()
        {
            dbc.ds.Clear();
            dbc.ds2.Clear();
            dbc.da.Update(dbc.dTable);
            dbc.DisplayInfo();
            dataGridRecords.DataSource = dbc.ds.Tables["Records"];
            dataGridAlarm.DataSource = dbc.ds2.Tables["Records"];
            dataGridViolations.DataSource = dbc.ds.Tables["Violations"];
            dataGridDriverInf.DataSource = dbc.ds.Tables["DriverInfo"];
        }

This is my delete button from this form:

private void btnDelete_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Are you sure you want to delete this row?", "Confirm deleting", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            
                if (dr == DialogResult.Yes)
                {
                    dbc.dTable = new DataTable("Records");
                    foreach (DataGridViewRow row in dataGridRecords.SelectedRows)
                    {
                        if (row.Index != dataGridRecords.Rows.Count - 1)
                        {
                            dbc.OpenConnect();
                            dbc.myConn.Open();
                            dbc.dTable = dbc.ds.Tables[0];
                            dbc.dTable.Rows[row.Index].Delete();
                            dbc.dTable.GetChanges();
                            dbc.dTable.AcceptChanges();
                            bSrc.DataSource = dbc.dTable;
                            dataGridRecords.DataSource = bSrc;
                            dbc.da.Update(dbc.dTable);
                        }
                    }

                    dbc.ds.GetChanges();

                    if (dbc.ds != null)
                    {
                        dbc.ds.AcceptChanges();
                    }
                    dbc.myConn.Close();
                }
       }
Last edited by harcaype : Jul 15th, 2008 at 8:20 am.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2008
Posts: 89
Reputation: camilojvarona is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 9
camilojvarona camilojvarona is offline Offline
Junior Poster in Training

Re: deleted rows not updating

  #2  
Jul 15th, 2008
Hi,

Your problem is that you have deleted rows from the DataTable(this act like a cache but its not related to the fisical DB wich means that whatever you do in the DT doesn't reflecs in the DB) and not in the actual data base what you need to do is run delete domands(SQL) against the DB .

Regards,
Camilo
Last edited by camilojvarona : Jul 15th, 2008 at 9:38 pm.
Reply With Quote  
Join Date: May 2008
Posts: 31
Reputation: harcaype is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
harcaype harcaype is offline Offline
Light Poster

Re: deleted rows not updating

  #3  
Jul 16th, 2008
I see. How can I run delete queries in SQL specifying the row number then?
Reply With Quote  
Join Date: Jul 2008
Posts: 89
Reputation: camilojvarona is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 9
camilojvarona camilojvarona is offline Offline
Junior Poster in Training

Re: deleted rows not updating

  #4  
Jul 16th, 2008
Hi,

I've found this link where the compiler create the SQL delete comand for you.

http://searchwindevelopment.techtarg...866086,00.html

note: You just need to change everywhere where has OleDb for Sql

Regards,
Camilo
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C# Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C# Forum

All times are GMT -4. The time now is 11:04 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC