Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Problem Updating a Database

Posted on 2009-07-07
5
Medium Priority
?
349 Views
Last Modified: 2013-12-25
I am having a problem updating information to a MS Access Database.

Before i code this much more, i simply have a link to a MS Access Database, a DataGridView, a TextBox, and a Button on Form1.

The database is linked and when running the program it pulls the columns from the database and populates the DataViewGrid. This DataViewGrid will not allow changes, i want the changes to occur in textboxs and that information then updated back to the database.

i am able to make the DataGridView change with the updated information from Textbox1, however that information is not being written back to the actual database and i can't seem to figure out why..

The code below is my source code for all of Form1, and Button1_Click.
Public Class Form1
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'CsvFileDS.CSVFILE' table. You can move, or remove it, as needed.
        Me.CSVFILETableAdapter.Fill(Me.CsvFileDS.CSVFILE)
 
    End Sub
 
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 
        Dim TempTable As DataTable = CsvFileDS.Tables("CSVFILE")
        Dim NewEntry As DataRow = TempTable.NewRow
 
        NewEntry("DOS") = TextBox1.Text
 
        TempTable.Rows.Add(NewEntry)
 
    End Sub
End Class

Open in new window

0
Comment
Question by:jokeefe1130
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 28

Expert Comment

by:Ark
ID: 24809179
Me.CSVFILETableAdapter.Update(YourDataSetNameHere)
0
 

Author Comment

by:jokeefe1130
ID: 24810181
OK, that worked and i was able to update the information in the database, but now i have another problem... "NullReferenceException was unhandled", "Object reference not set to an instance of an object"

        Dim TempTable As DataTable = Database1DataSet.Tables("CsvFile")
        Dim NewEntry As DataRow = TempTable.NewRow '<--ERROR IS HERE
 
        NewEntry("DOS") = "2009-07-08"
        NewEntry("LOS") = "BLS"
        NewEntry("PTNAME") = "DOE, JOHN"
 
        TempTable.Rows.Add(NewEntry)
 
        CsvTableTableAdapter.Update(Database1DataSet.CsvTable)

Open in new window

0
 
LVL 28

Accepted Solution

by:
Ark earned 2000 total points
ID: 24810294
Try
Dim TempTable As DataTable = Database1DataSet.CSVFILE
I suggest using bindingsource instead of DataTables
'********Can be set in design time**********
dim bsCSVFILE As New BindingSource
bsCSVFILE.DataSource = Database1DataSet
bsCSVFILE.DataMember="CsvFile"
'*********************
       dim drv As DataRowView=bsCSVFILE..AddNew()
       drv("DOS") = "2009-07-08"
       drv("LOS") = "BLS"
       drv("PTNAME") = "DOE, JOHN"
bsCSVFILE.EndEdit
Me.CSVFILETableAdapter.Update(Database1DataSet.CsvTable)
0
 

Author Comment

by:jokeefe1130
ID: 24810609
Your Solution worked prefectly!

I will update the "Accept Solutions" shortly,

If you able to, i am also in need of a few other specifics with this same database.
1. Update an existing row/cell in the row
2. Delete the row

Your help is greatly appreciated, i havent worked with programming in many years and so much of the  syntax and funcations have changed. Last time i worked with programming, the database interface was "recordset..." VB '97
 
Thank you very much
0
 
LVL 28

Expert Comment

by:Ark
ID: 24811158
Hi
The .net database approach is slightly different then old VB. Then main idea is:
1. Connect to database, get info (TableAdapter(s).Fill), disconnect
2. Working with dataset (like database mirror) - adding new records, modifying, deleting in memory
3. Finally - connect to database again, update changes (tableAdapter(s).Update), disconnect.
    3.1 Dataset.GetChanges gives changed records
    3.2 Dataset.AcceptChanges marks those records as unchanged to repeat 2-3 steps

So, if using BindingSourse(s):
1. Connect to DB (Fill adapters)
2. set bindingsource(s) DataSource(=DataSet) and DatatMember(s)(=TableName(s))
3. Work wit bindingsourse(s)
   3.1. Navigate: bs.Position=bs.Find("LOS","BLS")
   3.2 Add New (see my previous post)
   3.3 Change Cells: bs("PTNAME")="whatever", bs("otherColumn") = "newString"
   3.4 Delete bs.DeleteCurrent (or bs.Delete(bs(bs.Find("LOS","BLS")))
   3.4. Accept (bs.EndEdit)  or discard (bs.CancelEdit) in-memory changes
4. Update DataBase (TableAdapter.Update)
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question