vivek padelkar vivek padelkar - 4 months ago 16 Question

how do i get data of the selected row in new form and user can edit it?

I have form that contain a datagrid I want that,
When user selected any row of the datagrid and click's to the edit button
the data of the datagrid will opens in a new form and user can edit it and save.

sorry but i didnt get logic because Im entry level programmer.

How do i write a code on my '

' button
I have 9 fields on my primary form in datagrid.

I am using
sql server 2010 and vb2008


You really can just Google search "SQL SELECT and UPDATE" queries, however:

To select it, you need to use a query to select all of the information associated to that unique ID (Which you should have?).

Then, load it into a DataTable. Each control will be assigned it's value using the relevant DataTable data.

Then an update query will be run when you press Save/Update to update it.

Example SELECT Query

Dim Dt As New DataTable

    Using Da = New OleDbDataAdapter("SELECT * FROM Your_Table WHERE uniqueID=?", connectionstring)
        Da.SelectCommand.Parameters.Add("@ID", OleDbType.Integer).Value = IDvariable
    End Using

Then you need to use the DataAdapter to fill in the form controls, for example:

txtFirstName.Text = Dt.Rows(0).Item("firstName")

Then an UPDATE query

        Dim Dc As New OleDbCommand
        Dc.Connection = connectionstring
        Dc = New OleDb.OleDbCommand("UPDATE Your_Table SET column_1 = ?, column2 = ? WHERE uniqueID=?", connectionstring)
        Dc.Parameters.Add("@column1", OleDbType.DataType).Value = control1.Text
        Dc.Parameters.Add("@column2", OleDbType.DataType).Value = control2.Text

        Dc.Parameters.Add("@uniqueID", OleDbType.Integer).Value = uniqueID


        MsgBox("Edit Successful")

    Catch ex As Exception
        MsgBox("Failed to update data, refer to error log")

    End Try