Hi George616,
You need to send the UserId in email link as QueryString.
When user click on the link in th email he/she will be redirected to the SignUp page with QueryString.
In signup page button click retrieve the UserId and fetch the email from the table and save it as InvitedBy.
Check this example. Now please take its reference and correct your code.
HTML
Default
<table>
<tr>
<td>Email:</td>
<td>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="btnInvite" runat="server" Text="Invite" OnClick="OnInvite" /></td>
</tr>
</table>
SignUp
<table>
<tr>
<td>Email:</td>
<td>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Password:</td>
<td>
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox></td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="btnSugnUp" runat="server" Text="Sugn Up" OnClick="OnSugnUp" /></td>
</tr>
</table>
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Net;
using System.Net.Mail;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Net
Imports System.Net.Mail
Code
C#
Default
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
// Set userId after successfully login.
Session["UserID"] = 1;
}
}
protected void OnInvite(object sender, EventArgs e)
{
using (MailMessage mm = new MailMessage("sender@gmail.com", txtEmail.Text))
{
mm.Subject = "Account Registration.";
string body = "Hello " + txtEmail.Text.Trim() + ",";
body += "<br /><br />Please click the following link to register";
body += "<br /><a href = '" + Request.Url.AbsoluteUri.Replace("Default.aspx", "SignUp.aspx?Id=" + Session["UserID"]) + "'>Click here for Sign up</a>.";
body += "<br /><br />Thanks";
mm.Body = body;
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential("sender@gmail.com", "<password>");
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
}
}
SignUp
protected void OnSugnUp(object sender, EventArgs e)
{
string invitedBy = GetInvitedBy(Convert.ToInt32(Request.QueryString["Id"]));
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO Users VALUES(@Email,@Password,@InvitedBy,@InvitedDate)"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Email", txtEmail.Text.Trim());
cmd.Parameters.AddWithValue("@Password", txtPassword.Text.Trim());
cmd.Parameters.AddWithValue("@InvitedBy", invitedBy);
cmd.Parameters.AddWithValue("@InvitedDate", DateTime.Now);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
private string GetInvitedBy(int id)
{
string invitedBy = "";
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT User_email FROM Users WHERE UserID = @Id)"))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Id", id);
cmd.Connection = con;
con.Open();
invitedBy = Convert.ToString(cmd.ExecuteScalar());
con.Close();
}
}
return invitedBy;
}
VB.Net
Default
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
' Set userId after successfully login.
Session("UserID") = 1
End If
End Sub
Protected Sub OnInvite(ByVal sender As Object, ByVal e As EventArgs)
Using mm As MailMessage = New MailMessage("sender@gmail.com", txtEmail.Text)
mm.Subject = "Account Registration."
Dim body As String = "Hello " & txtEmail.Text.Trim() & ","
body += "<br /><br />Please click the following link to register"
body += "<br /><a href = '" & Request.Url.AbsoluteUri.Replace("Default.aspx", "SignUp.aspx?Id=" & Session("UserID")) & "'>Click here for Sign up</a>."
body += "<br /><br />Thanks"
mm.Body = body
mm.IsBodyHtml = True
Dim smtp As SmtpClient = New SmtpClient()
smtp.Host = "smtp.gmail.com"
smtp.EnableSsl = True
Dim NetworkCred As NetworkCredential = New NetworkCredential("sender@gmail.com", "<password>")
smtp.UseDefaultCredentials = True
smtp.Credentials = NetworkCred
smtp.Port = 587
smtp.Send(mm)
End Using
End Sub
SignUp
Protected Sub OnSugnUp(ByVal sender As Object, ByVal e As EventArgs)
Dim invitedBy As String = GetInvitedBy(Convert.ToInt32(Request.QueryString("Id")))
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("INSERT INTO Users VALUES(@Email,@Password,@InvitedBy,@InvitedDate)")
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@Email", txtEmail.Text.Trim())
cmd.Parameters.AddWithValue("@Password", txtPassword.Text.Trim())
cmd.Parameters.AddWithValue("@InvitedBy", invitedBy)
cmd.Parameters.AddWithValue("@InvitedDate", DateTime.Now)
cmd.Connection = con
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
End Using
End Sub
Private Function GetInvitedBy(ByVal id As Integer) As String
Dim invitedBy As String = ""
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT UserID FROM Users WHERE UserID = @Id)")
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@Id", id)
cmd.Connection = con
con.Open()
invitedBy = Convert.ToString(cmd.ExecuteScalar())
con.Close()
End Using
End Using
Return invitedBy
End Function