Solved

Insert multiple records into table from Select statement vb.net

Posted on 2015-01-07
3
303 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
[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
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

Industry Leaders: 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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
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.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

732 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