Help with ASP code

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 5
Reputation: happyhappy is an unknown quantity at this point 
Solved Threads: 0
happyhappy happyhappy is offline Offline
Newbie Poster

Help with ASP code

 
0
  #1
Jan 9th, 2008
Hi, everyone out there I am new to Daniweb but have been reading the forums. I'm glad that I joined. I am in desperate need of help in coding a web page that grabs the name and email address of all the peoples from a database and then when the viewer clicks on a person a form will be displayed so the user can enter their Name, Email and write a message, then when they click submit the message will be sent to the email of the person they clicked on. I have ben working on this for a month now, but still have no results. Any help would be greatly appreciated.
Thanks.
HappyHappy
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: Help with ASP code

 
0
  #2
Jan 15th, 2008
What you need to do is pull all the information on the database out using something like:
  1. "SELECT DISTINCT UserID, UserName FROM Users WHERE NOT UserID IS NULL AND NOT UserName IS NULL"
Depending on the size of your database and the records it contains, you may want to add a paging function for this and limit the amount of users shown. It would be difficult to find one user if 2,000 are listed on the same page.

Then you will need to display them accordingly. You can use a DataList for the functionality of multiple columns ordered any way you wish, or you can user a repeater for ease, but you will only have 1 column unless you split it up, which you should do the datalist then.

Then you will create it like this:
  1. <asp:DataList ID="dlUsers" RepeatColumns="3" runat="server>
  2. <HeaderTemplate>
  3. <!-- if needed, use it. otherwise get rid of it -->
  4. <strong>Users</strong>
  5. </HeaderTemplate>
  6. <ItemTemplate>
  7. <!-- Use <A> if you are going to have the form on another page, otherwise use controls like LinkButton or Label. This is not easy with only ASP.NET, you will need javascript if you wish it to be same page as well. -->
  8. <a href="mailpage.aspx?id=<%# DataBinder.Eval(Container.DataItem, "UserID") %>&name=<%# DataBinder.Eval(Container.DataItem, "UserName") %>"><%# DataBinder.Eval(Container.DataItem, "UserName") %></a>
  9. </ItemTemplate>
  10. <FooterTemplate>
  11. <!-- if don't need, get rid of FooterTemplate -->
  12. </FooterTemplate>
  13.  
Then on your mailpage.aspx, use a query to pull the information you need from the querystring and database:
  1. <script language="vb" runat="server">
  2. Sub SendEmail(ByVal S As Object, ByVal E As EventArgs)
  3. Dim conPubs As New OdbcConnection( "connstring" )
  4. Dim cmdSelect As New OdbcCommand( "SELECT UserEmail FROM Users WHERE UserID=?", conPubs )
  5. cmdSelect.Parameters.AddWithValue( "?UserID", Trim(Request.QueryString("id")).ToString )
  6. conPubs.Open()
  7. Dim dtrReader As OdbcDataReader = cmdSelect.ExecuteReader()
  8. if dtrReader.HasRows then
  9. while dtrReader.Read()
  10. Dim strEmail As String = dtrReader("UserEmail")
  11. end while
  12. dtrReader.Close()
  13. conPubs.Close()
  14. else
  15. dtrReader.Close()
  16. conPubs.Close()
  17. response.redirect("error.aspx?error=invalid+id")
  18. end if
  19. if Not strEmail Is Nothing then
  20. 'email stuff here
  21. else
  22. response.redirect("error.aspx?error=no+email")
  23. end if
  24. End Sub
  25. </script>
  26.  
  27. <HTML>
  28. <HEAD></HEAD>
  29. <BODY>
  30. Send <%= Trim(Request.QueryString("name")) %> a Message:
  31. <form name="sendemail">
  32. <!--
  33. form elements
  34. <asp:Button id="btnSend" OnClick="SendEmail" text="send message" runat="server" />
  35. -->
  36. </form>
  37. </BODY>
  38. </HTML>
  39.  
It could definitely be a lot better, but it should be something to point you in the right direction. If you are set on the "onclick popup an email div/dialog and send the email", then you will need lots of javascript. Otherwise, no one will care much about the second page. To save database usage I have put the ID and username in the query string to stop a first call, and only call when the user submits the form. You can use a Scalar instead of a Reader, which would be more efficient, just test to see if the email string is long enough. Usually it's hard to have an email less than 8 characters (5 are strictly for "@", ".", and "com"). So test that it is at least 5 characters in length.
Last edited by SheSaidImaPregy; Jan 15th, 2008 at 6:12 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 5
Reputation: happyhappy is an unknown quantity at this point 
Solved Threads: 0
happyhappy happyhappy is offline Offline
Newbie Poster

Re: Help with ASP code

 
0
  #3
Feb 4th, 2008
I have created a database and have display it onto a datalist as you said, but now I need to write code that will take me to a form when I click on the name of a person on the page. After it takes me to the form and I fill it out and press send I need the form to be sent to the email address of the person that I clicked on. The email address is in the database already with the person's information. Also the form will be on another page. Can you help me? Also can you explain in more detail this part:
  1. <asp:DataList ID="dlUsers" RepeatColumns="3" runat="server>
  2. <HeaderTemplate>
  3. <!-- if needed, use it. otherwise get rid of it -->
  4. <strong>Users</strong>
  5. </HeaderTemplate>
  6. <ItemTemplate>
  7. <!-- Use <A> if you are going to have the form on another page, otherwise use controls like LinkButton or Label. This is not easy with only ASP.NET, you will need javascript if you wish it to be same page as well. -->
  8. <a href="mailpage.aspx?id=<%# DataBinder.Eval(Container.DataItem, "UserID") %>&name=<%# DataBinder.Eval(Container.DataItem, "UserName") %>"><%# DataBinder.Eval(Container.DataItem, "UserName") %></a>
  9. </ItemTemplate>
  10. <FooterTemplate>
  11. <!-- if don't need, get rid of FooterTemplate -->
  12. </FooterTemplate><asp:DataList ID="dlUsers" RepeatColumns="3" runat="server>
  13. <HeaderTemplate>
  14. <!-- if needed, use it. otherwise get rid of it -->
  15. <strong>Users</strong>
  16. </HeaderTemplate>
  17. <ItemTemplate>
  18. <!-- Use <A> if you are going to have the form on another page, otherwise use controls like LinkButton or Label. This is not easy with only ASP.NET, you will need javascript if you wish it to be same page as well. -->
  19. <a href="mailpage.aspx?id=<%# DataBinder.Eval(Container.DataItem, "UserID") %>&name=<%# DataBinder.Eval(Container.DataItem, "UserName") %>"><%# DataBinder.Eval(Container.DataItem, "UserName") %></a>
  20. </ItemTemplate>
  21. <FooterTemplate>
  22. <!-- if don't need, get rid of FooterTemplate -->
  23. </FooterTemplate>
It's just that I don't understand whats going on there. You're probably the only one who can help me since I can't find anything else about this. Thank you
Last edited by peter_budo; Feb 4th, 2008 at 6:33 pm. Reason: Please use code tags for posting of your code
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: Help with ASP code

 
0
  #4
Feb 4th, 2008
That's exactly what I meant. Here:
  1. <asp:DataList ID="dlUsers" RepeatColumns="3" runat="server>
  2. <HeaderTemplate>
  3. <!-- if needed, use it. otherwise get rid of it -->
  4. <strong>Users</strong>
  5. </HeaderTemplate>
  6. <ItemTemplate>
  7. <!-- Use <A> if you are going to have the form on another page, otherwise use controls like LinkButton or Label. This is not easy with only ASP.NET, you will need javascript if you wish it to be same page as well. -->
  8. <a href="mailpage.aspx?id=<%# DataBinder.Eval(Container.DataItem, "UserID") %>&name=<%# DataBinder.Eval(Container.DataItem, "UserName") %>"><%# DataBinder.Eval(Container.DataItem, "UserName") %></a>
  9. </ItemTemplate>
  10. <FooterTemplate>
  11. <!-- if don't need, get rid of FooterTemplate -->
  12. </FooterTemplate>
  13.  
All this is saying that is if you are going to have the form on the same page (which you're not), then user a server control. If you're not (which you're not!), then use an "A" link meaning <a href="..."></a> to head to the page you need to. So simple way of doing it is this:
  1. <asp:DataList ID="dlUsers" RepeatColumns="3" runat="server>
  2. <HeaderTemplate>
  3. <!-- if needed, use it. otherwise get rid of it and design your own way -->
  4. <strong>Users</strong>
  5. </HeaderTemplate>
  6. <ItemTemplate>
  7. <a href="PageYouNeedToRedirectTo.aspx?id=<%# DataBinder.Eval(Container.DataItem, "UserID") %>" title="Send <%# DataBinder.Eval(Container.DataItem, "UserName") %> an email!"><%# DataBinder.Eval(Container.DataItem, "UserName") %></a>
  8. </ItemTemplate>
  9. </asp:Datalist>
Then on the "PageYouNeedToRedirectTo", let a user type in the information to send, a textbox you know? Then once they are ready to send it, have them click a button. The button should have an OnClick event that leads to a sub ( onClick="btnSendEmail_Click" ). Then within that sub, request the querystring and look up the information in the database. If it exists, then attempt to send the email. If it doesn't, alert the user of the problem.

If you need help, let me know.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC