|
-
May 2nd, 2008, 01:36 PM
#1
Thread Starter
New Member
Dataset does not update database
Using VB.net 2.0
I have created a form in to which i have dragged a details view of a table from a database.

When i add a new client and save, close the program and open it, the new client is present, if i close VS2005 and open the DB there is no new record, open VS2005 again and the new client record has disappeared.
I think the data set is not updating the database. I have the database properties set to copy if newer.
I have tried using the built in toolstrip to the same outcome.
The code for my form:
Code:
Public Class frmClient
Private Sub frmClient_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'DsGT2000New.tblClient' table. You can move, or remove it, as needed.
Me.TblClientTableAdapter.Fill(Me.DsGT2000New.tblClient)
'Check if User name = Admin if so enable the delete button
If frmPass.txtUserName.Text = "Admin" Then
btnDel.Enabled = True
End If
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
'close program
Me.Close()
End
End Sub
Private Sub btnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click
Me.TblClientBindingSource.MovePrevious()
End Sub
'-----------------------------------------------------------------------------------------------------------------------------
'-----------------------------------------------------------------------------------------------------------------------------
'Procedure :btnNext
'Project :GT2000Project
'Version :1.0
'Date :18/04/2008
'Author :Craig Hendley
'Description :button to take the user to the next record
' :
'-----------------------------------------------------------------------------------------------------------------------------
'-----------------------------------------------------------------------------------------------------------------------------
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
Me.TblClientBindingSource.MoveNext()
End Sub
'-----------------------------------------------------------------------------------------------------------------------------
'-----------------------------------------------------------------------------------------------------------------------------
'Procedure :btnAddNew
'Project :GT2000Project
'Version :1.0
'Date :18/04/2008
'Author :Craig Hendley
'Description :button to start a blank record for new input
' :
'-----------------------------------------------------------------------------------------------------------------------------
'-----------------------------------------------------------------------------------------------------------------------------
Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNew.Click
Me.TblClientBindingSource.AddNew()
End Sub
'-----------------------------------------------------------------------------------------------------------------------------
'-----------------------------------------------------------------------------------------------------------------------------
'Procedure :btnDel
'Project :GT2000Project
'Version :1.0
'Date :18/04/2008
'Author :Craig Hendley
'Description :button to delete record
' :
'-----------------------------------------------------------------------------------------------------------------------------
'-----------------------------------------------------------------------------------------------------------------------------
Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
MessageBox.Show("Are You Sure You Wish To Delete This Record?", "Confirm ", MessageBoxButtons.OKCancel, MessageBoxIcon.Question)
End Sub
'-----------------------------------------------------------------------------------------------------------------------------
'-----------------------------------------------------------------------------------------------------------------------------
'Procedure :CIDTextBox_KeyPress
'Project :GT2000Project
'Version :1.0
'Date :18/04/2008
'Author :Craig Hendley
'Description :Limits allowed character presses to "C", Numeric values, tab and backspace
' :
'-----------------------------------------------------------------------------------------------------------------------------
'-----------------------------------------------------------------------------------------------------------------------------
Private Sub CIDTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles CIDTextBox.KeyPress
If (e.KeyChar <> "C") Then
If (e.KeyChar < "0" Or e.KeyChar > "9") Then
If (AscW(e.KeyChar) <> AscW(ControlChars.Back) AndAlso AscW(e.KeyChar) <> AscW(ControlChars.Tab)) Then
e.Handled = True
End If
End If
End If
End Sub
'-----------------------------------------------------------------------------------------------------------------------------
'-----------------------------------------------------------------------------------------------------------------------------
'Procedure :DOBDateTimePicker_ValueChanged
'Project :GT2000Project
'Version :1.0
'Date :18/04/2008
'Author :Craig Hendley
'Description :Limits allowed character presses to "C", Numeric values, tab and backspace
' :
'-----------------------------------------------------------------------------------------------------------------------------
'-----------------------------------------------------------------------------------------------------------------------------
Private Sub DOBDateTimePicker_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DOBDateTimePicker.ValueChanged
Dim d As Date = Date.Now
d = New Date(d.Year - 18, d.Month, d.Day)
If DOBDateTimePicker.Value <= d Then
Me.err1.SetError(DOBDateTimePicker, "")
Else
Me.err1.SetError(DOBDateTimePicker, "Must Be over 18!")
Me.DOBDateTimePicker.Focus()
End If
End Sub
'-----------------------------------------------------------------------------------------------------------------------------
'-----------------------------------------------------------------------------------------------------------------------------
'Procedure :btnSav
'Project :GT2000Project
'Version :1.0
'Date :18/04/2008
'Author :Craig Hendley
'Description :button to save date in the dataset to the database
' :
'-----------------------------------------------------------------------------------------------------------------------------
'-----------------------------------------------------------------------------------------------------------------------------
Private Sub btnSav_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSav.Click
Me.Validate()
Me.TblClientBindingSource.EndEdit()
Me.TblClientTableAdapter.Update(Me.DsGT2000New.tblClient)
End Sub
End Class
This is really worrying me, i need to sort it out ASAP so anything you guys can advise me on would be much appreciated
Last edited by ABOU; May 2nd, 2008 at 01:44 PM.
-
May 2nd, 2008, 02:48 PM
#2
Re: Dataset does not update database
Does your table have a primary key defined?
I've never used a tableAdapter I'm afraid, I find the graphical method of linking data far too confusing.
-
May 2nd, 2008, 02:58 PM
#3
Re: Dataset does not update database
Is your connection string referencing your MDF in the |DataDirectory|?
-
May 2nd, 2008, 04:48 PM
#4
Thread Starter
New Member
Re: Dataset does not update database
My table does have a primary key.
Mendhak: How would i check if the connection string is referencing the MDF in the |DataDirectory|?
I dont know what an MDF is, or where to find the DataDirectory sorry
-
May 3rd, 2008, 01:29 AM
#5
Re: Dataset does not update database
When you add a database to your project it creates an MDF file and places it in your source folder, i.e. the project folder containing all your VB code files.
When you build your project, which happens each time you run it in the debugger, that MDF file is copied to the output folder along with your compiled EXE. It is that copy that your application interacts with.
By default, the database file is copied to the output folder EVERY TIME you build, therefore all the changes you make during one debugging session will be erased as soon as you build again.
Follow the first link in my signature for an explanation of how to change this behaviour.
Note also that your TableAdapters each wrap an SqlConnection that has a ConnectionString that controls how it connects to your database. That connection string contains an AttachDbFileName property whose value is the path to your database file. By default it will be something like "|DataDirectory|\MyDatabase.mdf". The "|DataDirectory|" part is interpreted by the Framework at run time and replaced with the actual folder path. In this way you don't have to change your connection string no matter where you install your application.
-
May 3rd, 2008, 06:24 AM
#6
New Member
Re: Dataset does not update database
 Originally Posted by jmcilhinney
When you add a database to your project it creates an MDF file and places it in your source folder, i.e. the project folder containing all your VB code files.
When you build your project, which happens each time you run it in the debugger, that MDF file is copied to the output folder along with your compiled EXE. It is that copy that your application interacts with.
By default, the database file is copied to the output folder EVERY TIME you build, therefore all the changes you make during one debugging session will be erased as soon as you build again.
Follow the first link in my signature for an explanation of how to change this behaviour.
Note also that your TableAdapters each wrap an SqlConnection that has a ConnectionString that controls how it connects to your database. That connection string contains an AttachDbFileName property whose value is the path to your database file. By default it will be something like "|DataDirectory|\MyDatabase.mdf". The "|DataDirectory|" part is interpreted by the Framework at run time and replaced with the actual folder path. In this way you don't have to change your connection string no matter where you install your application.
i have the seem ISSUE but seting Copy to Output Directory to Copy if newer not helps
-
May 3rd, 2008, 06:54 AM
#7
Re: Dataset does not update database
Try setting it to Do Not Copy and see if that works.
-
May 5th, 2008, 10:30 AM
#8
Thread Starter
New Member
Re: Dataset does not update database
Thank you, you have no idea the panic was in just then!!
I did not know that the database was copied again from the application directory to the bin folder.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|