modify and delete record from gridview vb.net

0

I'm doing a CRUD with vb.net in a web application, only I try to place unlink in a Gridview to update and delete but I do not know how to do it, the gridview already filled it with data as follows:

my function where I make the query and return the results

    Public Function consultar() As DataTable
    Dim cn = "Data Source=SQLEXPRESS;Initial Catalog=curso;User Id=sa;Password=****"

    Dim conexion As New SqlConnection(cn)
    conexion.Open()

    Dim cmd As New SqlCommand()
    cmd.Connection = conexion
    cmd.CommandType = CommandType.Text
    cmd.CommandText = "SELECT*FROM TELEFONO"

    Dim da As New SqlDataAdapter
    da.SelectCommand = cmd
    Dim dt As New DataTable
    da.Fill(dt)

    Return dt
End Function

my codebehind

Public Class Consultar
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim muestraDatos As New Operaciones

    GridView1.DataSource = muestraDatos.consultar()
    GridView1.DataBind()


End Sub

End Class

my gridview on the aspx page

    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Consultar.aspx.vb" Inherits="Entrevista.Consultar" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>
    <div>
    
    </div>
    </form>
</body>
</html>
    
asked by Ivxn 17.09.2016 в 23:29
source

1 answer

2

What I would personally do is create methods to update or delete records, after that I would finally call to consult () after doing one of the two operations so that the datagridview refreshes. For example:

Public sub ActualizarRegistro(ByVal columna as String, ByVal A as String, ByVal B as String)
Dim cn = "Data Source=SQLEXPRESS;Initial Catalog=curso;User Id=sa;Password=****"

 Dim conexion As New SqlConnection(cn)
 conexion.Open()

Dim cmd As New SqlCommand()
cmd.Connection = conexion
cmd.CommandType = CommandType.Text
cmd.CommandText = "UPDATE TELEFONO SET " & columna & "=@A WHERE " & columna & "=@B"
cmd.Parameters.AddWithValue(A,"@A")
cmd.Parameters.AddWithValue(B,"@B")
cmd.ExecuteNonQuery()
conexion.Close()
cmd.Dispose()
conexion.Dispose()
End Sub

This time here (the strings followed with @), in case you did not know, are called parameterized queries . They are faster and safer (they protect you from the famous SQL INJECTION ), so always use them to write them. (The only values that can not be passed as parameters are the columns, so there is no other way to concatenate them)

As the first parameter you pass the column of the table, the value A is the new data that you are going to enter and the data B is the existing one. You will need to capture the old data when clicking on a cell in the datagridview (At the same time you would also have to capture the name of the column for the "column" parameter) this could be achieved with the datagridview events (CellClic or CellContentClic as you wish) and you could capture them like this:

Dim datoViejo As String = DataGridView1.CurrentCell.Value
Dim nombreColumna As String = DataGridView1.CurrentRow.HeaderCell.Value

Well, with another event in the datagridview you could run the method you just wrote (the update) and then I call the function you created to consult (check ()) so a "refresh" of the datagridview is done ... I would do it with the CellLeave event. Basically the process would be like this:

Click on the cell you wish to modify, at this time you click on the page, capture the name of the column and the data that contains the cell.

You have clicked on the cell, you have the focus on it, it is time to write the new data to be replaced.

By clicking elsewhere, the focus in the cell is lost and the execution of the method written above will begin immediately.

TIPS: Seeing that you are creating the connection in a function and it is not a class attribute that you can reuse in different functions or methods, it is recommended that when you finish using it use the Dispose () to free resources on the page. This also has SqlCommand, SqlDataAdapter , even the same DataTables when you finish using it is recommended to delete it ... always when you finish using something, delete it.

Lastly, you have to open the connection to work, right? Likewise when you finish doing something with the database, close the connection.

Your function that you have posted I would write it in the following way:

Public Function consultar() As DataTable
Dim cn = "Data Source=SQLEXPRESS;Initial Catalog=curso;User Id=sa;Password=****"

Dim conexion As New SqlConnection(cn)
conexion.Open()

Dim cmd As New SqlCommand()
cmd.Connection = conexion
cmd.CommandType = CommandType.Text
cmd.CommandText = "SELECT*FROM TELEFONO"

Dim da As New SqlDataAdapter
da.SelectCommand = cmd
Dim dt As New DataTable
da.Fill(dt)

'Liberacion de recursos'
da.Dispose()
cmd.Dispose()
cn.Close()
cn.Dispose()

Return dt
End Function

Lastly I do not think you need a delete statement ... if you need it the same, the mechanics of the query would not change anything, the only thing that would change is the value of CommandText (that you would write here the DELETE clause). I hope this answer has illuminated you in what you want.

If this answer has served you, please do not hesitate to mark it:)

Greetings

    
answered by 18.09.2016 / 02:53
source