asp.net - Correct update statement c# -
i'd know if update sql statement correct, because have form wanna edit data. but, reason, form doesn't save updates , nothing happens in db.
this code-behind:
using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; using system.data.sqlclient; using system.data; public partial class edit : system.web.ui.page { sqlconnection con = new sqlconnection("data source=cassia-pc\\sqlexpress;initial catalog=clientes;integrated security=true"); protected void page_load(object sender, eventargs e) { string v = request.querystring["id"]; sqlcommand cmd = new sqlcommand("select idcliente, nmcliente, fantasia, cpf, cep, logradouro, numero, complemento, bairro, cidade, estado, telefone, celular, insestadual, insmunicipal, email, homepage, tbclientes.tpcliente, tbtipoclientes.idtipocliente, tbtipoclientes.nmtipocliente tbclientes inner join tbtipoclientes on tbclientes.tpcliente = tbtipoclientes.idtipocliente idcliente = '" + v + "'", con); try { con.open(); using (var reader = cmd.executereader()) { while (reader.read()) { txtid.text = reader["idcliente"].tostring(); txtnome.text = reader["nmcliente"].tostring(); txtfantasia.text = reader["fantasia"].tostring(); txtcpf.text = reader["cpf"].tostring(); txtcep.text = reader["cep"].tostring(); txtlogradouro.text = reader["logradouro"].tostring(); txtnumero.text = reader["numero"].tostring(); txtcomplemento.text = reader["complemento"].tostring(); txtbairro.text = reader["bairro"].tostring(); txtcidade.text = reader["cidade"].tostring(); txtestado.text = reader["estado"].tostring(); txttelefone.text = reader["telefone"].tostring(); txtcelular.text = reader["celular"].tostring(); txtinscestadual.text = reader["insestadual"].tostring(); txtinscmunicipal.text = reader["insmunicipal"].tostring(); txtemail.text = reader["email"].tostring(); txtsite.text = reader["homepage"].tostring(); } } cmd.executenonquery(); } catch (exception ex) { console.writeline(ex.message); } { con.close(); } } protected void btneditar_click(object sender, eventargs e) { string v = request.querystring["id"]; con.open(); sqlcommand cmd = new sqlcommand("update tbclientes set nmcliente = '"+txtnome.text+"', fantasia = '"+txtfantasia.text+"', cpf = '"+txtcpf.text+"', cep = '"+txtcep.text+"', logradouro = '"+txtlogradouro.text+"', numero = '"+txtnumero.text+"', complemento = '"+txtcomplemento.text+"', bairro = '"+txtbairro.text+"', cidade = '"+txtcidade.text+"', estado = '"+txtestado.text+"', telefone = '"+txttelefone.text+"', celular = '"+txtcelular.text+ "', insestadual = '"+txtinscestadual.text+"', insmunicipal = '"+txtinscmunicipal.text+"', email = '"+txtemail.text+"', homepage = '"+txtsite.text+"' idcliente = '" + v + "'", con); try { cmd.executenonquery(); } catch(exception ex) { console.writeline(ex.message); } { con.close(); } } }
i'm pretty sure problem is:
where idcliente = '" + v + "'"
because client id numeric field in database want treat such:
where idcliente = " + v
as blorgbeard mentions need use parameterised commands protect against sql injection attack. solve issues such textboxes containing apostrophes , etc cause update fail.
Comments
Post a Comment