9 Replies - 1425 Views - Last Post: 31 October 2008 - 12:05 PM Rate Topic: -----

#1 Mangore  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 177
  • Joined: 11-October 08

How to insert the data from table to another table

Posted 27 October 2008 - 06:39 PM

This code is to insert the item from listview to table(TBbills ),.
Is ther any way to insert the data from Table to another tABLE
Dim cmd As New OleDbCommand
        cmd.Connection = conn
        conn.Open()

        Dim i As Integer

        For i = 0 To Form1.ListView1.Items.Count - 1
            cmd.CommandText = "INSERT INTO TBbills (BNumber,BType,BHistory) VALUES (@BNumber,@BType,@BHistory) "
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@BNumber", Form1.ListView1.Items(i).SubItems(2).Text) ' the total
 cmd.Parameters.AddWithValue("@BType", Form1.ListView1.Items(i).SubItems(1).Text)
            cmd.Parameters.AddWithValue("@BHistory", Form1.ListView1.Items(i).SubItems(0).Text)
cmd.ExecuteNonQuery()

        Next i

        conn.Close()

This post has been edited by Mangore: 27 October 2008 - 06:41 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How to insert the data from table to another table

#2 fyrestorm  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 10
  • View blog
  • Posts: 3,113
  • Joined: 04-April 02

Re: How to insert the data from table to another table

Posted 27 October 2008 - 06:46 PM

Wouldn't it make sense to just write another insert statement?
Was This Post Helpful? 0
  • +
  • -

#3 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: How to insert the data from table to another table

Posted 27 October 2008 - 06:49 PM

Yes, create a Stored Procedure to write the values to as many tables as you need to.
Was This Post Helpful? 0
  • +
  • -

#4 Hary  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 427
  • Joined: 23-September 08

Re: How to insert the data from table to another table

Posted 28 October 2008 - 11:15 AM

You can use a SELECT-subquery to generate the data for the INSERT-query:

INSERT INTO Table2(a,b,c) VALUE (SELECT a,b,c FROM Table1)

*(Note: Support for subqueries is depending on your DBMS)
Was This Post Helpful? 0
  • +
  • -

#5 Mangore  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 177
  • Joined: 11-October 08

Re: How to insert the data from table to another table

Posted 31 October 2008 - 01:19 AM

View Poste-informationplace.com, on 30 Oct, 2008 - 04:11 PM, said:

Do you want to automatically copy the whole data from a table to another table? Because I don't see where you included that code. You can do this through a form, through data relations.
Give me more infor about your issue.

If you take a look to post#1. I want to put insetead ot ListView the second table.
Was This Post Helpful? 0
  • +
  • -

#6 Hary  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 427
  • Joined: 23-September 08

Re: How to insert the data from table to another table

Posted 31 October 2008 - 02:27 AM

Where is the code that created the ListView? What is it exactly? What is in it?
Was This Post Helpful? 0
  • +
  • -

#7 Mangore  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 177
  • Joined: 11-October 08

Re: How to insert the data from table to another table

Posted 31 October 2008 - 04:16 AM

View PostHary, on 31 Oct, 2008 - 02:27 AM, said:

Where is the code that created the ListView? What is it exactly? What is in it?

Here si the code I want to insert the data from TBbills to another Table as I inserted the Data from ListView to TBbills.
Dim cmd As New OleDbCommand  
        cmd.Connection = conn  
        conn.Open()  
  
        Dim i As Integer  
  
        For i = 0 To Form1.ListView1.Items.Count - 1  
            cmd.CommandText = "INSERT INTO TBbills (BNumber,BType,BHistory) VALUES (@BNumber,@BType,@BHistory) "  
cmd.CommandType = CommandType.Text  
cmd.Parameters.AddWithValue("@BNumber", Form1.ListView1.Items(i).SubItems(2).Text) ' the total  
 cmd.Parameters.AddWithValue("@BType", Form1.ListView1.Items(i).SubItems(1).Text)  
            cmd.Parameters.AddWithValue("@BHistory", Form1.ListView1.Items(i).SubItems(0).Text)  
cmd.ExecuteNonQuery()  
  
        Next i  
  
        conn.Close()  

Was This Post Helpful? 0
  • +
  • -

#8 Hary  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 427
  • Joined: 23-September 08

Re: How to insert the data from table to another table

Posted 31 October 2008 - 07:03 AM

I did read your first post and I've seen this code. But I still do not know what ListView1 contains?
Was This Post Helpful? 0
  • +
  • -

#9 Mangore  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 177
  • Joined: 11-October 08

Re: How to insert the data from table to another table

Posted 31 October 2008 - 08:41 AM

View PostHary, on 31 Oct, 2008 - 07:03 AM, said:

I did read your first post and I've seen this code. But I still do not know what ListView1 contains?


It contain data( text-date-number..)
Was This Post Helpful? 0
  • +
  • -

#10 Hary  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 427
  • Joined: 23-September 08

Re: How to insert the data from table to another table

Posted 31 October 2008 - 12:05 PM

Ah, then a second query solves your problem the easiest way. Just copy the code in the FOR-loop, and change the query the second time.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1