Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update Datagrid

Status
Not open for further replies.

hsingh1981

Programmer
Apr 8, 2008
56
GB
Hi Everyone,

I'm having trouble or finding an easy way to update my datagrid back to the sql table 2005.

I can pull the data from sql to datagrid....but when i change the figures in datagrid....i would like it to update the sql table afterwards? Could someone give me an example?

This is how i populate my datagrid.

Code:
Private Sub GetTempKPI()

        Dim sql As String

        sql = "SELECT Temp_KPIData.* "
        sql = sql + "FROM Temp_KPIData "
        sql = sql + "ORDER BY Temp_KPIData.KPI_ID;"

        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(connectionstring)
        Dim cmd As New OleDb.OleDbCommand(sql, conn)
        Dim adp As New OleDb.OleDbDataAdapter(cmd)
        Dim ds As New DataSet()
        Dim dt As DataTable = ds.Tables.Add("KPIData")
        Dim recordcount As Integer

        Try
            conn.Open()
        Catch ex As Exception
            MsgBox("ERROR: Database cannot be opened. See system administrator. MESSAGE: " + ex.Message.ToString, MsgBoxStyle.Critical, "APPLICATION ERROR")
            Exit Sub
        End Try

        Try
            adp.Fill(ds, "KPIData")
            recordcount = dt.Rows.Count
            KPI_DGView.DataSource = dt

        Catch ex As Exception
            MsgBox("ERROR: Table Temp_KPIData table cannot be queried. See system administrator. MESSAGE: " + ex.Message.ToString, MsgBoxStyle.Critical, "APPLICATION ERROR")
            Exit Sub
        Finally
            conn.Close()
        End Try



    End Sub

many thanks
 
Your DataAdapter needs more than just a .SelectCommand. It needs Update, Insert and Delete commands.

See here:


Also, you need to tell your DataAdapter when to pushes changes back to your database:


I would suggest googling or searching MSDN for an ADO.Net tutorial for a complete understanding of how ADO.Net works.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top