Join 300,356 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,939 people online right now. Registration is fast and FREE... Join Now!
hi guys..i have some problem with my VB program..hope u guys can help me out
here's my form
explanation on my program --------------------------------- the ' << ' and ' >> ' button will browse through records from the database...while the "delete" button will delete the record from the database... i have no problem browsing through the records from the database and no problem deleting the records with those buttons
but there's some minor problem.. ---------------------------------------- problem 1 - when i click on the 'delete' button to delete a record, the data will still appear in the textboxes when i click on the '<<' and '>>' buttons, although the data in the database already been deleted.
problem 2 (same problem related to problem 1) - when i keep clicking on the 'delete' button to delete all the records, the program will then crash and this error message appear 'Concurrency violation: the DeleteCommand affected 0 of the expected 1 records.'..........how to avoid this??
problem 3 - if the database is empty and i log into the form and click on the '<<' , '>>' buttons, the program will then crash and this error message appear 'There is no row at position 1.' ....how to lock those button's when the database is empty or any other ways??
please show me sample codes or something as i will find it hard to understand if you just explain those logic things etc for me
here's my code....
CODE
Private Sub frmUser_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
con.ConnectionString = "PROVIDER = Microsoft.Jet.OLEDB.4.0;Data Source = H:\Testing\Project\user.mdb" con.Open() sql = "SELECT * FROM Table1" da = New OleDb.OleDbDataAdapter(sql, con) da.Fill(ds, "Table1List") con.Close() maxrows = ds.Tables("Table1List").Rows.Count
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
If inc <> maxrows - 1 Then inc = inc + 1 navigaterecords(inc) Else MessageBox.Show("No more applications!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning) End If
Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
If inc <> 0 Then inc = inc - 1 navigaterecords(inc) Else MessageBox.Show("No more applications!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning) End If
End Sub ________________________________________________________________________________ __________________
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
con.ConnectionString = "PROVIDER = Microsoft.Jet.OLEDB.4.0;Data Source = H:\Testing\Project\user.mdb" con.Open() sql = "SELECT * FROM Table1" da = New OleDb.OleDbDataAdapter(sql, con) da.Fill(ds, "Table1List") con.Close() maxrows = ds.Tables("Table1List").Rows.Count
Dim cb As New OleDb.OleDbCommandBuilder(da) Dim intresult As Integer
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
If MessageBox.Show("Do you really want to Delete this Record?", _ "Delete", MessageBoxButtons.YesNo, _ MessageBoxIcon.Warning) = Windows.Forms.DialogResult.No Then
MsgBox("Operation Cancelled") Exit Sub
End If Dim cb As New OleDb.OleDbCommandBuilder(da)
A little suggestion, you should create some variables that equal the database results, so you won't have to keep re-writing all of that code again.
For your problem one and two, you should re-bind the data to the form so the correct data is shown. When the form first loads, it gets all the data from the database and binds it to the form.
As for your problem three, you should check to see if the field is empty before trying to display from the database. Something like this :
CODE
If Not ds.Tables("Table1List").Rows(0).Item(0) Is Nothing Then Me.txtAddress.Text = ds.Tables("Table1List").Rows(0).Item(0) End If
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
If MessageBox.Show("Do you really want to Delete this Record?", _ "Delete", MessageBoxButtons.YesNo, _ MessageBoxIcon.Warning) = Windows.Forms.DialogResult.No Then
MsgBox("Operation Cancelled") Exit Sub
End If Dim cb As New OleDb.OleDbCommandBuilder(da)
hi bro, i tried to run your code and this error message pop out....'Deleted row information cannot be accessed through the row.' the data cant be deleted from the database too ..
Here is a full example of what I was trying to explain earlier, I don't know if it works since I can't test the code so it might need some tweaking.
CODE
Private Sub frmUser_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.BindData() End Sub
Public Sub BindData() con.ConnectionString = "PROVIDER = Microsoft.Jet.OLEDB.4.0;Data Source = H:\Testing\Project\user.mdb" con.Open() Sql = "SELECT * FROM Table1" da = New OleDb.OleDbDataAdapter(Sql, con) da.Fill(ds, "Table1List") con.Close() maxrows = ds.Tables("Table1List").Rows.Count
Dim Name As String = ds.Tables("Table1List").Rows(0).Item(0) Dim UserName As String = ds.Tables("Table1List").Rows(0).Item(1) Dim Nickname As String = ds.Tables("Table1List").Rows(0).Item(2) Dim Password As String = ds.Tables("Table1List").Rows(0).Item(3) Dim City As String = ds.Tables("Table1List").Rows(0).Item(4) Dim Address As String = ds.Tables("Table1List").Rows(0).Item(5) Dim Town As String = ds.Tables("Table1List").Rows(0).Item(6) Dim PostCode As String = ds.Tables("Table1List").Rows(0).Item(7)
If Not Name Is Nothing Then Me.txtName.Text = Name End If If Not UserName Is Nothing Then Me.txtUsername.Text = UserName End If If Not Nickname Is Nothing Then Me.txtNickname.Text = Nickname End If If Not Password Is Nothing Then Me.txtPassword.Text = Password End If If Not City Is Nothing Then Me.txtCity.Text = City End If If Not Address Is Nothing Then Me.txtAddress.Text = Address End If If Not Town Is Nothing Then Me.txtTown.Text = Town End If If Not PostCode Is Nothing Then Me.txtPostcode.Text = PostCode End If
Me.DataBind() End Sub
Public Sub navigaterecords(ByVal position As Integer) Me.BindData()
If Not Name Is Nothing Then Me.txtName.Text = Name End If If Not UserName Is Nothing Then Me.txtUsername.Text = UserName End If If Not Nickname Is Nothing Then Me.txtNickname.Text = Nickname End If If Not Password Is Nothing Then Me.txtPassword.Text = Password End If If Not City Is Nothing Then Me.txtCity.Text = City End If If Not Address Is Nothing Then Me.txtAddress.Text = Address End If If Not Town Is Nothing Then Me.txtTown.Text = Town End If If Not PostCode Is Nothing Then Me.txtPostcode.Text = PostCode End If End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click If inc <> maxrows - 1 Then inc = inc + 1 navigaterecords(inc) Else MessageBox.Show("No more applications!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning) End If End Sub
Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click If inc <> 0 Then inc = inc - 1 navigaterecords(inc) Else MessageBox.Show("No more applications!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning) End If End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click con.ConnectionString = "PROVIDER = Microsoft.Jet.OLEDB.4.0;Data Source = H:\Testing\Project\user.mdb" con.Open() sql = "SELECT * FROM Table1" da = New OleDb.OleDbDataAdapter(sql, con) da.Fill(ds, "Table1List") con.Close() maxrows = ds.Tables("Table1List").Rows.Count
Dim cb As New OleDb.OleDbCommandBuilder(da) Dim intresult As Integer
Here is a full example of what I was trying to explain earlier, I don't know if it works since I can't test the code so it might need some tweaking.
Private Sub frmUser_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.BindData() End Sub ________________________________________________________________________________ _____ Public Sub BindData() con.ConnectionString = "PROVIDER = Microsoft.Jet.OLEDB.4.0;Data Source = H:\Testing\Project\user.mdb" con.Open() Sql = "SELECT * FROM Table1" da = New OleDb.OleDbDataAdapter(Sql, con) da.Fill(ds, "Table1List") con.Close() maxrows = ds.Tables("Table1List").Rows.Count
Dim Name As String = ds.Tables("Table1List").Rows(0).Item(0) Dim UserName As String = ds.Tables("Table1List").Rows(0).Item(1) Dim Nickname As String = ds.Tables("Table1List").Rows(0).Item(2) Dim Password As String = ds.Tables("Table1List").Rows(0).Item(3) Dim City As String = ds.Tables("Table1List").Rows(0).Item(4) Dim Address As String = ds.Tables("Table1List").Rows(0).Item(5) Dim Town As String = ds.Tables("Table1List").Rows(0).Item(6) Dim PostCode As String = ds.Tables("Table1List").Rows(0).Item(7)
If Not Name Is Nothing Then Me.txtName.Text = Name End If If Not UserName Is Nothing Then Me.txtUsername.Text = UserName End If If Not Nickname Is Nothing Then Me.txtNickname.Text = Nickname End If If Not Password Is Nothing Then Me.txtPassword.Text = Password End If If Not City Is Nothing Then Me.txtCity.Text = City End If If Not Address Is Nothing Then Me.txtAddress.Text = Address End If If Not Town Is Nothing Then Me.txtTown.Text = Town End If If Not PostCode Is Nothing Then Me.txtPostcode.Text = PostCode End If
Me.BindData() End Sub ________________________________________________________________________________ ______________ Public Sub navigaterecords(ByVal position As Integer) Me.BindData()
If Not Name Is Nothing Then Me.txtName.Text = Name End If If Not UserName Is Nothing Then Me.txtUsername.Text = UserName End If If Not Nickname Is Nothing Then Me.txtNickname.Text = Nickname End If If Not Password Is Nothing Then Me.txtPassword.Text = Password End If If Not City Is Nothing Then Me.txtCity.Text = City End If If Not Address Is Nothing Then Me.txtAddress.Text = Address End If If Not Town Is Nothing Then Me.txtTown.Text = Town End If If Not PostCode Is Nothing Then Me.txtPostcode.Text = PostCode End If End Sub ________________________________________________________________________________ ____________ Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click If inc <> maxrows - 1 Then inc = inc + 1 navigaterecords(inc) Else MessageBox.Show("No more applications!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning) End If End Sub ________________________________________________________________________________ ____________
Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click If inc <> 0 Then inc = inc - 1 navigaterecords(inc) Else MessageBox.Show("No more applications!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning) End If End Sub ________________________________________________________________________________ ____________
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click con.ConnectionString = "PROVIDER = Microsoft.Jet.OLEDB.4.0;Data Source = H:\Testing\Project\user.mdb" con.Open() sql = "SELECT * FROM Table1" da = New OleDb.OleDbDataAdapter(sql, con) da.Fill(ds, "Table1List") con.Close() maxrows = ds.Tables("Table1List").Rows.Count
Dim cb As New OleDb.OleDbCommandBuilder(da) Dim intresult As Integer
hi bro..thanks for your patience...i tried to type in your code in my program..but those words i mark down with blue color were underlined with blue lines in the program and the errors message there stated that all of those words were not declare..what should i do with it??
i tried to declare it like this...
CODE
Public Sub navigaterecords(ByVal position As Integer)
Dim Name As String = ds.Tables("Table1List").Rows(0).Item(0) Dim UserName As String = ds.Tables("Table1List").Rows(0).Item(1) Dim Nickname As String = ds.Tables("Table1List").Rows(0).Item(2) Dim Password As String = ds.Tables("Table1List").Rows(0).Item(3) Dim City As String = ds.Tables("Table1List").Rows(0).Item(4) Dim Address As String = ds.Tables("Table1List").Rows(0).Item(5) Dim Town As String = ds.Tables("Table1List").Rows(0).Item(6) Dim PostCode As String = ds.Tables("Table1List").Rows(0).Item(7)
Me.BindData()
If Not Name Is Nothing Then Me.txtName.Text = Name End If If Not UserName Is Nothing Then Me.txtUsername.Text = UserName End If If Not Nickname Is Nothing Then Me.txtNickname.Text = Nickname End If If Not Password Is Nothing Then Me.txtPassword.Text = Password End If If Not City Is Nothing Then Me.txtCity.Text = City End If If Not Address Is Nothing Then Me.txtAddress.Text = Address End If If Not Town Is Nothing Then Me.txtTown.Text = Town End If If Not PostCode Is Nothing Then Me.txtPostcode.Text = PostCode End If End Sub
but the error message "Argument not specified for parameter 'index' of 'Public ReadOnly Default Property Chars(index As Integer) As Char'. appeared...
______________________________ update: i tried to remove the codes in "Public Sub navigaterecords(ByVal position As Integer)" and tried to log into my form...but it failed to login...the loading seems like extremely slow and cant log into my form at all ...you sure those codes are ok?
This post has been edited by wongth7: 2 Jul, 2009 - 08:33 AM
I am a strong advocate of structured learning, there is no way to know what is happening in the code or even how to modify other peoples code to your needs if you don't get some kind of structured learning.
I am a strong advocate of structured learning, there is no way to know what is happening in the code or even how to modify other peoples code to your needs if you don't get some kind of structured learning.
Spoiler:
Now to get the Windex...
if there's google for everything..then what's the use of this forum??.......i already look through search engine for what i want but i still cant get it..that's why im here posting this question..
if you cant help just don't bother to post....you dont know how to modify the code or whatever then why bother to reply here?..why not just post your sentence in everyone's question and ask them to google for answer..and ask the admin to close this forum down....
if there's google for everything..then what's the use of this forum??.......i already look through search engine for what i want but i still cant get it..that's why im here posting this question..
if you cant help just don't bother to post....you dont know how to modify the code or whatever then why bother to reply here?..why not just post your sentence in everyone's question and ask them to google for answer..and ask the admin to close this forum down....
peace!
Just in case you come back to read this or other aspiring programmers, there is a lot going through my head so let me see if I can make something of this. Part of me would apologize, but I posted with legitimate resources that could potentially help you. Others even posted code examples as you requested:
QUOTE(wongth7 @ 2 Jul, 2009 - 06:03 AM)
please show me sample codes or something as i will find it hard to understand if you just explain those logic things etc for me
The trouble is just that we could give you all the code examples in the world, and you still wouldn't understand because you must have some remote idea of what the logic behind it is. We are here to help, but we're not (I am not) here to just modify code to suite your needs. I believe that would be bordering on impossible, because I (as of yet) am not a mind reader.
if there's google for everything..then what's the use of this forum??.......i already look through search engine for what i want but i still cant get it..that's why im here posting this question..
if you cant help just don't bother to post....you dont know how to modify the code or whatever then why bother to reply here?..why not just post your sentence in everyone's question and ask them to google for answer..and ask the admin to close this forum down....
peace!
Just in case you come back to read this or other aspiring programmers, there is a lot going through my head so let me see if I can make something of this. Part of me would apologize, but I posted with legitimate resources that could potentially help you. Others even posted code examples as you requested:
QUOTE(wongth7 @ 2 Jul, 2009 - 06:03 AM)
please show me sample codes or something as i will find it hard to understand if you just explain those logic things etc for me
The trouble is just that we could give you all the code examples in the world, and you still wouldn't understand because you must have some remote idea of what the logic behind it is. We are here to help, but we're not (I am not) here to just modify code to suite your needs. I believe that would be bordering on impossible, because I (as of yet) am not a mind reader.
I already stated my problems there didn't I?..or did i not explain my problems clear enough??..why people like camo and Luc001 have no problem understanding what im saying?? all i need is just ANY ways to solve my problems, im not asking you to guess my problem or whatever...
instead of spamming here...why not you just explain what i should do or give me suggestion on how to solve my problem..but i seriously doubt you would bother to do so..
all i need is just ANY ways to solve my problems, im not asking you to guess my problem or whatever... instead of spamming here...why not you just explain what i should do or give me suggestion on how to solve my problem..but i seriously doubt you would bother to do so..
Right. Uh-huh. Well, you could probably get have gotten some help had you followed any of my suggestions so that you could find out how to declare a variable of a particular scope. Or by looking here for a start.
Or I could say "You need to put the variable declarations at the top of your class". And give you this code:
CODE
Public Class Form1 Dim sName As String Dim UserName As String Dim Nickname As String Dim Password As String Dim City As String Dim Address As String Dim Town As String Dim PostCode As String End Class
Yall don't have to get into a fight over this. I didn't say my code would work, it was merely an example to help get you off your feet.
Wongth, LoveIsNull didn't say anything wrong when he said to use Google. I use Google first and can usually get some general idea of what's going on and what needs to be done to fix it.
sorry bout the fuss..just trying to get things done....and im not a pro in vb...as due date is getting near, frustration kicks in..
i already tried to declare all those words as string in my Public Class Form1
but it gets even worst....i can't log into my form at all..it just freeze there...any other ways???..i thought just some simple code to refresh the form after the data is deleted would solve it...no??
or can someone teach me how to
1. refresh the form right after i delete my data 2. lock the << , >> buttons if there were no data in the database
This post has been edited by wongth7: 4 Jul, 2009 - 05:55 AM