•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the ASP.NET section within the Web Development category of DaniWeb, a massive community of 374,006 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,845 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our ASP.NET advertiser: Lunarpages ASP Web Hosting
Views: 2807 | Replies: 140
![]() |
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:
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
•
•
Join Date: Sep 2007
Posts: 1,054
Reputation:
Rep Power: 3
Solved Threads: 61
Okay, then where you have the response redirect at the end of your code, put this:
Response.Redirect("default.aspx?id=" & cusID)
Replace default.aspx with whatever page it is going to. Then on that following page have something like this:
Response.Redirect("default.aspx?id=" & cusID)
Replace default.aspx with whatever page it is going to. Then on that following page have something like this:
Dim id As String = Trim(Request.QueryString("id"))
Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("HRIServiceConnectionString1")
Dim cmd As New SqlCommand("SELECT * FROM Customers WHERE cusID=@cusID", conn)
cmd.Parameters.AddWithValue("@cusID", id)
Try
conn.Open()
Dim dtrReader As SqlDataReader = cmd.ExecuteReader()
if dtrReader.HasRows then
'found record with that id
while dtrReader.Read()
'set your textboxes here
tbCompanyname.Text = dtrReader("Company")
tbAddress1.Text = dtrReader("address1")
'this is sample code. follow the pattern
end while
else
'no customer found for that id. redirect.
response.redirect("default.aspx")
end if
dtrReader.Close()
conn.Close()
Catch ex As SqlException
response.write(ex)
End Try Last edited by SheSaidImaPregy : Feb 19th, 2008 at 9:38 am.
•
•
Join Date: Sep 2007
Posts: 1,054
Reputation:
Rep Power: 3
Solved Threads: 61
Ok. Then so on this page with the form, once you insert the information, redirect to the page back there with a querystring.
Response.Redirect("default.aspx?cusID=" & cusID & "&conID=" & conID)
Then on that page, (copied code from before) do this under page_load AFTER you populated the dropdownlists:
You also need to change the INSERT to:
Something like that should work.
This sets the page_load to call the selectedindexchanged events so you do not have to redo code.
Response.Redirect("default.aspx?cusID=" & cusID & "&conID=" & conID)
Then on that page, (copied code from before) do this under page_load AFTER you populated the dropdownlists:
Dim cusID As String = Trim(Request.QueryString("cusID"))
Dim conID As String = Trim(Request.QueryString("conID"))
DropDownList1.Items.FindByValue(cusID).Selected = True
Dim eventarg As New EventArgs
Call DropDownList1_SelectedIndexChanged(DropDownList1, eventarg.Empty)
DropDownList2.Items.FindByValue(conID).Selected = True
eventarg = New EventArgs
Call DropDownList2_SelectedIndexChanged(DropDownList2, eventarg.Empty)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)
Dim conID As Integer = Conver.ToInt32(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.ExecuteNonQuery()
Finally
If conn.State = System.Data.ConnectionState.Open Then conn.Close()
End Try
Response.Redirect("default.aspx?cusID=" & cusID & "&conID=" & conID)
End Sub
End ClassThis sets the page_load to call the selectedindexchanged events so you do not have to redo code.
Last edited by SheSaidImaPregy : Feb 19th, 2008 at 10:45 am.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb ASP.NET Marketplace
- Previous Thread: login validation
- Next Thread: session



Linear Mode