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

Recommended Answers

All 3 Replies

What you need to do is pull all the information on the database out using something like:

"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:

<asp:DataList ID="dlUsers" RepeatColumns="3" runat="server>
<HeaderTemplate>
<!-- if needed, use it. otherwise get rid of it -->
<strong>Users</strong>
</HeaderTemplate>
<ItemTemplate>
<!-- 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.  -->
<a href="mailpage.aspx?id=<%# DataBinder.Eval(Container.DataItem, "UserID") %>&name=<%# DataBinder.Eval(Container.DataItem, "UserName") %>"><%# DataBinder.Eval(Container.DataItem, "UserName") %></a>
</ItemTemplate>
<FooterTemplate>
<!-- if don't need, get rid of FooterTemplate -->
</FooterTemplate>

Then on your mailpage.aspx, use a query to pull the information you need from the querystring and database:

<script language="vb" runat="server">
Sub SendEmail(ByVal S As Object, ByVal E As EventArgs)
  Dim conPubs As New OdbcConnection( "connstring" )
  Dim cmdSelect As New OdbcCommand( "SELECT UserEmail FROM Users WHERE UserID=?", conPubs )
  cmdSelect.Parameters.AddWithValue( "?UserID", Trim(Request.QueryString("id")).ToString )
  conPubs.Open()
  Dim dtrReader As OdbcDataReader = cmdSelect.ExecuteReader()
  if dtrReader.HasRows then
    while dtrReader.Read()
      Dim strEmail As String = dtrReader("UserEmail")
    end while
    dtrReader.Close()
    conPubs.Close()
  else
    dtrReader.Close()
    conPubs.Close()
    response.redirect("error.aspx?error=invalid+id")
  end if
  if Not strEmail Is Nothing then
    'email stuff here
  else
    response.redirect("error.aspx?error=no+email")
  end if
End Sub
</script>

<HTML>
<HEAD></HEAD>
<BODY>
Send <%= Trim(Request.QueryString("name")) %> a Message:
<form name="sendemail">
<!--
form elements
<asp:Button id="btnSend" OnClick="SendEmail" text="send message" runat="server" />
-->
</form>
</BODY>
</HTML>

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.

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:

<asp:DataList ID="dlUsers" RepeatColumns="3" runat="server>
<HeaderTemplate>
<!-- if needed, use it. otherwise get rid of it -->
<strong>Users</strong>
</HeaderTemplate>
<ItemTemplate>
<!-- 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.  -->
<a href="mailpage.aspx?id=<%# DataBinder.Eval(Container.DataItem, "UserID") %>&name=<%# DataBinder.Eval(Container.DataItem, "UserName") %>"><%# DataBinder.Eval(Container.DataItem, "UserName") %></a>
</ItemTemplate>
<FooterTemplate>
<!-- if don't need, get rid of FooterTemplate -->
</FooterTemplate><asp:DataList ID="dlUsers" RepeatColumns="3" runat="server>
<HeaderTemplate>
<!-- if needed, use it. otherwise get rid of it -->
<strong>Users</strong>
</HeaderTemplate>
<ItemTemplate>
<!-- 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.  -->
<a href="mailpage.aspx?id=<%# DataBinder.Eval(Container.DataItem, "UserID") %>&name=<%# DataBinder.Eval(Container.DataItem, "UserName") %>"><%# DataBinder.Eval(Container.DataItem, "UserName") %></a>
</ItemTemplate>
<FooterTemplate>
<!-- if don't need, get rid of FooterTemplate -->
</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

That's exactly what I meant. Here:

<asp:DataList ID="dlUsers" RepeatColumns="3" runat="server>
<HeaderTemplate>
<!-- if needed, use it. otherwise get rid of it -->
<strong>Users</strong>
</HeaderTemplate>
<ItemTemplate>
<!-- 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.  -->
<a href="mailpage.aspx?id=<%# DataBinder.Eval(Container.DataItem, "UserID") %>&name=<%# DataBinder.Eval(Container.DataItem, "UserName") %>"><%# DataBinder.Eval(Container.DataItem, "UserName") %></a>
</ItemTemplate>
<FooterTemplate>
<!-- if don't need, get rid of FooterTemplate -->
</FooterTemplate>

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:

<asp:DataList ID="dlUsers" RepeatColumns="3" runat="server>
<HeaderTemplate>
<!-- if needed, use it. otherwise get rid of it and design your own way -->
<strong>Users</strong>
</HeaderTemplate>
<ItemTemplate>
<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>
</ItemTemplate>
</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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.