Question How to Save DataGridView Data Changes

zakaryahelal

Member
Joined
Mar 23, 2009
Messages
6
Programming Experience
Beginner
Hi,

I have a Form called FrmOrderSource which contains a datagridview. I have a AgibOrdersDataSet which contains the data obtained from an SQL server from the AgibOrders table. i can get the datagrid to display the value in the dataset using a table adapter.

instead of displaying the whole data, i put a combobox which users can filter data by BrokerName, and which i pass on to the fill function of the table adapater as a parameter. This seems to be working fine as well.

What i need to do now is,how to save or update data changes when the users update the data on datagrideview

this my code

VB.NET:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Windows.Forms
Imports System.Globalization

Public Class FrmOrderSource
    Dim strSQL As String = "SELECT DISTINCT BrokerUsers.BrokerUserID ,BrokerUsers.BrokerUserName , BrokerUsers.FullName, BrokerUsers.Deleted, Orders.transfer FROM  BrokerUsers BrokerUsers INNER JOIN Orders Orders ON BrokerUsers.BrokerUserID = Orders.BrokerUserID WHERE Orders.transfer = 0 and BrokerUsers.Deleted = 'false' "
    Dim Broda As New System.Data.SqlClient.SqlDataAdapter(strSQL, OTSconn)
    Dim Brods As New DataSet
    Private da As New SqlDataAdapter()
    Private ds As New DataSet()
    Dim Bro As Integer
    Dim txtFromDate As String
    Dim txtToDate As String
    Dim cm As New SqlCommand("SELECT dbo.AgibOrders.Date, dbo.AgibOrders.OrderID, dbo.AgibOrders.Time, dbo.AgibOrders.BrokerUserName, dbo.ModeTyp.Text AS Mode, dbo.BuySellTyp.Text AS BuySell, dbo.AgibOrders.UserID, dbo.AgibOrders.FullName, dbo.AgibOrders.Symbol, dbo.AgibOrders.Quantity, dbo.AgibOrders.Price, dbo.AgibOrders.CancelTime, dbo.AgibOrders.Cancelled, dbo.AgibOrders.Source, dbo.AgibOrders.Notes FROM dbo.AgibOrders INNER JOIN dbo.BuySellTyp ON dbo.AgibOrders.BuySell = dbo.BuySellTyp.Type INNER JOIN dbo.ModeTyp ON dbo.AgibOrders.Mode = dbo.ModeTyp.Type", AGIBconn)
    Dim cm1 As New SqlCommand("SELECT dbo.AgibOrders.Date, dbo.AgibOrders.OrderID, dbo.AgibOrders.Time, dbo.AgibOrders.BrokerUserName, dbo.ModeTyp.Text AS Mode, dbo.BuySellTyp.Text AS BuySell, dbo.AgibOrders.UserID, dbo.AgibOrders.FullName, dbo.AgibOrders.Symbol, dbo.AgibOrders.Quantity, dbo.AgibOrders.Price, dbo.AgibOrders.CancelTime, dbo.AgibOrders.Cancelled, dbo.AgibOrders.Source, dbo.AgibOrders.Notes FROM dbo.AgibOrders INNER JOIN dbo.BuySellTyp ON dbo.AgibOrders.BuySell = dbo.BuySellTyp.Type INNER JOIN dbo.ModeTyp ON dbo.AgibOrders.Mode = dbo.ModeTyp.Type where  dbo.AgibOrders.BrokerUserID = @P1", AGIBconn)

    Private Sub Trading_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        AGIBconn.ConnectionString = AGIBconstring
        OTSconn.ConnectionString = OTSconstring

        OrdersDGV()

        'Fill Broker ComboBox
        Broda.Fill(Brods, "strSQL")
        If AGIBconn.State = ConnectionState.Open Then AGIBconn.Close()
        AGIBconn.Open()
        With Me.Broker
            .DataSource = Brods.Tables("strSQL")
            .DisplayMember = "BrokerUserName"
            .ValueMember = "BrokerUserID"
            .SelectedIndex = -1
        End With
    End Sub

    Private Sub CmdRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdRefresh.Click
        OrdersDGV()
    End Sub
    Private Sub OrdersDGV()

        If Broker.SelectedValue = Nothing Then
            da.SelectCommand = cm
        Else : da.SelectCommand.Parameters.Add("@P1", SqlDbType.Int).Value = Broker.SelectedValue
            da.SelectCommand = cm1
        End If
        da.Fill(ds, "AgibOrders")
        If AGIBconn.State = ConnectionState.Open Then AGIBconn.Close()
        AGIBconn.Open()
        With Me.OderSourceDGV
            .DataSource = ds.Tables(0)
        End With
        da.Dispose()
        AGIBconn.Close()
    End Sub

    Private Sub CmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdSave.Click
        Dim dsChanges As New DataSet
        da.Fill(ds, "AgibOrders")
        Me.OderSourceDGV.BindingContext(ds).EndCurrentEdit()
        dsChanges = ds.GetChanges(DataRowState.Modified)
        Me.da.Update(dsChanges, "AgibOrders")

    End Sub
End Class

when I press Save Btn it give me this error

PHP:
The SelectCommand property has not been initialized before calling 'Fill'.



Any suggestions would be much appreciated.

thanks,

Z. Helal
 
Please read the DW2 link in my signature, section Creating A Simple Data App
Also read the DNU link
 

Latest posts

Back
Top