Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Here is the story

I have a Mysql database setup:

Table Accounts:

Id: 1
Name: Donny
Credits: 600



and i have write a code with a button call's Buy

now i have on the button this little peace of code:

VB
If credits.Text <= 0 Then
          MsgBox("Geen credits meer.")
      Else

          Dim conn As MySqlConnection
          conn = New MySqlConnection
          conn.ConnectionString = "server=xxx;port=3306; user id=xxx; password=xxx; database=xxx"
          Try
              conn.Open()
          Catch myerror As MySqlException
              MsgBox("error connecting to database")
          End Try
          Dim myadapter As New MySqlDataAdapter

          Dim sqlquery = "UPDATE account SET credits = credits -700 WHERE id='" & Trim(id.Text) & "'"

          Dim mycommand As New MySqlCommand()
          mycommand.Connection = conn
          mycommand.CommandText = sqlquery
          myadapter.SelectCommand = mycommand
          mycommand.ExecuteNonQuery()
          Try
              conn.Close()
          Catch myerror As MySqlException
              MessageBox.Show("Cannot connect to database: " & myerror.Message)
          Finally
              conn.Dispose()
          End Try

      End If


Now for a example i have on my account 600 credits.
and wanna buy a product it cost 700. now what i want is that the system says its not possible because you got only 600 credits .. now i have write a code that if its null then it show a message box but what is gonna happened is it show me the value: -100 and then if i click again then it shows the msg box.

How i must edit this peace of code its gonna happened what i want.
Posted
Comments
Maciej Los 12-Apr-15 13:47pm    
You need to check if there is enough credits, before you allow further operation.

1 solution

Why is the logic behind this so difficult?

All you do is query the database for the user record and his account balance. Check to see if there is enough credits. If there is another database query is required to update the user record with the new balance and probably another query to record the transaction.
 
Share this answer
 
Comments
Theo Thea 12-Apr-15 16:28pm    
And my question is exactly what is the query? how i must begin with that.
Dave Kreskowiak 12-Apr-15 16:42pm    
You're kidding, right?

SELECT credits FROM Account WHERE id=@id

You do the checking of balance against purchase in your C# business logic code, not in the query.

DO NOT USE STRING CONCATENATION TO BUILD SQL QUERIES. You open yourself up to SQL Injection attacks that can destroy your entire database. Google for "C# SQL parameterized queries" to find out how to do it correctly.
Theo Thea 12-Apr-15 17:38pm    
Dude, i respect you way of scripting.. i got a simple script to create a example and then i create it with security etc etc, im not here to disrespect each other scripting i got a question i bring this out....... with all respect sir, my question is what query i must perform that the system can see how much i can spent on a product and if i have no credits enough that the systems says your out of credits or not enough credits to buy the product. that it...and if u can see the tags... its vb.net not c#
Dave Kreskowiak 12-Apr-15 19:12pm    
OK, so then replace C# with VB.NET.

Seriously, if you're looking for me to write the code for you, you've come to the wrong place.

You obviously already know how to execute a query and I gave you the query, so what's the problem? You're VB.NET code should be getting the value from executing the query, then check to see if the balance is large enough for the purchase.
Theo Thea 12-Apr-15 19:25pm    
may i ask why u registered on this forum anyway? i mean being a jerk is not helping anybody, i have been nice to you and talk with any respect and if u dont want to help people on a forum like this... dont register and dont give answers instead. then YOU are on the right place..... have a nice evening...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900