|
-
Jul 19th, 2009, 03:12 AM
#1
Thread Starter
Frenzied Member
[RESOLVED] Transfering Data from one part to other of database to other!!!
The form is like this:
A database contains six columns:
"From Bank","From Account","From Balance","To Bank","To Account","To Balance"
On transfer button click,The balanced will be transfered and added to the "To Balance" of "To Account"
i did the code for form load but i cant get throught the transfer money code:
Code:
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Dim con As New OleDbConnection
Dim cmd As New OleDbCommand
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
con = New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=C:\Users\Gautam\Documents\Visual Studio 2005\Projects\Transfer\Transfer\db1.mdb")
con.Open()
cmd = New OleDbCommand("Select * from table1", con)
Dim da As New OleDbDataAdapter(cmd)
Dim dt As New DataTable
da.Fill(dt)
con.Close()
ComboBox1.DataSource = dt
ComboBox1.DisplayMember = "From_Bank_Name"
ComboBox1.Text = ""
ComboBox2.DataSource = dt
ComboBox2.DisplayMember = "From_Account_No"
ComboBox2.Text = ""
ComboBox3.DataSource = dt
ComboBox3.DisplayMember = "To_Bank_Name"
ComboBox3.Text = ""
ComboBox4.DataSource = dt
ComboBox4.DisplayMember = "To_Account_No"
ComboBox4.Text = ""
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
End Sub
End Class
now in button click i want to perform this:
Original balance(Sender)=Original balance-From_Balance
Original balance(Receiver)=Original balance+TO_balance
then
From_Balance=Original balance(Sender)
To_Balance=Original balance(Receiver)
Am I getting it right?
Please help!!!
Last edited by gautamshaw; Feb 21st, 2010 at 01:22 PM.
-
Jul 19th, 2009, 05:05 AM
#2
Re: Transfering Data from one part to other of database to other!!!
I would create and execute two SQL statement something along the lines of :
UDPATE table1 SET balance = balance + @sum WHERE BankName = @RecipientBankName AND AccountNo = @RecipientAccountNo
and
UDPATE table1 SET balance = balance - @sum WHERE BankName = @SenderBankName AND AccountNo = @SenderAccountNo
Where you replace the parameters @sum, @senderbankname, @senderaccountno, @recipientbankname and @recipientaccountno with the values from the controls on your form.
-
Jul 20th, 2009, 11:22 AM
#3
Thread Starter
Frenzied Member
Re: Transfering Data from one part to other of database to other!!!
Code:
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
con = New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source= C:\Users\Gautam\Documents\Visual Studio 2005\Projects\Transfer\Transfer\db1.mdb")
con.Open()
cmd = New OleDbCommand(("Update Table1 set From_Balance= From_Balance - ' " & TextBox1.Text.Trim() & " ' where From_Bank_Name='" & ComboBox1.Text.Trim() & "' and From_Account_No='" & ComboBox2.Text.Trim() & "' ") _
And ("Update Table1 set To_Balance= To_Balance + ' " & TextBox2.Text.Trim() & " ' where To_Bank_Name= '" & ComboBox3.Text.Trim() & "' and To_Account_No='" & ComboBox4.Text.Trim() & "' "), con)
cmd.ExecuteNonQuery()
Dim rowsaffected As Integer = cmd.ExecuteNonQuery
MsgBox("Number of rows affected : " + cmd.ExecuteNonQuery.ToString)
Catch ex As Exception
End Try
End Sub
Its giving an exceotion:
Attachment 72095
Please hlp me.....
Last edited by gautamshaw; Feb 21st, 2010 at 01:22 PM.
-
Jul 20th, 2009, 11:28 AM
#4
Re: Transfering Data from one part to other of database to other!!!
You should be using parameters in your SQL rather than concatenating strings.
If you do that then it will be easier to work out what is wrong.
-
Jul 20th, 2009, 11:39 AM
#5
-
Jul 20th, 2009, 11:49 AM
#6
Thread Starter
Frenzied Member
Re: Transfering Data from one part to other of database to other!!!
Code:
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Dim con As New OleDbConnection
Dim cmd As New OleDbCommand
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
con = New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=C:\Users\Gautam\Documents\Visual Studio 2005\Projects\Transfer\Transfer\db1.mdb")
con.Open()
cmd = New OleDbCommand("Select * from Table1", con)
Dim da As New OleDbDataAdapter(cmd)
Dim dt As New DataTable
da.Fill(dt)
con.Close()
ComboBox1.DataSource = dt
ComboBox1.DisplayMember = "From_Bank_Name"
ComboBox1.Text = ""
ComboBox2.DataSource = dt
ComboBox2.DisplayMember = "From_Account_No"
ComboBox2.Text = ""
ComboBox3.DataSource = dt
ComboBox3.DisplayMember = "To_Bank_Name"
ComboBox3.Text = ""
ComboBox4.DataSource = dt
ComboBox4.DisplayMember = "To_Account_No"
ComboBox4.Text = ""
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
con = New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source= C:\Users\Gautam\Documents\Visual Studio 2005\Projects\Transfer\Transfer\db1.mdb")
con.Open()
' cmd = New OleDbCommand(("Update Table1 set From_Balance= From_Balance - ' " & TextBox1.Text.Trim() & " ' where From_Bank_Name='" & ComboBox1.Text.Trim() & "' and From_Account_No='" & ComboBox2.Text.Trim() & "' ") _
'And ("Update Table1 set To_Balance= To_Balance + ' " & TextBox2.Text.Trim() & " ' where To_Bank_Name= '" & ComboBox3.Text.Trim() & "' and To_Account_No='" & ComboBox4.Text.Trim() & "' "), con)
cmd = New OleDbCommand("Update Table1 set From_Balance= From_Balance-@Fsum where From_Bank_Name=@FBankName and From_Account_No=@FAccountNo", con)
cmd.Parameters.AddWithValue("@Fsum", TextBox1.Text)
cmd.Parameters.AddWithValue("@FBankName", ComboBox1.Text)
cmd.Parameters.AddWithValue("@FAccountNo", ComboBox2.Text)
cmd = New OleDbCommand("Update Table1 set To_Balance= To_Balance+@Tsum where To_Bank_Name=@TBankName and To_Account_No=@TAccountNo", con)
cmd.Parameters.AddWithValue("@Tsum", TextBox2.Text)
cmd.Parameters.AddWithValue("@TBankName", ComboBox3.Text)
cmd.Parameters.AddWithValue("@TAccountNo", ComboBox4.Text)
cmd.ExecuteNonQuery()
Dim rowsaffected As Integer = cmd.ExecuteNonQuery
MsgBox("Number of rows affected : " + cmd.ExecuteNonQuery.ToString)
Catch ex As Exception
End Try
End Sub
End Class
I did this sir.........
It runs well..........msgbox displays 1 row affected..............
But sir no change in the database............
Help me out of this sir........
-
Jul 20th, 2009, 11:57 AM
#7
Re: Transfering Data from one part to other of database to other!!!
You do realize you are only performing the last update right? What is the backend database and how are you checking that no change is occuring. What is the resutlr of rowsaffected?
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Jul 20th, 2009, 12:04 PM
#8
Thread Starter
Frenzied Member
Re: Transfering Data from one part to other of database to other!!!
At the right hand end tere you can see the db1.mdb....
Attachment 72096
After the msgbox displays.......i just double click that db1.mdb and check the output......
How to get the desired output sir........
should i change the update query.........
i cant guess where is my mistake sir..........
Please help me sir...........
Last edited by gautamshaw; Feb 21st, 2010 at 01:22 PM.
-
Jul 20th, 2009, 12:23 PM
#9
Thread Starter
Frenzied Member
Re: Transfering Data from one part to other of database to other!!!
the result of rows affected is 1 but no change in the data base.....
how to solve this problem?
-
Jul 20th, 2009, 12:49 PM
#10
Re: Transfering Data from one part to other of database to other!!!
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Jul 20th, 2009, 12:55 PM
#11
Thread Starter
Frenzied Member
Re: Transfering Data from one part to other of database to other!!!
Reading the site sir I think i have done things right............
Copies the database and pasted in the correct place........the db1 even appears in the bin folder......
Then why is my above code not working?
Please help me sir......
-
Jul 20th, 2009, 01:18 PM
#12
Re: Transfering Data from one part to other of database to other!!!
Please print out and show the sql statement that is generated before the execution of that statement on the database
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Jul 20th, 2009, 01:33 PM
#13
Thread Starter
Frenzied Member
Re: Transfering Data from one part to other of database to other!!!
I cant follow what are you asking for........
Code:
cmd = New OleDbCommand("Update Table1 set From_Balance= From_Balance-@Fsum where From_Bank_Name=@FBankName and From_Account_No=@FAccountNo", con)
cmd.Parameters.AddWithValue("@Fsum", TextBox1.Text)
cmd.Parameters.AddWithValue("@FBankName", ComboBox1.Text)
cmd.Parameters.AddWithValue("@FAccountNo", ComboBox2.Text)
cmd = New OleDbCommand("Update Table1 set To_Balance= To_Balance+@Tsum where To_Bank_Name=@TBankName and To_Account_No=@TAccountNo", con)
cmd.Parameters.AddWithValue("@Tsum", TextBox2.Text)
cmd.Parameters.AddWithValue("@TBankName", ComboBox3.Text)
cmd.Parameters.AddWithValue("@TAccountNo", ComboBox4.Text)
cmd.ExecuteNonQuery()
Dim rowsaffected As Integer = cmd.ExecuteNonQuery
MsgBox("Number of rows affected : " + cmd.ExecuteNonQuery.ToString)
Catch ex As Exception
-
Jul 20th, 2009, 01:39 PM
#14
Re: Transfering Data from one part to other of database to other!!!
First off why are you executing the statement twice?
cmd.ExecuteNonQuery() 1st execure
Dim rowsaffected As Integer = cmd.ExecuteNonQuery secondExecute
Comment out the first on and only run the second.
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Jul 20th, 2009, 01:42 PM
#15
Thread Starter
Frenzied Member
Re: Transfering Data from one part to other of database to other!!!
I commented the firs statement sir...........
Now what to do?
-
Jul 20th, 2009, 01:43 PM
#16
Re: Transfering Data from one part to other of database to other!!!
OK part of the problem is that because you are using Access not SQL Server you can't use named parameters so you need to make those @Fsum etc just "?" instead.
But that said if the query says it updated 1 row then it updated 1 row and the odds are you are looking at the wrong database, or the database is getting overwritten.
-
Jul 20th, 2009, 01:49 PM
#17
Re: Transfering Data from one part to other of database to other!!!
The other issue with parameters and Access is that they must be added in the same order as they appear in the statement. Since they are not named they are ordinal in how the load.
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Jul 20th, 2009, 01:52 PM
#18
Thread Starter
Frenzied Member
Re: Transfering Data from one part to other of database to other!!!
Code:
you are using Access not SQL Server you can't use named parameters so you need to make those @Fsum etc just "?" instead.
I did this sir but still no result.................
I think the first update query is over written by the 2nd one..........hence no output........
Am i right sir?
-
Jul 20th, 2009, 01:58 PM
#19
Re: Transfering Data from one part to other of database to other!!!
I think the first update query is over written by the 2nd one..........hence no output........
You are right... you aren't executing the first one - you set it up and then you overwrite it without executing it.
-
Jul 20th, 2009, 01:59 PM
#20
Thread Starter
Frenzied Member
Re: Transfering Data from one part to other of database to other!!!
This is my daabase:
Attachment 72098
Here lies my code:
Code:
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
con = New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source= C:\Users\Gautam\Documents\Visual Studio 2005\Projects\Transfer\Transfer\db1.mdb")
con.Open()
'cmd = New OleDbCommand(("Update Table1 set From_Balance= From_Balance - " & TextBox1.Text.Trim() & " where From_Bank_Name='" & ComboBox1.Text.Trim() & "' and From_Account_No='" & ComboBox2.Text.Trim() & "' ") _
'And ("Update Table1 set To_Balance= To_Balance + " & TextBox2.Text.Trim() & " where To_Bank_Name= '" & ComboBox3.Text.Trim() & "' and To_Account_No='" & ComboBox4.Text.Trim() & "' "), con)
cmd = New OleDbCommand("Update Table1 set From_Balance= From_Balance - ?Fsum where From_Bank_Name = ?FBankName and From_Account_No = ?FAccountNo", con)
cmd.Parameters.AddWithValue("?Fsum", TextBox1.Text)
cmd.Parameters.AddWithValue("?FBankName", ComboBox1.Text)
cmd.Parameters.AddWithValue("?FAccountNo", ComboBox2.Text)
cmd = New OleDbCommand("Update Table1 set To_Balance= To_Balance + ?Fsum where To_Bank_Name = ?TBankName and To_Account_No = ?TAccountNo", con)
' cmd.Parameters.AddWithValue("?Tsum", TextBox2.Text)
cmd.Parameters.AddWithValue("?TBankName", ComboBox3.Text)
cmd.Parameters.AddWithValue("?TAccountNo", ComboBox4.Text)
'cmd.ExecuteNonQuery()
Dim rowsaffected As Integer = cmd.ExecuteNonQuery
MsgBox("Number of rows affected : " + cmd.ExecuteNonQuery.ToString)
Catch ex As Exception
End Try
End Sub
End Class
Where lies the defect?
Last edited by gautamshaw; Feb 21st, 2010 at 01:22 PM.
-
Jul 20th, 2009, 01:59 PM
#21
Re: Transfering Data from one part to other of database to other!!!
Try this and what happend?\
vb.net Code:
cmd = New OleDbCommand("Update Table1 set From_Balance= From_Balance-@Fsum where From_Bank_Name=@FBankName and From_Account_No=@FAccountNo", con)
cmd.Parameters.AddWithValue("@Fsum", TextBox1.Text)
cmd.Parameters.AddWithValue("@FBankName", ComboBox1.Text)
cmd.Parameters.AddWithValue("@FAccountNo", ComboBox2.Text)
Dim rowseffected1 as Integer - cmd.ExecuteNonQuery()
cmd2 = New OleDbCommand("Update Table1 set To_Balance= To_Balance+@Tsum where To_Bank_Name=@TBankName and To_Account_No=@TAccountNo", con)
cmd2.Parameters.AddWithValue("@Tsum", TextBox2.Text)
cmd2.Parameters.AddWithValue("@TBankName", ComboBox3.Text)
cmd2.Parameters.AddWithValue("@TAccountNo", ComboBox4.Text)
Dim rowsaffected As Integer = cmd2.ExecuteNonQuery
cmd.Dispose()
cmd2.Dispose()
MsgBox("Number of rows affected from first Query are: " & rowseffected1.ToString())
MsgBox("Number of rows affected from second Query are: " & rowseffected.ToString())
ps Paul I tried to tell him that at least once already
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Jul 20th, 2009, 02:01 PM
#22
Thread Starter
Frenzied Member
Re: Transfering Data from one part to other of database to other!!!
Then sir how to modify this so that both the updation take place at the same time?
Should i do it using the and clause between the two update statements?
What should i do sir?
-
Jul 20th, 2009, 02:02 PM
#23
Re: Transfering Data from one part to other of database to other!!!
Look at post 21. That is the only way using Access
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Jul 20th, 2009, 02:03 PM
#24
Re: Transfering Data from one part to other of database to other!!!
 Originally Posted by GaryMazzone
Try this and what happend?\
Code:
cmd = New OleDbCommand("Update Table1 set From_Balance= From_Balance-@Fsum where From_Bank_Name=@FBankName and From_Account_No=@FAccountNo", con)
cmd.Parameters.AddWithValue("@Fsum", TextBox1.Text)
cmd.Parameters.AddWithValue("@FBankName", ComboBox1.Text)
cmd.Parameters.AddWithValue("@FAccountNo", ComboBox2.Text)
Dim rowseffected1 as Integer = cmd.ExecuteNonQuery()
cmd2 = New OleDbCommand("Update Table1 set To_Balance= To_Balance+@Tsum where To_Bank_Name=@TBankName and To_Account_No=@TAccountNo", con)
cmd2.Parameters.AddWithValue("@Tsum", TextBox2.Text)
cmd2.Parameters.AddWithValue("@TBankName", ComboBox3.Text)
cmd2.Parameters.AddWithValue("@TAccountNo", ComboBox4.Text)
Dim rowsaffected As Integer = cmd2.ExecuteNonQuery
cmd.Dispose()
cmd2.Dispose()
MsgBox("Number of rows affected from first Query are: " & rowseffected1.ToString())
MsgBox("Number of rows affected from second Query are: " & rowseffected.ToString())
ps Paul I tried to tell him that at least once already
Slight typo above - corrected in green above.
I think also the problem is that the database table isn't what I would expect, surely it should just have bank name, account number and balance only. Instead it seems to have the to and from halves of the equation.
And with that I am saying goodnight - don't want to get trapped in another 4 page marathon like yesterday!
-
Jul 20th, 2009, 02:09 PM
#25
Thread Starter
Frenzied Member
Re: Transfering Data from one part to other of database to other!!!
Errors in:
Code:
Dim rowseffected1 as Integer - cmd.ExecuteNonQuery()
End of statement expected
Code:
cmd2 = New OleDbCommand("Update Table1 set To_Balance= To_Balance+@Tsum where To_Bank_Name=@TBankName and To_Account_No=@TAccountNo", con)
cmd2 not declared
Code:
MsgBox("Number of rows affected from first Query are: " & rowsaffected1.ToString())
rowsaffected1 not declared...........
How to rectify these errors sir!!!
-
Jul 20th, 2009, 02:12 PM
#26
Thread Starter
Frenzied Member
Re: Transfering Data from one part to other of database to other!!!
I removed the 2nd one.........
how to remove the 1st and 3rd one?
-
Jul 20th, 2009, 02:15 PM
#27
Thread Starter
Frenzied Member
Re: Transfering Data from one part to other of database to other!!!
Code:
Dim rowsaffected As Integer = cmd2.ExecuteNonQuerycmd.Dispose()
The only error lies in this statement sir.....I resolved the rest
-
Jul 20th, 2009, 02:16 PM
#28
Thread Starter
Frenzied Member
Re: Transfering Data from one part to other of database to other!!!
Code:
Dim rowsaffected As Integer = cmd2.ExecuteNonQuerycmd.Dispose()
The error is:
ExecuteNonQuerycmd is not a member of system.data.oledb.oledbcommand....
Please help me sir......
-
Jul 20th, 2009, 02:23 PM
#29
Thread Starter
Frenzied Member
Re: Transfering Data from one part to other of database to other!!!
I corrected all the errors......
still the code did nt work.......
no msgbox displayed.........
even no change in the database........
Now what to do sir?
-
Jul 20th, 2009, 02:26 PM
#30
Re: Transfering Data from one part to other of database to other!!!
 Originally Posted by gautamshaw
Code:
Dim rowsaffected As Integer = cmd2.ExecuteNonQuerycmd.Dispose()
The error is:
ExecuteNonQuerycmd is not a member of system.data.oledb.oledbcommand....
Please help me sir......
You have two lines together. It should be
Code:
Dim rowsaffected As Integer = cmd2.ExecuteNonQuery
cmd.Dispose()
-
Jul 20th, 2009, 02:27 PM
#31
Re: Transfering Data from one part to other of database to other!!!
That must tell you something...... Like there is an error in the code that you are absoultely ignoring.....
vb.net Code:
Try
.......
Catch ex As Exception
End Try
You are placing your hands over your ear and yelling LALA-LALA-LALA All is perfect. Meanwhile VB is trying to tell you something is wrong
Ps this is my last post to the thread
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Jul 20th, 2009, 02:29 PM
#32
Thread Starter
Frenzied Member
Re: Transfering Data from one part to other of database to other!!!
Now it worked sir..............
Thanks a lot sir...............
I need the concept of the process that you did...........and what was wrong with my code sir?
Please remove my confusions a bit sir......
-
Jul 20th, 2009, 02:31 PM
#33
Thread Starter
Frenzied Member
Re: Transfering Data from one part to other of database to other!!!
What is the use of dispose() sir?
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
|