In my project I wanna automatically delete accounts that are kept inactive for more that 45days. For testing purpose I took 2 minutes and wrote the following code. But its not working. Can anybody tell me how to perform “Record deletion” if kept inactive for 2 minutes.
My code follows.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Odbc;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" + "SERVER=localhost;" + "DATABASE=inactive;" + "UID=root;" + "PASSWORD=*********;" + "OPTION=3";
OdbcConnection MyConnection = new OdbcConnection(MyConString);
try
{
MyConnection.Open();
OdbcCommand cmd = new OdbcCommand("DELETE name FROM email WHERE date < DATE_SUB(NOW(), INTERVAL 2 MIN)", MyConnection);
MyConnection.Close();
Label1.Text = "Done";
}
catch (Exception ex)
{
Label1.Text = ex.ToString();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" + "SERVER=localhost;" + "DATABASE=inactive;" + "UID=root;" + "PASSWORD=*********;" + "OPTION=3";
OdbcConnection MyConnection = new OdbcConnection(MyConString);
try
{
MyConnection.Open();
OdbcCommand cmd = new OdbcCommand("Select name from email where email=?", MyConnection);
cmd.Parameters.Add("@email", OdbcType.VarChar, 255).Value = TextBox1.Text;
OdbcDataReader dr = cmd.ExecuteReader();
if (dr.HasRows == false)
{
throw new Exception();
}
if (dr.Read())
{
Response.Write(dr[0].ToString());
}
}
catch
{
}
}
}
Updated:
First I created a database named inactive and created a table email. The below screenshot says the record.

Later I used the following code..
protected void Page_Load(object sender, EventArgs e)
{
string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" + "SERVER=localhost;" + "DATABASE=inactive;" + "UID=root;" + "PASSWORD=******;" + "OPTION=3";
OdbcConnection MyConnection = new OdbcConnection(MyConString);
try
{
MyConnection.Open();
OdbcCommand cmd = new OdbcCommand("DELETE FROM email WHERE `date` < DATE_SUB(NOW(), INTERVAL 2 MINUTE)", MyConnection);
MyConnection.Close();
}
catch (Exception ex)
{
Label1.Text = ex.ToString();
}
}
Your
DELETEsyntax seems to be wrong, drop the field name “name”, ie(the
deletecommand deletes whole row, not a single field)