Insert to DB & View Results on Different Page

Reply

Join Date: Feb 2008
Posts: 179
Reputation: foundsheep is an unknown quantity at this point 
Solved Threads: 0
foundsheep's Avatar
foundsheep foundsheep is offline Offline
Junior Poster

Insert to DB & View Results on Different Page

 
0
  #1
Feb 19th, 2008
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:

  1.  
  2. Imports System
  3. Imports System.Configuration
  4. Imports System.Data.SqlClient
  5. Imports System.Web.UI.WebControls
  6.  
  7. Partial Public Class DisAddCo
  8. Inherits System.Web.UI.Page
  9. Protected Sub FormView1_ItemInserting(ByVal sender As Object, ByVal e As FormViewInsertEventArgs)
  10. ' Retrieve controls
  11. Dim CompanyTextBox As TextBox = TryCast(FormView1.FindControl("CompanyTextBox"), TextBox)
  12. Dim FirstNameTextBox As TextBox = TryCast(FormView1.FindControl("FirstNameTextBox"), TextBox)
  13. Dim LastNameTextBox As TextBox = TryCast(FormView1.FindControl("LastNameTextBox"), TextBox)
  14. Dim Address1TextBox As TextBox = TryCast(FormView1.FindControl("Address1TextBox"), TextBox)
  15. Dim Address2TextBox As TextBox = TryCast(FormView1.FindControl("Address2TextBox"), TextBox)
  16. Dim CityTextBox As TextBox = TryCast(FormView1.FindControl("CityTextBox"), TextBox)
  17. Dim StateTextBox As TextBox = TryCast(FormView1.FindControl("StateTextBox"), TextBox)
  18. Dim ZipTextBox As TextBox = TryCast(FormView1.FindControl("ZipTextBox"), TextBox)
  19. Dim PhoneTextBox As TextBox = TryCast(FormView1.FindControl("PhoneTextBox"), TextBox)
  20.  
  21. If CompanyTextBox Is Nothing Then Return
  22. If FirstNameTextBox Is Nothing Then Return
  23. If LastNameTextBox Is Nothing Then Return
  24. If Address1TextBox Is Nothing Then Return
  25. If CityTextBox Is Nothing Then Return
  26. If StateTextBox Is Nothing Then Return
  27. If ZipTextBox Is Nothing Then Return
  28. If PhoneTextBox Is Nothing Then Return
  29.  
  30. Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("HRIServiceConnectionString1").ConnectionString)
  31.  
  32. Dim cmd As New SqlCommand("INSERT INTO [Customers] ([Company], [Address1], [Address2], [Phone]) VALUES (@Company, @Address1, @Address2, @Phone); SELECT SCOPE_IDENTITY();", conn)
  33. cmd.Parameters.AddWithValue("@Company", CompanyTextBox.Text)
  34. cmd.Parameters.AddWithValue("@Address1", Address1TextBox.Text)
  35. cmd.Parameters.AddWithValue("@Address2", Address2TextBox.Text)
  36. cmd.Parameters.AddWithValue("@Phone", PhoneTextBox.Text)
  37.  
  38. Try
  39. conn.Open()
  40.  
  41. Dim cusID As Integer = Convert.ToInt32(cmd.ExecuteScalar())
  42.  
  43. cmd = New SqlCommand("INSERT INTO [Contacts] ([cusID], [FirstName], [LastName]) VALUES (@cusID, @FirstName, @LastName); SELECT SCOPE_IDENTITY();", conn)
  44. cmd.Parameters.AddWithValue("@cusID", cusID)
  45. cmd.Parameters.AddWithValue("@FirstName", FirstNameTextBox.Text)
  46. cmd.Parameters.AddWithValue("@LastName", LastNameTextBox.Text)
  47.  
  48. cmd.ExecuteScalar()
  49.  
  50. cmd = New SqlCommand("INSERT INTO [Zip] ([cusID], [City], [State], [Zip]) VALUES (@cusID, @City, @State, @Zip)", conn)
  51. cmd.Parameters.AddWithValue("@cusID", cusID)
  52. cmd.Parameters.AddWithValue("@City", CityTextBox.Text)
  53. cmd.Parameters.AddWithValue("@State", StateTextBox.Text)
  54. cmd.Parameters.AddWithValue("@Zip", ZipTextBox.Text)
  55.  
  56. cmd.ExecuteScalar()
  57. Finally
  58. If conn.State = System.Data.ConnectionState.Open Then conn.Close()
  59. End Try
  60.  
  61. 'Response.Redirect("default.aspx")
  62.  
  63. End Sub
  64.  
  65. End Class
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Insert to DB & View Results on Different Page

 
0
  #2
Feb 19th, 2008
I'm a bit confused on this part, but are you trying to insert the new information, then redirect the page to a new ticket page, where most of the data is already loaded into the fields?
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 179
Reputation: foundsheep is an unknown quantity at this point 
Solved Threads: 0
foundsheep's Avatar
foundsheep foundsheep is offline Offline
Junior Poster

Re: Insert to DB & View Results on Different Page

 
0
  #3
Feb 19th, 2008
The info submitted on this page is needed before a new service ticket is created. So, once this has been inserted in the database I'd like it to automatically go to the insert new ticket form and the user will fill out the rest of the necessary info.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Insert to DB & View Results on Different Page

 
0
  #4
Feb 19th, 2008
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:
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 179
Reputation: foundsheep is an unknown quantity at this point 
Solved Threads: 0
foundsheep's Avatar
foundsheep foundsheep is offline Offline
Junior Poster

Re: Insert to DB & View Results on Different Page

 
0
  #5
Feb 19th, 2008
Should this go in a new protected sub? BTW, this is on the same page with the drop downs we've been working on.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Insert to DB & View Results on Different Page

 
0
  #6
Feb 19th, 2008
Ok good to know, let me see if I have this straight. You will have a page where they will add a company or become a customer, then it will lead to the dropdownlist page where the information should already be populated. Right?
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 179
Reputation: foundsheep is an unknown quantity at this point 
Solved Threads: 0
foundsheep's Avatar
foundsheep foundsheep is offline Offline
Junior Poster

Re: Insert to DB & View Results on Different Page

 
0
  #7
Feb 19th, 2008
Here is the structure:
Company DDL
Contact DDL
Add Customer/Company Link: (takes you to new page to fill out form)

Dispatch form that is populated by ddls or add Customer Page
This is the one that include the equipment ddl
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Insert to DB & View Results on Different Page

 
0
  #8
Feb 19th, 2008
Then after you fill out the Addcustomer/company form, it redirects you back to that page, where the information is pre-filled out with the information submitted on the form, right?
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 179
Reputation: foundsheep is an unknown quantity at this point 
Solved Threads: 0
foundsheep's Avatar
foundsheep foundsheep is offline Offline
Junior Poster

Re: Insert to DB & View Results on Different Page

 
0
  #9
Feb 19th, 2008
You got it.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Insert to DB & View Results on Different Page

 
0
  #10
Feb 19th, 2008
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:
  1. Dim cusID As String = Trim(Request.QueryString("cusID"))
  2. Dim conID As String = Trim(Request.QueryString("conID"))
  3. DropDownList1.Items.FindByValue(cusID).Selected = True
  4. Dim eventarg As New EventArgs
  5. Call DropDownList1_SelectedIndexChanged(DropDownList1, eventarg.Empty)
  6. DropDownList2.Items.FindByValue(conID).Selected = True
  7. eventarg = New EventArgs
  8. Call DropDownList2_SelectedIndexChanged(DropDownList2, eventarg.Empty)
You also need to change the INSERT to:
  1. Imports System
  2. Imports System.Configuration
  3. Imports System.Data.SqlClient
  4. Imports System.Web.UI.WebControls
  5.  
  6. Partial Public Class DisAddCo
  7. Inherits System.Web.UI.Page
  8. Protected Sub FormView1_ItemInserting(ByVal sender As Object, ByVal e As FormViewInsertEventArgs)
  9. ' Retrieve controls
  10. Dim CompanyTextBox As TextBox = TryCast(FormView1.FindControl("CompanyTextBox"), TextBox)
  11. Dim FirstNameTextBox As TextBox = TryCast(FormView1.FindControl("FirstNameTextBox"), TextBox)
  12. Dim LastNameTextBox As TextBox = TryCast(FormView1.FindControl("LastNameTextBox"), TextBox)
  13. Dim Address1TextBox As TextBox = TryCast(FormView1.FindControl("Address1TextBox"), TextBox)
  14. Dim Address2TextBox As TextBox = TryCast(FormView1.FindControl("Address2TextBox"), TextBox)
  15. Dim CityTextBox As TextBox = TryCast(FormView1.FindControl("CityTextBox"), TextBox)
  16. Dim StateTextBox As TextBox = TryCast(FormView1.FindControl("StateTextBox"), TextBox)
  17. Dim ZipTextBox As TextBox = TryCast(FormView1.FindControl("ZipTextBox"), TextBox)
  18. Dim PhoneTextBox As TextBox = TryCast(FormView1.FindControl("PhoneTextBox"), TextBox)
  19.  
  20. If CompanyTextBox Is Nothing Then Return
  21. If FirstNameTextBox Is Nothing Then Return
  22. If LastNameTextBox Is Nothing Then Return
  23. If Address1TextBox Is Nothing Then Return
  24. If CityTextBox Is Nothing Then Return
  25. If StateTextBox Is Nothing Then Return
  26. If ZipTextBox Is Nothing Then Return
  27. If PhoneTextBox Is Nothing Then Return
  28.  
  29. Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("HRIServiceConnectionString1").ConnectionString)
  30.  
  31. Dim cmd As New SqlCommand("INSERT INTO [Customers] ([Company], [Address1], [Address2], [Phone]) VALUES (@Company, @Address1, @Address2, @Phone); SELECT SCOPE_IDENTITY();", conn)
  32. cmd.Parameters.AddWithValue("@Company", CompanyTextBox.Text)
  33. cmd.Parameters.AddWithValue("@Address1", Address1TextBox.Text)
  34. cmd.Parameters.AddWithValue("@Address2", Address2TextBox.Text)
  35. cmd.Parameters.AddWithValue("@Phone", PhoneTextBox.Text)
  36.  
  37. Try
  38. conn.Open()
  39.  
  40. Dim cusID As Integer = Convert.ToInt32(cmd.ExecuteScalar())
  41.  
  42. cmd = New SqlCommand("INSERT INTO [Contacts] ([cusID], [FirstName], [LastName]) VALUES (@cusID, @FirstName, @LastName); SELECT SCOPE_IDENTITY();", conn)
  43. cmd.Parameters.AddWithValue("@cusID", cusID)
  44. cmd.Parameters.AddWithValue("@FirstName", FirstNameTextBox.Text)
  45. cmd.Parameters.AddWithValue("@LastName", LastNameTextBox.Text)
  46.  
  47. Dim conID As Integer = Conver.ToInt32(cmd.ExecuteScalar())
  48.  
  49. cmd = New SqlCommand("INSERT INTO [Zip] ([cusID], [City], [State], [Zip]) VALUES (@cusID, @City, @State, @Zip)", conn)
  50. cmd.Parameters.AddWithValue("@cusID", cusID)
  51. cmd.Parameters.AddWithValue("@City", CityTextBox.Text)
  52. cmd.Parameters.AddWithValue("@State", StateTextBox.Text)
  53. cmd.Parameters.AddWithValue("@Zip", ZipTextBox.Text)
  54.  
  55. cmd.ExecuteNonQuery()
  56. Finally
  57. If conn.State = System.Data.ConnectionState.Open Then conn.Close()
  58. End Try
  59.  
  60. Response.Redirect("default.aspx?cusID=" & cusID & "&conID=" & conID)
  61.  
  62. End Sub
  63.  
  64. End Class
  65.  
Something like that should work.

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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the ASP.NET Forum


Views: 6694 | Replies: 140
Thread Tools Search this Thread



Tag cloud for ASP.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC