简体   繁体   English

如何使用网络服务从 gridview 和 SQL 中删除一行?

[英]How do I delete a row from a gridview and SQL using a webservice?

I want to be able to delete a row from a gridview when I click on a delete button.当我单击删除按钮时,我希望能够从 gridview 中删除一行。 But for some reason, the code only deletes the rows from the gridview but not the from the SQL DataBase.但出于某种原因,该代码仅删除 gridview 中的行,而不是 SQL 数据库中的行。

It's because i'm not using the web method eliminarTarea correctly?这是因为我没有正确使用 web 方法 eliminarTarea?

What am I doing wrong?我究竟做错了什么? Many Thanks!非常感谢!

button method按钮方式

            try
            {
                if (MessageBox.Show("¿Está seguro de querer eliminar esta nota?","Message",MessageBoxButtons.YesNo,MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    int rows = DataGVTareas.RowCount;
                    for(int i = rows - 1; i >= 0; i--)
                    {
                        if (DataGVTareas.Rows[i].Selected) 
                        {
                            ServiceMantenedorCliente.WebServiceMantenedorClienteSoapClient auxNegocio = new ServiceMantenedorCliente.WebServiceMantenedorClienteSoapClient();
                            auxNegocio.webEliminarTarea(titulotareasDataGridViewTextBoxColumn.ToString());
                            tareasBindingSource.RemoveAt(DataGVTareas.Rows[i].Index);
                        }
                    }
                     

                    
                }
                //ServiceMantenedorCliente.WebServiceMantenedorClienteSoapClient auxNegocio = new ServiceMantenedorCliente.WebServiceMantenedorClienteSoapClient();
                if (String.IsNullOrEmpty(this.DataGVTareas.ToString()))
                {
                    MessageBox.Show("No hay datos por eliminar ", "NoteIt", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                }
                else
                {
                    //auxNegocio.webEliminarCliente(DataGVTareas.Rows[i]);
                    
                    MessageBox.Show("Los datos han sido eliminados", "NoteIt", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show("Se ha producido un error inesperado, por favor reinicie la aplicación e intente nuevamente" + ex.Message, "NoteIt", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

app code应用代码

        public void eliminarTarea(String titulo_tareas)
        {
            this.ConexionTareas();
            this.Conec1.CadenaSQL = "DELETE FROM " + this.Conec1.NombreTabla
                                    + " WHERE titulo_tareas = '" + titulo_tareas + "';";
            this.Conec1.EsSelect = false;
            this.Conec1.Conectar();
        }

webservice method网络服务方法

        [WebMethod]
        public void webEliminarTarea(String titulo_tareas)
        {
            NegocioNotas auxNegocio = new NegocioNotas();
            auxNegocio.eliminarCliente(titulo_tareas);
        }

I don't read spanish so I hope I get the gist of how your controls are wired up... where do you call the .EndEdit() for the BindingSource which actually writes back to the database?我不读西班牙语,所以我希望我能了解您的控件是如何连接的要点...您在哪里调用BindingSource.EndEdit()实际写回数据库?

Also, as @Larnu mentioned, fix your SQL query:另外,正如@Larnu 提到的,修复您的 SQL 查询:


public void eliminarTarea(String titulo_tareas)
{
    this.ConexionTareas();

    //You'll have to fix this up because I don't know what "Conec1" is.
    //This demonstrates putting your text values into a parameter instead
    //of string concatenation.
    //Concatenation of NombreTabla is also a vulnerability but I don't know how to fix that one 
    //because it is a table name and I don't know where NobreTabla is sourced from.
    string sqlTxt = "DELETE FROM " + this.Conec1.NombreTabla
        + " WHERE titulo_tareas = @TituloTareas;";
    SqlCommand cmd = new SqlCommand([sqlConnection here], sqlTxt);
    cmd.Parameters.AddWithValue("@TituloTareas", titulo_tareas);
    this.Conec1.SqlCommand = cmd;  //Does this even work with Conec1?

    this.Conec1.EsSelect = false;
    this.Conec1.Conectar();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM