.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Email >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Subscribe and UnSubscribe in ASP.NET

Posted By:Athira Sreejith       Posted Date: February 22, 2009    Points: 10    Category: Email    URL: http://www.dotnetspark.com  
 

Subscribe and UnSubscribe in ASP.NET: Below is the code to achieve that.

public void Subscribe(string s_EmailID)

{

try

{

m_Conn = ConfigurationManager.ConnectionStrings["aaa"].ConnectionString;

m_Scon = new SqlConnection(m_Conn);

m_Query = "Select emailid,status from Tr_Subscribe where emailid='" + s_EmailID + "'";

m_Scmd = new SqlCommand(m_Query, m_Scon);

m_Scon.Open();

SqlDataReader m_Sdr = m_Scmd.ExecuteReader(CommandBehavior.CloseConnection);

if (m_Sdr.HasRows)

{

if (m_Sdr.Read())

{

if (bool.Parse(m_Sdr["status"].ToString()) == true)

{

lblMessage.Text = "You have already subscribed.

";

lblMessage.Text += "";

}

else if (bool.Parse(m_Sdr["status"].ToString()) == false)

{

ObjUsers = new User();

ObjUsers.UpdateSubscribe(1, s_EmailID);

lblMessage.Text = "Thank you for subscribing.

";

lblMessage.Text += "";

}

}

m_Sdr.Close();

}

else

{

ObjUsers = new User();

ObjUsers.InsertSubscribe(s_EmailID);

lblMessage.Text = "Thank you for subscribing.

";

lblMessage.Text += "";

}

}

catch (Exception ex)
{

lblMessage.Text = ex.Message;

lblMessage.Visible = true;

}

finally
{

if (m_Scon.State == ConnectionState.Open)

m_Scon.Close();

}

}

public void UnSubscribe(string s_EmailID)

{

ObjUsers = new User();

ObjUsers.UpdateSubscribe(0, s_EmailID);

lblMessage.Text = "Your E-mail ID is unsubscribed successfully.

";

lblMessage.Text += "";

}

 

User.cs Class:using System;

using System.Data;

using System.Data.SqlClient;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

///

/// Summary description for User

///

public class User

{

SqlConnection m_Scon;

string m_Query;

SqlCommand m_Scmd;

public User()

{

//

// TODO: Add constructor logic here

//

m_Scon = new SqlConnection(ConfigurationManager.ConnectionStrings["aaa"].ConnectionString);

}

public string Generatecode(string Tname)

{

string Code = string.Empty;

//Generating random code

Random rf_Code = new Random();

Code = rf_Code.Next(999, 10000).ToString();

return CheckGeneratedCode(Code+"W");

}

public string CheckGeneratedCode(string Code)

{

string ReturnCode = "";

try

{

//checking for the code existance in the database

m_Query = "Select code from ms_customer where code ='" + Code + "'";

m_Scmd = new SqlCommand(m_Query, m_Scon);

m_Scon.Open();

if (m_Scmd.ExecuteScalar() != null)

{

m_Scon.Close();

Random rs_Code = new Random();

Code = rs_Code.Next(999, 10000).ToString();

ReturnCode = CheckGeneratedCode(Code);

}

else

{

ReturnCode = Code;

m_Scon.Close();

}

}

catch (Exception ex)

{

Code = ex.Message;

}

finally

{

if (m_Scon.State == ConnectionState.Open)

m_Scon.Close();

}

return ReturnCode;

}

public string Generateid(string Tname)

{

string Gid = string.Empty;

try

{

//generating guid

Gid = Guid.NewGuid().ToString();

//checking id in the database

m_Query = "Select id from " + Tname + " where id ='" + Gid + "'";

m_Scmd = new SqlCommand(m_Query, m_Scon);

m_Scon.Open();

if (m_Scmd.ExecuteScalar() != null)

{

Gid = Guid.NewGuid().ToString();

}

m_Scon.Close();

}

catch (Exception ex)

{

Gid = ex.Message;

}

finally

{

if (m_Scon.State == ConnectionState.Open)

m_Scon.Close();

}

return Gid;

}

public void InsertSubscribe(string Email)

{

string Id = "";

Id = Generateid("Tr_Subscribe");

m_Query = "insert into Tr_Subscribe values('" + Id + "','" + Email.Trim() + "'," + 1 + ",'Y')";

m_Scmd = new SqlCommand(m_Query, m_Scon);

m_Scon.Open();

m_Scmd.ExecuteNonQuery();

m_Scon.Close();

}

public void UpdateSubscribe(int Status, string Email)

{

m_Query = "Update Tr_Subscribe set status=" + Status + "where emailid ='" + Email.Trim() + "'";

m_Scmd = new SqlCommand(m_Query, m_Scon);

m_Scon.Open();

m_Scmd.ExecuteNonQuery();

m_Scon.Close();

}

~User()

{

if (m_Scon.State == ConnectionState.Open)

{

m_Scon.Close();

}

}

}



     

Further Readings:

    Responses

    No response found. Be the first to respond this post

    Post Comment

    You must Sign In To post reply
    Find More code samples in C#, ASP.Net, Vb.Net and more Here

    Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend