i have this two tables fields:

table1 (BOQSectionsAndParts_ID,BOQSection,BOQPart,ProjectNO) BOQSectionsAndParts_ID is a primary key
table2 (BOQItemEntry_ID,ProjectNO,BOQSectionsAndParts_ID,BOQ_Item) BOQItemEntry_ID is a primary key and BOQSectionsAndParts_ID is a forign key. this is a sql server
in my vb forum :
i view the BOQ sectin as a combobox
what i need is when the user choose the BOQSection the BOQSectionsAndParts_ID value save to table 2
i use DataGridView to save the value of table2.

anyhelp PLzZzz :)

My SQL is not the best, but you can do a select/insert like the following:

"INSERT INTO table2 (BOQSectionsAndParts_ID, BOQSection)
 SELECT BOQSectionsAndParts_ID, BOQSection
 FROM db.table1
 WHERE BOQSectionsAndParts_ID='" & ComboBox1.text & "'"
this is my code to insert into table 2 

   Public Sub add()
        Getconnection()

        Dim sql As String = "INSERT INTO BOQItemEntry(ProjectNO,BOQSectionsAndParts_ID,BOQ_Item) VALUES (@ProjectNO,@BOQSectionsAndParts_ID,@BOQ_Item)"
        Dim Cmd As New SqlCommand(sql, Me.conn)

        Cmd.Parameters.Add("@ProjectNO", SqlDbType.NVarChar).Value = _ProjectNO
        Cmd.Parameters.Add("@BOQSectionsAndParts_ID", SqlDbType.Int).Value = _BOQSectionsAndParts_ID
        Cmd.Parameters.Add("@BOQ_Item", SqlDbType.NVarChar).Value = _BOQ_Item
        Try
            Cmd.ExecuteNonQuery()
        Catch ex As SqlException
            Me._Msg = "Error Message"
        Finally
            Try
                Me.conn.Close()
            Catch
            End Try
        End Try
    End Sub

    how i can make your code work correct with my code. 
    i am trying to use your code no error but nothing happen.

this is the way iyou have to use

Public connectionString As String = "Data Source=HP-PC;Initial Catalog=VBP;user ID =sa; password= 1234"
 myConnection = New SqlConnection(connectionstring)
        myConnection.Open()



       Dim sqlcmd As New SqlCommand
                  sqlcmd  = New SqlCommand("Insert into table2 (BOQSectionsAndParts_ID, BOQSection) values (@ProjectNO,@BOQSectionsAndParts_ID,@BOQ_Item)", myConnection)


            MessageBox.Show("New Data is added")

myConnection.Close()

If you are using any of these methods, you will have to refresh your datagridview to show the updated records....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.