?
Solved

Inserting into a Database from a Dataset - Formula Help VB.Net Easy Question

Posted on 2005-03-30
5
Medium Priority
?
377 Views
Last Modified: 2010-04-24
I have an easy one.

I have a web method set-up that accepts a dataset from client.

 Dim myDA1 As SqlDataAdapter = New SqlDataAdapter

        myDA1.InsertCommand = New SqlCommand("Insert into Rec_Orders (order_Number,  qty, cost, value) Values (@order_Number,  @qty, @cost, (@cost * @qty))", myConn)

        myDA1.InsertCommand.Parameters.Add("@order_Number", SqlDbType.Int, 4, "order_Number")
        myDA1.InsertCommand.Parameters.Add("@qty", SqlDbType.Int, 9, "qty")
        myDA1.InsertCommand.Parameters.Add("@cost", SqlDbType.Int, 9, "cost")
     'right here ...I am talking about
        myDA1.InsertCommand.Parameters.Add("(@cost * @qty)", SqlDbType.Int, 9, "value")


        Dim param As SqlParameter = myDA1.InsertCommand.Parameters.Add("@Identity", SqlDbType.Int, 0, "id")
        param.Direction = ParameterDirection.Output

        myDA1.Update(ds1, "rec_Types")

My question is I am inserting into a table that has 4 fields.

OrderNumber
qty
cost
Value

The Value field is a field that is populated by     qty multipled by cost...ie (@qty * @cost)

Do I have this done right in my Insert Statement  and also have I got it right in the section where I declare my parameters.



Any insight  or help would be great.
0
Comment
Question by:nomar2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 4

Expert Comment

by:imperial_p79
ID: 13664209
I can see an extra comma in this piece of insert statement

 Values (@order_Number, ,

hope this is your problem!
0
 
LVL 34

Assisted Solution

by:Brian Crowe
Brian Crowe earned 100 total points
ID: 13664262
> myDA1.InsertCommand.Parameters.Add("(@cost * @qty)", SqlDbType.Int, 9, "value")

you don't need this parameter since @cost and @qty are already defined
0
 

Author Comment

by:nomar2
ID: 13664269
myDA1.InsertCommand = New SqlCommand("Insert into Rec_Orders (order_Number,  qty,   cost, value) Values (@order_Number,  @qty, @cost, (@cost * @qty))", myConn)


myDA1.InsertCommand.Parameters.Add("(@cost * @qty)", SqlDbType.Int, 9, "value")

Is the above the right way to handle...

1. the formula in the insert statement
2. setting the parameter for the variable that will fill the VALUE field
0
 
LVL 4

Accepted Solution

by:
imperial_p79 earned 100 total points
ID: 13664277
1. the formula in the insert statement is the right one
2. you dont have to set the parameter for the value field.
0
 

Author Comment

by:nomar2
ID: 13664311
Excellent...so I just need this

  myDA1.InsertCommand = New SqlCommand("Insert into Rec_Orders (order_Number,  qty, cost, value) Values (@order_Number,  @qty, @cost, (@cost * @qty))", myConn)

        myDA1.InsertCommand.Parameters.Add("@order_Number", SqlDbType.Int, 4, "order_Number")
        myDA1.InsertCommand.Parameters.Add("@qty", SqlDbType.Int, 9, "qty")
        myDA1.InsertCommand.Parameters.Add("@cost", SqlDbType.Int, 9, "cost")

...rest of the code

Thanks for the quick turnaround
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question