Newbie with a Problem

N

No_So_Clever

Hi There,

Hope Somebody dosn`t mind Helping a Newbie?

I have the Follwoing Code:

Private Sub frmSystemUsers_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'File Datasets
Try
dsSystemUsers.EnforceConstraints = False
Try
Me.odcSystemUsers.Open()
odaSystemUsers.Fill(dsSystemUsers)
cmSystemUsers = DirectCast(BindingContext(dvSystemUsers), CurrencyManager)
mlLoading = True
Catch fillException As System.Exception
Throw fillException
Finally
Me.odcSystemUsers.Close()
End Try
Catch eLoad As System.Exception
System.Windows.Forms.MessageBox.Show(eLoad.Message)
Finally
mlLoading = False
End Try
'End of Fill Datasets
End Sub

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAdd.Click
cmSystemUsers.AddNew()
txtFirstname.Enabled = True
txtSurname.Enabled = True
txtUsername.Enabled = True
txtPassword.Enabled = True
End Sub
Private Sub btnAccept_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAccept.Click
If txtFirstname.Text = "" Then
ValidDataMessage = "First Name Required"
txtFirstname.Focus()
ElseIf txtSurname.Text = "" Then
ValidDataMessage = "Surname Required"
txtSurname.Focus()
ElseIf txtUsername.Text = "" Then
ValidDataMessage = "Username Required"
txtUsername.Focus()
ElseIf txtPassword.Text = "" Then
ValidDataMessage = "Password Required"
txtPassword.Focus()
If ValidDataMessage = "" Then
If MessageBox.Show("This Will Save All Changes - Are You Sure?", "Warning!",
MessageBoxButtons.OKCancel) = DialogResult.OK Then
cmSystemUsers.EndCurrentEdit()
If dsSystemUsers.HasChanges Then
odaSystemUsers.Update(dsSystemUsers)
dsSystemUsers.Clear()
odaSystemUsers.Fill(dsSystemUsers)
End If
Else
End If
MessageBox.Show(ValidDataMessage, "Data Entry Error", MessageBoxButtons.OK,
MessageBoxIcon.Error)
End If
End If
End Sub

Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnDelete.Click
Dim DeleteResult As DialogResult = MessageBox.Show("Delete " &
txtUsername.Text & " ?", "Warning!", MessageBoxButtons.YesNo,
MessageBoxIcon.Question)
If DeleteResult = DialogResult.Yes Then
Me.dvSystemUsers(cmSystemUsers.Position).Delete()
End If
End Sub

Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnEdit.Click
txtFirstname.Enabled = True
txtSurname.Enabled = True
txtUsername.Enabled = True
txtPassword.Enabled = True
End Sub

Private Sub btnCancelChanges_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnCancelChanges.Click
If MessageBox.Show("This Will Cancel All Changes - Are You Sure?",
"Warning!", MessageBoxButtons.OKCancel) = DialogResult.OK Then
cmSystemUsers.CancelCurrentEdit()
End If
txtFirstname.Enabled = False
txtSurname.Enabled = False
txtUsername.Enabled = False
txtPassword.Enabled = False
End Sub
End Class

First Question - Am I writing my code the correct way for a Database
Application, or am I going about it totally the Wrong Way?
Second Question is - Why I try and click my Update Button, it dosn`t Update
the Data to my Database. It Adds a Row but with just Blank Data
Third Question - When I delete a Row, It deletes it on Screen but dosn`t
Update My Database Again

Sorry for been sooooooo Stupid but like I said Im a newbie thats confused,
I`ve search the Net but got even more confused because it seems there is so
many ways of doing things.
Hope omebody dosn`t mind Helping out?

Many Thanks
NSC
 
C

Cor Ligthert [MVP]

Hi,

To start with, your application is a single user application. If there are
two persons updating a same row, than an error is thrown by the last update,
and than you have to decide what to do. You can reject your changes or you
can say "the last counts". Or you use an in between method where you give by
instance the changes make by the first to the second to evalutat, that is up
to you. The name for that is concurrency error handling. Search for that
using that keyword on MSDN.

http://search.microsoft.com/search/...y+error+handling&View=msdn&st=b&c=0&s=1&swc=0

In the second place is there a nice feature in net what is named
databinding. You see that here in action.

http://www.vb-tips.com/default.aspx?ID=c6c7d9a8-7511-41a1-a488-2e91e5295e7c

Your code is not wrong, you do is the same as every newbie does so certainly
not totally wrong. However it is the very simple way. Not bad, however when
you look after 2 years to it, than you think ...................... You
probably know what I want to say.

See this as positive because I could as well have written that you would
never be abled to look at it after 2 years because that I did not expect
progress as I see your code.

As last I have the idea that you use ODBC.

Try forever this sequence
If possible the real provider SQLClient, Oracle etc.
Than OleDB
And if it is absolute not posible (I would not know why) to use those
previous than ODBC.

I hope this helps something

Cor
 
P

P. Van Den Goess

No_So_Clever said:
Hi There,

Hope Somebody dosn`t mind Helping a Newbie?

I have the Follwoing Code:

Private Sub frmSystemUsers_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'File Datasets
Try
dsSystemUsers.EnforceConstraints = False
Try
Me.odcSystemUsers.Open()
odaSystemUsers.Fill(dsSystemUsers)
cmSystemUsers = DirectCast(BindingContext(dvSystemUsers), CurrencyManager)
mlLoading = True
Catch fillException As System.Exception
Throw fillException
Finally
Me.odcSystemUsers.Close()
End Try
Catch eLoad As System.Exception
System.Windows.Forms.MessageBox.Show(eLoad.Message)
Finally
mlLoading = False
End Try
'End of Fill Datasets
End Sub

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAdd.Click
cmSystemUsers.AddNew()
txtFirstname.Enabled = True
txtSurname.Enabled = True
txtUsername.Enabled = True
txtPassword.Enabled = True
End Sub
Private Sub btnAccept_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAccept.Click
If txtFirstname.Text = "" Then
ValidDataMessage = "First Name Required"
txtFirstname.Focus()
ElseIf txtSurname.Text = "" Then
ValidDataMessage = "Surname Required"
txtSurname.Focus()
ElseIf txtUsername.Text = "" Then
ValidDataMessage = "Username Required"
txtUsername.Focus()
ElseIf txtPassword.Text = "" Then
ValidDataMessage = "Password Required"
txtPassword.Focus()
If ValidDataMessage = "" Then
If MessageBox.Show("This Will Save All Changes - Are You Sure?", "Warning!",
MessageBoxButtons.OKCancel) = DialogResult.OK Then
cmSystemUsers.EndCurrentEdit()
If dsSystemUsers.HasChanges Then
odaSystemUsers.Update(dsSystemUsers)

-----This may be unnecssary and in all chances is. The data in Update will
match the data in database, so clearing and refilling is probablynot
necessity unless you have other data that may been changed and you need
refresh.
dsSystemUsers.Clear()
odaSystemUsers.Fill(dsSystemUsers)
End If
Else
End If
MessageBox.Show(ValidDataMessage, "Data Entry Error", MessageBoxButtons.OK,
MessageBoxIcon.Error)
End If
End If
End Sub

Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnDelete.Click
Dim DeleteResult As DialogResult = MessageBox.Show("Delete " &
txtUsername.Text & " ?", "Warning!", MessageBoxButtons.YesNo,
MessageBoxIcon.Question)
If DeleteResult = DialogResult.Yes Then
Me.dvSystemUsers(cmSystemUsers.Position).Delete()
End If
End Sub

Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnEdit.Click
txtFirstname.Enabled = True
txtSurname.Enabled = True
txtUsername.Enabled = True
txtPassword.Enabled = True
End Sub

Private Sub btnCancelChanges_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnCancelChanges.Click
If MessageBox.Show("This Will Cancel All Changes - Are You Sure?",
"Warning!", MessageBoxButtons.OKCancel) = DialogResult.OK Then
cmSystemUsers.CancelCurrentEdit()
End If
txtFirstname.Enabled = False
txtSurname.Enabled = False
txtUsername.Enabled = False
txtPassword.Enabled = False
End Sub
End Class

First Question - Am I writing my code the correct way for a Database
Application, or am I going about it totally the Wrong Way?
Second Question is - Why I try and click my Update Button, it dosn`t Update
the Data to my Database. It Adds a Row but with just Blank Data

------Look to Update command and make sure parameters are matched correctly
to controls and verify what the syntax is that's being sent to database and
parameters.
Third Question - When I delete a Row, It deletes it on Screen but dosn`t
Update My Database Again
--Again, look to your Delete command and verify it. For beggining, use the
Data Adapter configuration wizards and see the code generated by those. I
suspect your problems lie in the commands that are used. Also, after
delete, you should probably call Update on your dataset unless you have
reason not to - just to make sure that an update is called after you delete
row locally. If you are sure it's getting called though, then this is not
of necessity necessary.
Sorry for been sooooooo Stupid but like I said Im a newbie thats confused,
---We all are newbie sometime, no question is stupid and if not knowing
dotnet makes one stupid, then most of the world is fools.
 
S

Sahil Malik [MVP]

Hey NSC,

Well, updating disconnected data involves a lot of things you need to
consider - Concurrency that Cor mentioned above is only one of them, but
that is like step 10 - lets worry about step 1 through 9 first.

The basic context being, your data is disconnected - the changes you make
won't actually go into the database until you call "Update". As you are
making changes to the disconnected datatable, the rowstates change and
maintain some history wherever necessary.

Then based on those changes, and various rowstates the DataAdapter calls
either the UpdateCommand, the DeleteCommand or the InsertCommand .. when you
call Update.

In those UpdateCommand/Delete/InsertCmd you have "parameters" - the
parameters are used for input and output values - you need to specify the
appropriate information in each one of those such as data type, length,
source column, direction etc.

Then when you call Update - your code will work as you are trying to make it
work.

Once you get that much working, then you can worry about

1. The right way to databind - which is rather simple in your case atleast
2. Concurrency issues
3. Application UI issues
4. Working with various other facets of a disconnected application ~ issues.

I suggest further reading as this topic cannot be duly addressed in a one
page reply.

- Sahil Malik [MVP]
ADO.NET 2.0 book -
http://codebetter.com/blogs/sahil.malik/archive/2005/05/13/63199.aspx
 
N

No_So_Clever

Hi There Thank for Everyones Help,

I used the Wizard and i`ve looked at he code from that. Its working Great
Now. Now the Only Problem I have is i`ve added a Datagrid to my App Aswell,
Makes it easier then licking next a few hundred times to get to the record I
want. When I click on my Row on my datagrid is dosn`t update my
Position_Changed thinggy. Here is my Position Changed Code:

Private Sub dsSystemUsers_PositionChanged()
Me.lblNavLocation.Text = (((Me.BindingContext(dsSystemUsers,
"Passwords").Position + 1).ToString + " of ") +
Me.BindingContext(dsSystemUsers, "Passwords").Count.ToString)
End Sub

I take it I need to Add a Line for My Datagrid I just cn`t find any info
Anywhere on how o do it:( Anybody point me in the right Direction please?

Many Thanks
NSC
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top