I have been using VB.Net for a little while now, but I just started needing to write code to put information in a SQL Server database. I have two tables that I need to add information into. I know that I have to use an Insert statement, maybe I need to use two insert statements. I understand how to open a connection to the database. I just do not understand how to use ADO.NET in VB.NET to get information from text boxes on my form into the database. I might also add that both tables use identity columns and one table has the idenity column of the other table as the foriegn key. If someone could please give me an example I would really apprieciate it.

Here's the code I tried to use, but obviously you can't update two table in one insert statement because my code doesn't work:

Private Sub btnSaveToDB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveToDB.Click

        Dim connection As SqlClient.SqlConnection = getConnection()
        Dim insertStatement As String _
            = "INSERT Operations, Operation_Dates" _
            & "(Operation, Date) " _
            & "VALUES (@Operation , @Date )"
        Dim insertCommand As New SqlClient.SqlCommand(insertStatement, connection)
        insertCommand.Parameters.AddWithValue("@Operation", txtOperation.Text.Trim)
        insertCommand.Parameters.AddWithValue("@Date", txtDate.Text.Trim)
        Try
            connection.Open()
            Dim operationCount As Integer = insertCommand.ExecuteNonQuery
        Catch ex As SqlClient.SqlException
            MessageBox.Show(ex.Message)
        Finally
            connection.Close()
        End TryPrivate Sub btnSaveToDB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveToDB.Click

        Dim connection As SqlClient.SqlConnection = getConnection()
        Dim insertStatement As String _
            = "INSERT Operations, Operation_Dates" _
            & "(Operation, Date) " _
            & "VALUES (@Operation , @Date )"
        Dim insertCommand As New SqlClient.SqlCommand(insertStatement, connection)
        insertCommand.Parameters.AddWithValue("@Operation", txtOperation.Text.Trim)
        insertCommand.Parameters.AddWithValue("@Date", txtDate.Text.Trim)
        Try
            connection.Open()
            Dim operationCount As Integer = insertCommand.ExecuteNonQuery
        Catch ex As SqlClient.SqlException
            MessageBox.Show(ex.Message)
        Finally
            connection.Close()
        End Try

I think I figured it out.

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.