SQL Calculations via Visual Basic
Hello all,
I've recently ventured out into incorporating SQL databases with Visual Basic, and I'll admit, I'm very intimidated by SQL. I'm relatively new to programming in general, but am quite comfortable with my VB skills which is why I prefer to try and use VB for what I'm about to do. I'm open to any suggestions though. Ok, here goes.
I have a SQL Database titled "FramedArtDataSet1" which contains a data table titled "FramedArt" and it's organized in the following manner:
ProjectID ProjectName HoursTotal LaborRate LaborCost
1 Project A 25.25 10.00
2 Project B 3.75 10.00
3 Project X 10.25 10.00
4 Project Y 16.25 10.00
The project ID is my primary key (integer), project name is varchar(250), Hours Total and Labor Rate are both decimal(18,2) (because I want rounding, rather than small money). I would like to be able to simply multiply HoursTotal*LaborRate and place the result into my LaborCost column. I thought I had this issue resolved, but no. Here is my code that I tried to use:
Public
Class frmFramedArtPrivateSub FramedArtBindingNavigatorSaveItem_Click(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles FramedArtBindingNavigatorSaveItem.Click
Me.Validate()
Me.FramedArtBindingSource.EndEdit()
Me.FramedArtTableAdapter.Update(Me.ArtDatabaseDataset1.FramedArt)
EndSub
PrivateSub frmFramedArt_Load(ByVal senderAs System.Object,ByVal eAs System.EventArgs)HandlesMyBase.Load'TODO: This line of code loads data into the 'ArtDatabaseDataset1.FramedArt' table. You can move, or remove it, as needed.Me.FramedArtTableAdapter.Fill(Me.ArtDatabaseDataset1.FramedArt)Call FramedArtUpdate()EndSubPrivateSub FramedArtUpdate()ArtDatabaseDataset1.Tables(
"FramedArt").Columns("LaborCost").Expression ="HoursTotal*LaborRate"EndSubEnd
ClassThe error I am receiving (that many of you may have already anticipated) is something along the lines of "The column mapping from SourceColumn 'LaborCost' failed because the DataColumn 'LaborCost' is a computed column." I understand the reason it's giving me the error is because a computed column is read only. I would prefer to store that calculation in the dataset, BUT, if I can accomplish all of this using SQL through VB, that's fine with me, but I'm not sure how. I have looked into many sources and have been confused by some of them. Another method that was described on a blog was to generate a new table, make my calculations in that table, then "copy/paste" the calculated values back into my table of interest, but again, I could not figure this method out. Right now, I just don't even know where to "start" because I'm just frustrated. I'm not opposed to any single method in particular, but I definitely need help working with the dataset. I might also add that, if possible, I'd like this calculation to be made for each record as it's being viewed. If anyone requires more information to help me resolve this issue, please let me know. I thank you all in advance for your assistance.
Regards,
Steve G.

