I have a page that inserts information to multiple tables. This part is working fine. What I want to add is when a user adds a new company not only is it inserting to the db but it is also posting the results on a separate page. For instance it will fill in some of the fields of a new form that will be submitted to create a new ticket. I'm not sure how to this part.
This is the code I have for currently:
Imports System
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Web.UI.WebControls
Partial Public Class DisAddCo
Inherits System.Web.UI.Page
Protected Sub FormView1_ItemInserting(ByVal sender As Object, ByVal e As FormViewInsertEventArgs)
' Retrieve controls
Dim CompanyTextBox As TextBox = TryCast(FormView1.FindControl("CompanyTextBox"), TextBox)
Dim FirstNameTextBox As TextBox = TryCast(FormView1.FindControl("FirstNameTextBox"), TextBox)
Dim LastNameTextBox As TextBox = TryCast(FormView1.FindControl("LastNameTextBox"), TextBox)
Dim Address1TextBox As TextBox = TryCast(FormView1.FindControl("Address1TextBox"), TextBox)
Dim Address2TextBox As TextBox = TryCast(FormView1.FindControl("Address2TextBox"), TextBox)
Dim CityTextBox As TextBox = TryCast(FormView1.FindControl("CityTextBox"), TextBox)
Dim StateTextBox As TextBox = TryCast(FormView1.FindControl("StateTextBox"), TextBox)
Dim ZipTextBox As TextBox = TryCast(FormView1.FindControl("ZipTextBox"), TextBox)
Dim PhoneTextBox As TextBox = TryCast(FormView1.FindControl("PhoneTextBox"), TextBox)
If CompanyTextBox Is Nothing Then Return
If FirstNameTextBox Is Nothing Then Return
If LastNameTextBox Is Nothing Then Return
If Address1TextBox Is Nothing Then Return
If CityTextBox Is Nothing Then Return
If StateTextBox Is Nothing Then Return
If ZipTextBox Is Nothing Then Return
If PhoneTextBox Is Nothing Then Return
Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("HRIServiceConnectionString1").ConnectionString)
Dim cmd As New SqlCommand("INSERT INTO [Customers] ([Company], [Address1], [Address2], [Phone]) VALUES (@Company, @Address1, @Address2, @Phone); SELECT SCOPE_IDENTITY();", conn)
cmd.Parameters.AddWithValue("@Company", CompanyTextBox.Text)
cmd.Parameters.AddWithValue("@Address1", Address1TextBox.Text)
cmd.Parameters.AddWithValue("@Address2", Address2TextBox.Text)
cmd.Parameters.AddWithValue("@Phone", PhoneTextBox.Text)
Try
conn.Open()
Dim cusID As Integer = Convert.ToInt32(cmd.ExecuteScalar())
cmd = New SqlCommand("INSERT INTO [Contacts] ([cusID], [FirstName], [LastName]) VALUES (@cusID, @FirstName, @LastName); SELECT SCOPE_IDENTITY();", conn)
cmd.Parameters.AddWithValue("@cusID", cusID)
cmd.Parameters.AddWithValue("@FirstName", FirstNameTextBox.Text)
cmd.Parameters.AddWithValue("@LastName", LastNameTextBox.Text)
cmd.ExecuteScalar()
cmd = New SqlCommand("INSERT INTO [Zip] ([cusID], [City], [State], [Zip]) VALUES (@cusID, @City, @State, @Zip)", conn)
cmd.Parameters.AddWithValue("@cusID", cusID)
cmd.Parameters.AddWithValue("@City", CityTextBox.Text)
cmd.Parameters.AddWithValue("@State", StateTextBox.Text)
cmd.Parameters.AddWithValue("@Zip", ZipTextBox.Text)
cmd.ExecuteScalar()
Finally
If conn.State = System.Data.ConnectionState.Open Then conn.Close()
End Try
'Response.Redirect("default.aspx")
End Sub
End Class