none
Не работает запрос обновление, что не так в коде?? RRS feed

  • Вопрос

  • {
                try
                {
                    if (MessageBox.Show("Вы хотите обновить строку", "Увага", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                    {
                        adp.UpdateCommand = new OleDbCommand("Update into MembersInfo set Group_ID='" + txbx_Group_ID.Text + "', Region_ID='" + cbx_IDRegion.Text + "', City='" + txbx_City.Text + "', PIB='" + txbx_PIB.Text + "', mobilePhone='" + txbx_Mobile.Text + "', eMail='" + txbx_eMail.Text + "', VK_ID='" + txbx_VK_ID.Text + "', FaceBook_ID='" + txbx_Fb.Text + "', Odk_ID='" + txbx_Odk.Text + "', GooglePlus_ID='" + txbx_Gplus.Text + "'( where MI_ID='" + txbx_MI_ID.Text + "')", con);
                        con.Open();
                        adp.UpdateCommand.ExecuteNonQuery();
                        con.Close();
                    }
                }
                catch
                {
    
                }
            }

    31 марта 2013 г. 22:31

Ответы

  • Add parameters the same way they appear in the statement (since OleDb is using named parameters). In other words, move your first parameter to the end. Each parameter needs to be added in the same order they appear in the command. So, looks like all your parameters are in the correct order except for the first one which should be last.


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog

    1 апреля 2013 г. 18:47
  • Add parameters the same way they appear in the statement (since OleDb is using named parameters). In other words, move your first parameter to the end. Each parameter needs to be added in the same order they appear in the command. So, looks like all your parameters are in the correct order except for the first one which should be last.


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog

     private void btn_EditRecord_Click(object sender, EventArgs e)
            {
                string update = "update MembersInfo set Group_ID=?, Region_ID=?, City=?, PIB=?, mobilePhone=?, eMail=?, VK_ID=?, FaceBook_ID=?, Odk_ID=?, GooglePlus_ID=? where MI_ID=?";
                OleDbCommand com = new OleDbCommand(update, con);
                com.CommandType = CommandType.Text;
                com.Parameters.Add("@Group_ID", OleDbType.Integer).Value = Convert.ToInt32(txbx_Group_ID.Text);
                com.Parameters.Add("@Region_ID", OleDbType.Integer).Value = Convert.ToInt32(cbx_IDRegion.SelectedValue.ToString());
                com.Parameters.Add("@City", OleDbType.Char, 55).Value = txbx_City.Text;
                com.Parameters.Add("@PIB", OleDbType.Char, 122).Value = txbx_PIB.Text;
                com.Parameters.Add("@mobilePhone", OleDbType.Char, 16).Value = txbx_Mobile.Text;
                com.Parameters.Add("@eMail", OleDbType.Char, 44).Value = txbx_eMail.Text;
                com.Parameters.Add("@VK_ID", OleDbType.Char, 55).Value = txbx_VK_ID.Text;
                com.Parameters.Add("@FaceBook_ID", OleDbType.Char, 55).Value = txbx_Fb.Text;
                com.Parameters.Add("@Odk_ID", OleDbType.Char, 55).Value = txbx_Odk.Text;
                com.Parameters.Add("@GooglePlus_ID", OleDbType.Char, 55).Value = txbx_Gplus.Text;
                com.Parameters.Add("@MI_ID", OleDbType.Integer).Value = Convert.ToInt32(txbx_MI_ID.Text);
                con.Open();
                com.ExecuteNonQuery();
                con.Close();
            }


    1 апреля 2013 г. 19:02
  • Just wondering why did you mark your own response that quoted my answer? Should not you mark my as answer if it solved your problem?

    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog

    1 апреля 2013 г. 19:05

Все ответы

  • 1. You need to use parameters

    2. Is something happening on the ExecuteNonQuery line - do you get an exception?


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog

    31 марта 2013 г. 23:01
  • 1. You need to use parameters

    2. Is something happening on the ExecuteNonQuery line - do you get an exception?


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog

    А подскажите, как правильно в этом запроси обьявить параметры?
    1 апреля 2013 г. 11:33
  • Didn't we discuss this exact same problem in few of your other threads? I think I did research that time in regards to OleDb parameters.

    See second answer here 

    http://stackoverflow.com/questions/2675610/how-to-update-a-table-using-oledb-parameters


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog

    1 апреля 2013 г. 12:59
  • Didn't we discuss this exact same problem in few of your other threads? I think I did research that time in regards to OleDb parameters.

    See second answer here 

    http://stackoverflow.com/questions/2675610/how-to-update-a-table-using-oledb-parameters


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog

    Вот, как я сделал:

     public void update()
            {
                try
                {
                    if (MessageBox.Show("Ви дійсно хочете додати запис?", "Увага", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                    {
                        con.Open();
                        string update = "update MembersInfo set Group_ID=?, Region_ID=?, City=?, PIB=?, mobilePhone=?, eMail=?, VK_ID=?, FaceBook_ID=?, Odk_ID=?, GooglePlus_ID=? where MI_ID=?";
                        OleDbCommand com = new OleDbCommand(update, con);
                        com.CommandType = CommandType.Text;
                        com.Parameters.Add("@Group_ID", OleDbType.Integer).Value = Convert.ToInt32(txbx_Group_ID.Text);
                        com.Parameters.Add("@Region_ID", OleDbType.Integer).Value = Convert.ToInt32(cbx_IDRegion.SelectedValue.ToString());
                        com.Parameters.Add("@City", OleDbType.Char).Value = txbx_City.Text;
                        com.Parameters.Add("@PIB", OleDbType.Char).Value = txbx_PIB.Text;
                        com.Parameters.Add("@mobilePhone", OleDbType.Char).Value = txbx_Mobile.Text;
                        com.Parameters.Add("@eMail", OleDbType.Char).Value = txbx_eMail.Text;
                        com.Parameters.Add("@VK_ID", OleDbType.Char).Value = txbx_VK_ID.Text;
                        com.Parameters.Add("@FaceBook_ID", OleDbType.Char).Value = txbx_Fb.Text;
                        com.Parameters.Add("@Odk_ID", OleDbType.Char).Value = txbx_Odk.Text;
                        com.Parameters.Add("@GooglePlus_ID", OleDbType.Char).Value = txbx_Gplus.Text;
                        com.ExecuteNonQuery();
                        con.Close();
                    }
                }
                catch
                {
    
                }
            }
    Ну нечего не обновляется

    1 апреля 2013 г. 15:16
  • The code now seems to be OK. Did you verify that all your ID columns are of the right type? Also, for the Char column (are you sure about Char, BTW?) I would also add size as 3rd parameter.

    So, do you get an error on the ExecuteNonQuery command? How about you comment out try/catch for now and let it produce an error. Are you stepping through the code?


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    • Изменено Naomi N 1 апреля 2013 г. 15:46
    1 апреля 2013 г. 15:45
  • Код теперь, кажется, в порядке. Вы убедитесь, что все ваши столбцы ID имеют право типа? Кроме того, для Char колонки (вы уверены Char, кстати?) Я хотел бы также добавить размер, третий параметр.

    Итак, вы получили сообщение об ошибке на ExecuteNonQuery команду? Как насчет вас закомментировать попытаться / поймать на данный момент и пусть он об ошибке. Вы пошагового выполнения кода?


    Для каждого эксперта, есть равное и противоположное эксперт. - Закон Беккер Мой блог



    А что значит "все ваши столбцы ID имеют право типа?" типы колонок верны 
    1 апреля 2013 г. 16:00
  • Say, in your table the GooglePlus_Id identified as Integer, but in your code it's set as character. So, I suggest to double-check that each type is defined correctly in your table and code.

    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog

    1 апреля 2013 г. 17:59
  • Say, in your table the GooglePlus_Id identified as Integer, but in your code it's set as character. So, I suggest to double-check that each type is defined correctly in your table and code.

    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    1 апреля 2013 г. 18:13
  • Looks like you're now missing a space before where. Also, your UPDATE string is now not what you showed before. You showed all parameters with ? which I think it correct for OleDb But now you're showing UPDATE command like you're using SQL Server.

    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog

    1 апреля 2013 г. 18:16
  • Looks like you're now missing a space before where. Also, your UPDATE string is now not what you showed before. You showed all parameters with ? which I think it correct for OleDb But now you're showing UPDATE command like you're using SQL Server.

    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog

     private void btn_EditRecord_Click(object sender, EventArgs e)
            {
                string update = "update MembersInfo set Group_ID=?, Region_ID=?, City=?, PIB=?, mobilePhone=?, eMail=?, VK_ID=?, FaceBook_ID=?, Odk_ID=?, GooglePlus_ID=? where MI_ID=?";
                OleDbCommand com = new OleDbCommand(update, con);
                com.CommandType = CommandType.Text;
                com.Parameters.Add("@MI_ID", OleDbType.Integer).Value = Convert.ToInt32(txbx_MI_ID.Text);
                com.Parameters.Add("@Group_ID", OleDbType.Integer).Value = Convert.ToInt32(txbx_Group_ID.Text);
                com.Parameters.Add("@Region_ID", OleDbType.Integer).Value = Convert.ToInt32(cbx_IDRegion.SelectedValue.ToString());
                com.Parameters.Add("@City", OleDbType.Char, 55).Value = txbx_City.Text;
                com.Parameters.Add("@PIB", OleDbType.Char, 122).Value = txbx_PIB.Text;
                com.Parameters.Add("@mobilePhone", OleDbType.Char, 16).Value = txbx_Mobile.Text;
                com.Parameters.Add("@eMail", OleDbType.Char, 44).Value = txbx_eMail.Text;
                com.Parameters.Add("@VK_ID", OleDbType.Char, 55).Value = txbx_VK_ID.Text;
                com.Parameters.Add("@FaceBook_ID", OleDbType.Char, 55).Value = txbx_Fb.Text;
                com.Parameters.Add("@Odk_ID", OleDbType.Char, 55).Value = txbx_Odk.Text;
                com.Parameters.Add("@GooglePlus_ID", OleDbType.Char, 55).Value = txbx_Gplus.Text;
                con.Open();
                com.ExecuteNonQuery();
                con.Close();
            }

    Вот так сделал ошибки не появляются, ну не чего не обновляется
    1 апреля 2013 г. 18:30
  • Add parameters the same way they appear in the statement (since OleDb is using named parameters). In other words, move your first parameter to the end. Each parameter needs to be added in the same order they appear in the command. So, looks like all your parameters are in the correct order except for the first one which should be last.


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog

    1 апреля 2013 г. 18:47
  • Add parameters the same way they appear in the statement (since OleDb is using named parameters). In other words, move your first parameter to the end. Each parameter needs to be added in the same order they appear in the command. So, looks like all your parameters are in the correct order except for the first one which should be last.


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog

     private void btn_EditRecord_Click(object sender, EventArgs e)
            {
                string update = "update MembersInfo set Group_ID=?, Region_ID=?, City=?, PIB=?, mobilePhone=?, eMail=?, VK_ID=?, FaceBook_ID=?, Odk_ID=?, GooglePlus_ID=? where MI_ID=?";
                OleDbCommand com = new OleDbCommand(update, con);
                com.CommandType = CommandType.Text;
                com.Parameters.Add("@Group_ID", OleDbType.Integer).Value = Convert.ToInt32(txbx_Group_ID.Text);
                com.Parameters.Add("@Region_ID", OleDbType.Integer).Value = Convert.ToInt32(cbx_IDRegion.SelectedValue.ToString());
                com.Parameters.Add("@City", OleDbType.Char, 55).Value = txbx_City.Text;
                com.Parameters.Add("@PIB", OleDbType.Char, 122).Value = txbx_PIB.Text;
                com.Parameters.Add("@mobilePhone", OleDbType.Char, 16).Value = txbx_Mobile.Text;
                com.Parameters.Add("@eMail", OleDbType.Char, 44).Value = txbx_eMail.Text;
                com.Parameters.Add("@VK_ID", OleDbType.Char, 55).Value = txbx_VK_ID.Text;
                com.Parameters.Add("@FaceBook_ID", OleDbType.Char, 55).Value = txbx_Fb.Text;
                com.Parameters.Add("@Odk_ID", OleDbType.Char, 55).Value = txbx_Odk.Text;
                com.Parameters.Add("@GooglePlus_ID", OleDbType.Char, 55).Value = txbx_Gplus.Text;
                com.Parameters.Add("@MI_ID", OleDbType.Integer).Value = Convert.ToInt32(txbx_MI_ID.Text);
                con.Open();
                com.ExecuteNonQuery();
                con.Close();
            }


    1 апреля 2013 г. 19:02
  • Just wondering why did you mark your own response that quoted my answer? Should not you mark my as answer if it solved your problem?

    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog

    1 апреля 2013 г. 19:05