Solved

Insert multiple records into table from Select statement vb.net

Posted on 2015-01-07
3
296 Views
Last Modified: 2015-01-07
Hi all.

I have the following code below that pulls records from the SQL select statement and inserts them into the ItemQuote_CostDetail table. Unfortunately, when there are more than 1 records returned in the select statement it only inserts the first record into the table. How can I do this? I was thinking a for loop but I don't know how to get started.

Thank you in advance!

cmd.CommandText = "SELECT count(*) As count FROM ItemQuote WHERE Status = 'Active' AND MasterItemNumber = @MasterItemNumberA AND RawMaterialNumber = @RawMaterialNumberA AND CostType = @CostTypeA"
            cmd.Parameters.AddWithValue("@MasterItemNumberA", DataGrid_Pending.SelectedRows(0).Cells("MasterItemNumber").Value)
            cmd.Parameters.AddWithValue("@RawMaterialNumberA", DataGrid_Pending.SelectedRows(0).Cells("RawMaterialNumber").Value)
            cmd.Parameters.AddWithValue("@CostTypeA", DataGrid_Pending.SelectedRows(0).Cells("CostType").Value)

            dr = cmd.ExecuteReader
            dr.Read()
           
            If dr("count") >= 1 Then

                Dim quotenumber As Integer
                cmd.CommandText = "SELECT QuoteNumber FROM ItemQuote WHERE Status = 'Active' AND MasterItemNumber = @MasterItemNumberB AND RawMaterialNumber = @RawMaterialNumberB AND CostType = @CostTypeB"

                cmd.Parameters.AddWithValue("@MasterItemNumberB", DataGrid_Pending.SelectedRows(0).Cells("MasterItemNumber").Value)
                cmd.Parameters.AddWithValue("@RawMaterialNumberB", DataGrid_Pending.SelectedRows(0).Cells("RawMaterialNumber").Value)
                cmd.Parameters.AddWithValue("@CostTypeB", DataGrid_Pending.SelectedRows(0).Cells("CostType").Value)

                dr.Close()
                con.Close()

                con.Open()

                cmd.Connection = con

                dr = cmd.ExecuteReader

                dr.Read()
                quotenumber = dr("QuoteNumber")
                dr.Close()

                cmd.CommandText = "INSERT INTO ItemQuote_CostDetail(QuoteNumber, PriceSheetID, CreateDate) VALUES (@QuoteNumberC, @PriceSheetIDC, @CreateDateC) "
                cmd.Parameters.AddWithValue("@QuoteNumberC", quotenumber)
                cmd.Parameters.AddWithValue("@PriceSheetIDC", pricesheetID)
                cmd.Parameters.AddWithValue("@CreateDateC", DateTime.Now.ToString("MMMM dd, yyyy"))
                cmd.ExecuteNonQuery()

End If

Open in new window

0
Comment
Question by:printmedia
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
Beartlaoi earned 500 total points
ID: 40536343
On line 27 change it to

While dr.Read()

Change line 36

End While

Move line 29 to below line 36 so dr does not close until the While loop is finished

Read() returns True when there are rows left.  Currently if your select returned no rows you would get errors.
0
 

Author Comment

by:printmedia
ID: 40536360
Thank you for your reply Beartlaoi.

Now I get an error at the line:
While dr.Read()

Open in new window


The errors says: Invalid attempt to call Read when reader is closed.
0
 

Author Comment

by:printmedia
ID: 40536412
Nevermind I figured it out. I can't read and then insert records in the same code body because you can only have one open connection. So instead of doing the insert right below the reader, I created a Sub to do the insert and I simply call it from within the while loop.

Thanks again!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
vb.net 1 month apart 11 38
Optimize the query 5 49
Using/Import statement in code doesn't see reference of dll just added. 3 25
Need help with a query 3 36
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

680 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