none
update statement only modifies only last row modified

    Frage

  • Hi all,

    I was not able to update data using datagridview before no i am able update using code below i am calling below mentioned method on cellendedit event but the problem is it update only last row modifies previously modified data not showing in database please inform me am i making any mistake

    the code is:

    private void updatedata(int num)
            {
                string quarter = null;
                string month = null;
                string task = null;
                string subtask = null;
                string resp = null;
                string timeline = null;
                string bsc = null;
                string weightage = null;
                string firstwk = null;
                string secondwk = null;
                string thirdwk = null;
                string fourthwk = null;

                for (int i = num;i<=num ;i++ )
                {
                    quarter = mgm_dgv.Rows[i].Cells["Quarter"].Value.ToString();
                    month = mgm_dgv.Rows[i].Cells["Month"].Value.ToString(); ;
                    task = mgm_dgv.Rows[i].Cells["Task"].Value.ToString();
                    subtask = mgm_dgv.Rows[i].Cells["Subtask"].Value.ToString();
                    resp = mgm_dgv.Rows[i].Cells["Responsibility"].Value.ToString();
                    timeline = mgm_dgv.Rows[i].Cells["Timeline"].Value.ToString();
                    bsc = mgm_dgv.Rows[i].Cells["Bsc"].Value.ToString();
                    weightage = mgm_dgv.Rows[i].Cells["Weightage"].Value.ToString();
                    firstwk = mgm_dgv.Rows[i].Cells["FirstWeek"].Value.ToString();
                    secondwk = mgm_dgv.Rows[i].Cells["SecondWeek"].Value.ToString();
                    thirdwk = mgm_dgv.Rows[i].Cells["ThirdWeek"].Value.ToString();
                    fourthwk = mgm_dgv.Rows[i].Cells["FourthWeek"].Value.ToString();
                    break;
                }

                string query = "UPDATE Table2 SET [Quarter] ='" + quarter + "',[Month]='" + month + "',[Task]='" + task + "',[Subtask]='" + subtask + "',[Responsibility]='" + resp + "',[Timeline]='" + timeline + "',[Bsc]='" + bsc + "',[Weightage]='" + weightage + "',[FirstWeek]='" + firstwk + "',[SecondWeek]='" + secondwk + "',[ThirdWeek]='" + thirdwk + "',[FourthWeek]='" + fourthwk + "' WHERE [Sr]=" + num + "";
                OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Vinayak\Documents\Scorecard.mdb");

                OleDbCommand com = new OleDbCommand(query, con);
                com.CommandType = CommandType.Text;
                
                con.Open();
                
                com.ExecuteNonQuery();
                con.Close();            
                con.Dispose();
            }

    num parameter is for giving value of current row

    Thanks in advance

    Dienstag, 6. März 2012 16:00

Antworten

  • hmm... this is a sausage...

    ok, back to your problem. You have to put stirng and all the code INTO THE for loop! This way you can update each row. Now only the last one, as it does now!!

    Will it go?

    Let me help you, do as:

     OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Vinayak\Documents\Scorecard.mdb");
                con.Open();
                OleDbCommand com = new OleDbCommand();
                string query = "";
                for (int i = num; i <= num; i++)
                {
                    quarter = mgm_dgv.Rows[i].Cells["Quarter"].Value.ToString();
                    month = mgm_dgv.Rows[i].Cells["Month"].Value.ToString(); ;
                    task = mgm_dgv.Rows[i].Cells["Task"].Value.ToString();
                    subtask = mgm_dgv.Rows[i].Cells["Subtask"].Value.ToString();
                    resp = mgm_dgv.Rows[i].Cells["Responsibility"].Value.ToString();
                    timeline = mgm_dgv.Rows[i].Cells["Timeline"].Value.ToString();
                    bsc = mgm_dgv.Rows[i].Cells["Bsc"].Value.ToString();
                    weightage = mgm_dgv.Rows[i].Cells["Weightage"].Value.ToString();
                    firstwk = mgm_dgv.Rows[i].Cells["FirstWeek"].Value.ToString();
                    secondwk = mgm_dgv.Rows[i].Cells["SecondWeek"].Value.ToString();
                    thirdwk = mgm_dgv.Rows[i].Cells["ThirdWeek"].Value.ToString();
                    fourthwk = mgm_dgv.Rows[i].Cells["FourthWeek"].Value.ToString();
                    query = "UPDATE Table2 SET [Quarter] ='" + quarter + "',[Month]='" + month + "',[Task]='" + task + "',[Subtask]='" + subtask + "',[Responsibility]='" + resp + "',[Timeline]='" + timeline + "',[Bsc]='" + bsc + "',[Weightage]='" + weightage + "',[FirstWeek]='" + firstwk + "',[SecondWeek]='" + secondwk + "',[ThirdWeek]='" + thirdwk + "',[FourthWeek]='" + fourthwk + "' WHERE [Sr]=" + num + "";
                    com = new OleDbCommand(query, con);
                    com.ExecuteNonQuery();
                }
                com.Dispose(); //if you dispose, then no need to close() disposing mean permanently closing
                con.Dispose();



    Mitja

    • Als Antwort vorgeschlagen AGovil Dienstag, 6. März 2012 16:44
    • Als Antwort markiert Vinayak Bamane Samstag, 10. März 2012 07:24
    Dienstag, 6. März 2012 16:16