Click here to Skip to main content
15,917,174 members
Please Sign up or sign in to vote.
1.80/5 (5 votes)
See more:
please check my code


SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ToString());
                string sql = "select DISTINCT id,MAX(enddate) as enddate from table1 group by id";
                SqlCommand cmd = new SqlCommand(sql, con);
                string d1 = DateTime.Now.ToString("dd/m/yyyy");
                DateTime datenow = new DateTime();
                datenow = Convert.ToDateTime(d1);

                DateTime enddate = new DateTime();
                enddate = Convert.ToDateTime(cmd.ExecuteScalar());

                if (DateTime.Compare(datenow, enddate) > 0)
                {

                    string sql1 = "update users set type='Suspended Users' where users.id in(select DISTINCT id from table1 where enddate ='" + enddate + "')";
                    SqlCommand cmd1 = new SqlCommand(sql1, con);
                    cmd1.ExecuteNonQuery();
                    for (int i = 1; i < 5; i++)
                    {
                        int period = 1;
                        string sql2 = "update table3 set period='" + period + "'";
                        SqlCommand cmd2 = new SqlCommand(sql2, con);
                        cmd2.ExecuteNonQuery();
                        period++;
                    }

my tables are

table1:

id|enddate
______________

user table:

tableid|type|id
___________________

table3:

pid|period|id
______________
1 |1 |1
2 |2 |1
3 |1 |2
4 |3 |1
5 |2 |2
Posted
Updated 2-Apr-11 11:31am
v4
Comments
Manfred Rudolf Bihy 2-Apr-11 18:12pm    
This is not how it works here. You have to ask a question, explain what you are trying to achieve and tell us what problems you are facing. So go ahead and modify your question accordingly.

Thanks for your cooperation!
shms_rony 3-Apr-11 9:12am    
question is clear ..what do you mean!!

1 solution

Well, it doesn't work.

Next!

Oh, sorry, you want more detail?

I'll point out one big problem, then you can get started on the rest:

When you use SELECT to return more than one value, ExecuteScalar is going to return the first value of the first column, not the whole record content. In this case, it will be an ID. The chances of that being a useful DateTime are not good.
 
Share this answer
 
Comments
shms_rony 3-Apr-11 9:11am    
what can i modify??
OriginalGriff 3-Apr-11 9:16am    
I would start by replacing the ExecuteScalar with a ExecuteReader. Of course, that means you need to create an SqlDataReader and process it, but you should find all the details in your course notes.
shms_rony 3-Apr-11 9:21am    
i replaced ExecuteScalar with a ExecuteReader

check my code


SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
DateTime enddate = new DateTime();
enddate = (DateTime)dr["enddate"];
int id = Convert.ToInt32(dr["id"]);
if (DateTime.Compare(datenow, enddate) > 0)
{

string sql1 = "update users set type='Suspended Users' where users.id='" + id + "'";
SqlCommand cmd1 = new SqlCommand(sql1, con);
cmd1.ExecuteNonQuery();
OriginalGriff 3-Apr-11 9:30am    
Looks better, give it a try and see if it works - good luck!
shms_rony 3-Apr-11 9:34am    
not work :(

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