Results 1 to 9 of 9

Thread: datagrid update

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2001
    Location
    Lebanon,Beirut
    Posts
    48

    datagrid update

    hi there, im using the following code to load data into a datagrid and update it, but the update part isnt working fine, any ideas?

    Dim conn As New OleDbConnection(connection_str)
    conn.Open()
    sql_str = "select * from num"

    Dim objCommand As New OleDbCommand(sql_str, conn)

    Dim DS As New DataSet()

    data_adapter = New OleDbDataAdapter(objCommand)
    data_adapter.Fill(ds, "to")

    DataGrid1.DataSource = ds.Tables("to")

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    'Dim ds As New DataSet()

    If ds.HasChanges() Then
    MsgBox("yes")
    End If



    the error msg is :
    An unhandled exception of type 'System.NullReferenceException' occurred in datagridexple.exe

    Additional information: Object reference not set to an instance of an object.


    thank you.
    There is,we are,abou il zoulouf,mijana w mal3ona

  2. #2
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Your code has some problems:
    1-I dont see any need for 'conn.Open()' in second line.
    2- What you mean by update, if you mean to update the database, i see no update code.
    3-In click event 'ds' is not decalred, so you face Nullreference excpetion.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2001
    Location
    Lebanon,Beirut
    Posts
    48
    im getting the error in the click code:

    if ds.haschanges() ----

    ds is allready declared in form_load,
    im checking if the dataset has changes then im gonna update, and the error is keep on showing!

    thank u.
    There is,we are,abou il zoulouf,mijana w mal3ona

  4. #4
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    I think you need to learn more about the scope of variables. The variable not declared as public in a private Sub will remain private to that sub and not accecible by others.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  5. #5
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Then ds is not accessible from the click event . Better you declare it public .

  6. #6

    Thread Starter
    Member
    Join Date
    Jan 2001
    Location
    Lebanon,Beirut
    Posts
    48
    thank u all for yr replies, it just was stupid from me not to realize it!

    cld u tell tell me whats the code behind updating the datagrid on the button_click event. ill make sure i buy a dot net book but in the mean while i cld really use any available help.
    thank you
    There is,we are,abou il zoulouf,mijana w mal3ona

  7. #7
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    There are some ways to perform an update. But all of those need an update/insert command. Just as a quciky first declare your adapter and dataset public. then use a command builder to build the adapters update, delete, and insert commands. then u can update yourt database using data_adapter.Update(ds, "to")

    Search this forum for command builder and you will find how to use it. Use the search below the posts not the one in the above blue band.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  8. #8
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    if you have updated your access table / dataset then just doing this :
    VB Code:
    1. data_adapter.Fill(ds, "to")'/// fill with the updated stuff
    2. DataGrid1.DataSource = ds.Tables("to")
    should update the stuff in the datagrid.
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  9. #9
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Oh my bad, I thought he is talking of updating the database.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width