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.
Related Article: Need a Help with Vb Project
is a VB.NET discussion thread by sanhitabhide that has 3 replies and was last updated 5 years ago.
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