| | |
Insert to DB & View Results on Different Page
Please support our ASP.NET advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
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:
ASP.NET Syntax (Toggle Plain Text)
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,080
Reputation:
Solved Threads: 68
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 10:38 am.
•
•
Join Date: Sep 2007
Posts: 1,080
Reputation:
Solved Threads: 68
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:
ASP.NET Syntax (Toggle Plain Text)
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)
ASP.NET Syntax (Toggle Plain Text)
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 Class
This 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 11:45 am.
![]() |
Other Threads in the ASP.NET Forum
- Previous Thread: login validation
- Next Thread: session
Views: 6694 | Replies: 140
| Thread Tools | Search this Thread |
Tag cloud for ASP.NET
.net 2.0 3.5 ajax appliances application asp asp.net beginner box browser businesslogiclayer button c# c#gridviewcolumn cac checkbox child class compatible complex confirmationcodegeneration content contenttype control countryselector courier database datagrid datagridview datalist deployment development dgv dialog dropdown dropdownmenu dynamic edit embeddingactivexcontrol feedback fileuploader findcontrol flash flv folder form forms gridview gudi homeedition hosting identity iis image index javascript jquery languages list maps menu mobile mssql nameisnotdeclared novell order problem profile ratings redirect refer relationaldatabases response.redirect search security select serializesmo.table sessionvariables silverlight software sql ssl tracking treeview typeof validatedate validation vb vb.net vista visual-studio visualstudio vs2008 web webarchitecture webdevelopment wizard xml xsl






