Click here to Skip to main content
15,898,374 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to compare current date with database date which is expired date...if current date greater than expired date in database then status column in database should changed to "Expired" using C#...i m not getting the code to implement this pls help me its a window application...

What I have tried:

string constring = "Data Source=LENOVO-PC\\SUMIT;Initial Catalog=Ngo_Mgmt;User ID=sa;Password=123";
SqlConnection con = new SqlConnection(constring);
try
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select Subscription_Expire,Status,currentdate from AddMember";
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
if (dr["Subscription_Expire"].ToString().Trim() == dr["currentdate"].ToString().Trim())
{


cmd.CommandText = "update AddMember set Status=@Status ";
string ans="Expired";
cmd.Parameters.AddWithValue("@Status",ans);
cmd.ExecuteNonQuery();
}

}
}

else
{
//MessageBox.Show("No Such record Exists", "Alert!");
}
cmd.Dispose();
dr.Close();
}
catch (Exception ex)
{
//MessageBox.Show("Can not open connection ! ");
}
finally
{
con.Close();
}
Posted
Updated 15-Nov-16 9:34am

Don't do that! Never convert dates to strings and try to convert them - always leave them as DateTime objects and compare those.
C#
DateTime expireDate = (DateTime) dr["Subscription_Expire"];
if (expireDate <= DateTime.Now.Date)
   {
   // Expired.
   ...
This assumes that your DB holds dates as DATE or DATETIME columns - and if it doesn't it should as string based dates cause enormous problems very, very quickly!
 
Share this answer
 
You can do that with a single query:
SQL
UPDATE
    AddMember
SET
    Status = 'Expired'
WHERE
    Subscription_Expire < GetDate()
;

You are storing your dates as one of the date types[^], and not as strings, right? If not, fix that first, before you run into the significant problems that come with dates stored as strings.

NB: Your application should never connect to the database as sa. That is an unlimited user which could be used to take over your server, and possibly your network. Create a specific user which has only the privileges your application needs, or use Windows authentication.

C#
// Using Windows authentication:
const string constring = "Data Source=LENOVO-PC\\SUMIT;Initial Catalog=Ngo_Mgmt;Trusted_Connection=True;"; 

const string query = @"UPDATE AddMember SET Status = 'Expired' WHERE Subscription_Expire < GetDate();";

using (var connection = new SqlConnection(constring))
using (var command = new SqlCommand(query, connection))
{
    connection.Open();
    command.ExecuteNonQuery();
}
 
Share this answer
 
Comments
Member 12852119 16-Nov-16 0:41am    
what is GetDate()
does is get current date
as its not working
Richard Deeming 16-Nov-16 7:59am    
It's a built-in SQL function:
https://msdn.microsoft.com/en-us/library/ms188383.aspx[^]

Define "not working".
perhaps a better solution would be a computed column in the db.

create table test1
(
column1 int identity(1,1) not null primary key,
column2 datetime2 not null default(sysdatetime()),
column3 as case when column2 < sysdatetime() then 'this'
else 'that' end
)

insert into test1(column2) select '1/1/2010'
insert into test1(column2) select '1/1/2050'

select * from test1
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900