Solved

Insert multiple records into table from Select statement vb.net

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now