[英]How do I delete a row from a gridview and SQL using a webservice?
当我单击删除按钮时,我希望能够从 gridview 中删除一行。 但出于某种原因,该代码仅删除 gridview 中的行,而不是 SQL 数据库中的行。
这是因为我没有正确使用 web 方法 eliminarTarea?
我究竟做错了什么? 非常感谢!
按钮方式
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);
}
应用代码
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();
}
网络服务方法
[WebMethod]
public void webEliminarTarea(String titulo_tareas)
{
NegocioNotas auxNegocio = new NegocioNotas();
auxNegocio.eliminarCliente(titulo_tareas);
}
我不读西班牙语,所以我希望我能了解您的控件是如何连接的要点...您在哪里调用BindingSource的.EndEdit()
实际写回数据库?
另外,正如@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.