wher ami i ging wrong

sudani1969

Member
Joined
Sep 25, 2009
Messages
13
Programming Experience
Beginner

hi Guys this is my first post hopefully it not a repeat if it is i do appolgize for time wasting
i try to create a windows form that act something smilar to a cash machine i managed to enter a mount with my first button then my second button is for withdrawing money it checks the amount in the text amount if it is higher than the amount the small dat hold in the amount it will throw an error message if it is less or equal it write the amount in the text box
my goal is to deduct that amount in the textbox amount from the amount already at the data base COULD ANY ONE DIRECT ME AND WHERE I'M GOING WRONG THE CODE IS BELOW
:confused::confused:


thanks in advance

Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Dim con As New SqlConnection("Data Source = bedroom\vista; initial catalog = Volunteers; integrated security = true")
Dim DtRd As SqlDataReader


Private Sub txtDeposit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeposit.Click
con.Open()
Dim insert As New SqlCommand(" Insert into [Balance] ([Amount]) Values(@amount)", con)
insert.Parameters.AddWithValue("@amount", txtAmount.Text)
Dim RowId As Integer = insert.ExecuteScalar()
End Sub

Private Sub btnWithdraw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWithdraw.Click
con.Open()
Dim balance As Double
Dim cmd As New SqlCommand
cmd.Connection = con
cmd.CommandText = "Select Amount from balance"
DtRd = cmd.ExecuteReader()
While DtRd.Read

If txtAmount.Text > DtRd.Item(0).ToString Then
MsgBox("There is not enough sufficent fund in your account", MsgBoxStyle.Critical)
ElseIf txtAmount.Text <> DtRd.Item(0).ToString Then
MsgBox("The amount of " + txtAmount.Text + " will be deducted from your account", MsgBoxStyle.Information)
' DtRd.Close()
End If
balance = DtRd.Read
MainCon.UpdateCommand.CommandText = "UPDATE balance SET [Amount]'" & txtAmount.Text - balance & "'"
MainCon.UpdateCommand.ExecuteNonQuery()

End While
'DtRd.Close()
End Sub
End Class

 
Your design is seriously flawed. You have a database table named Balance, right? Forget programming and think about your own bank account. What is the balance? It's the actual amount you have in the bank at the moment, right? Is that what your Balance table is recording? No, it's not. That table is recording transactions, not balance. If you make three deposits of $100, $50 and $20, what will your Balance table contain? It will contain three records with 100, 50 and 20 respectively, right? That means that your balance is $170, but nowhere are you recording or calculating that value, so nowhere are you using the actual balance.
 
Your design is seriously flawed. You have a database table named Balance, right? Forget programming and think about your own bank account. What is the balance? It's the actual amount you have in the bank at the moment, right? Is that what your Balance table is recording? No, it's not. That table is recording transactions, not balance. If you make three deposits of $100, $50 and $20, what will your Balance table contain? It will contain three records with 100, 50 and 20 respectively, right? That means that your balance is $170, but nowhere are you recording or calculating that value, so nowhere are you using the actual balance.
Thank you for ur reply
i gues flawed is big word i guess as a newbie i desrve to make flawed design don't we all have to start some where....any way i didn't even put design into it
all i need a simple program that dedcut the amount enterd by the user to be deducted from amount already exist in , lets say in a store box not the big word DATABASE.
SO WHE I HIT THE WITHDRAW BUTTON I NEED at the same time to update my amout available
thak you
ad be patiet with a newbie cause if i don't get help here i wont be able to get it any where else
 
The problem is that you're writing code to implement a design that doesn't work. You have to come up with a design that works first, otherwise you may as well give up because all the code in the world won't help. Forget code and programming for a moment and think about what data you're going to have to store to make this work. Do you need to store each individual transaction, or do you just need to store the current total, i.e. the balance? That one simple decision is going to have a big impact on how things are implemented, so you need to be clear about before you do ANYTHING else.

If you need to keep a record of every transaction then you'll need to insert a record for every transaction. You can then calculate the balance by adding up all the individual transactions. That's not really practical though, because could end up being a lot of data over time, so the alternative would be to keep a running total with each transaction. If you don't need each transaction then you only need one record per account, which you update with each transaction. This is basic stuff that you don't need any programming experience to figure out, so don't use that as an excuse. It's mostly common sense with a bit of logic.
 
I was told to give up ?????

guys my task was simple all i needed to do to deduct the withdrawal amoutn from the already balance which was simple
all you have to do use something simialr:


Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
con.Open()
Dim update As New SqlCommand
update.Connection = con




update.CommandText = "UPDATE Balance SET [Amount] = Amount -'" & txtAmount.Text & "'"
dtRd = update.ExecuteReader()
con.Close()
End Sub


if you are a beginer you need to take one step at a time, hence when i DESIGN A PROJECT at lease i will not stop at how ami going to sort this out
DIFFERENT BRAINS USE DIFFERENT TECHNIQUES SO YOU DO NOT TELL SOME ONE YOU HAVE TO DO THIS
ALL YOU HAVE TO DO IS GUIDE THEM TO THE RIGHT DIRECTION
SO LONG
 
If what you've posted so far is correct then your solution is no solution at all. According to your code, each time you make a deposit you insert a record into the Balance table containing the amount. If you make three deposits you will have three records. If you then make a withdrawal you are going to subtract the withdrawal amount from each deposit amount. Let's say that you make three deposits of $100 each. You'll have three records, each with an amount of $100. If you then make a withdrawal of, say, $50, you will subtract $50 from all three records and so you'll have three records, each with an amount of $50. So, you've deposited $300 and withdrawn $50, so your actual balance is $250. Your code won't result in that value being stored anywhere or even that value be able to be calculated. Your code simply won't work. I will tell you what you should do when it's time-tested development technique. If you don't bother with any design before you start writing code then your code will likely not end up doing what it's supposed to do, because you don't actually know what it's supposed to do.
 
Back
Top