Motoko055

[ASP.NET] Inserir e Update

Jan 21st, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.64 KB | None | 0 0
  1. try
  2.                 {
  3.                     foreach (GridViewRow row in GridView1.Rows)
  4.                     {
  5.                         if (row.RowType == DataControlRowType.DataRow)
  6.                         {
  7.                             CheckBox chkRow = (row.Cells[0].FindControl("CheckBox1") as CheckBox);
  8.                             if (chkRow.Checked)
  9.                             {
  10.                                 int Id = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value.ToString());
  11.                                 SqlCommand cmd = new SqlCommand("UPDATE dbo.registo SET nome='" + txtNome.Text + "',apelido='" + txtApelido.Text + "',titulo='" + txtTitulo.Text + "',cidade='" + txtCidade.Text + "',pais='" + txtPais.Text + "',Imagem='" + "~/imagens/" + FileUpload1.FileName + "' WHERE Id=@id", conn);
  12.  
  13.                                 cmd.Parameters.Add("@Id", SqlDbType.Int).Value = Id.ToString();
  14.                                 cmd.ExecuteNonQuery();
  15.                                 Response.Redirect("Main.aspx");
  16.                             }
  17.                         }
  18.                     }
  19.  
  20.                 }
  21.                 catch
  22.                 {
  23.                     comm.CommandType = System.Data.CommandType.Text;
  24.                     comm.CommandText = "INSERT INTO Registo(Id,Nome,Apelido,Titulo,Cidade,Pais,Imagem) VALUES ('" + id + "','" + txtNome.Text + "','" + txtApelido.Text + "','" + txtTitulo.Text + "','" + txtCidade.Text + "','" + txtPais.Text + "','" + local.ToString() + "')";
  25.  
  26.                     comm.ExecuteNonQuery();
  27.                     Atualizar();
  28.                     Response.Redirect("Main.aspx");
  29.                 }
Add Comment
Please, Sign In to add comment