We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,127 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

I am confused and I need some help with VB.NET/SQL Server/ADO.NET

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.

1
Contributor
2
Replies
17 Hours
Discussion Span
5 Years Ago
Last Updated
5
Views
Question
Answered
Jesi523
Junior Poster
104 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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
Jesi523
Junior Poster
104 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

I think I figured it out.

Jesi523
Junior Poster
104 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Self-Answered as of 5 Years Ago

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0643 seconds using 2.69MB