Click here to Skip to main content
15,912,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am not able to compare current date with database existing date in c#.

What I have tried:

<pre>
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

        string userid = GVmydsr.DataKeys[e.RowIndex].Value.ToString();
        GridViewRow row = (GridViewRow)GVmydsr.Rows[e.RowIndex];

        //string date = GVmydsr.Rows[e.RowIndex].Cells[1].Text;
        Label date = (Label)row.FindControl("lblcurrentdate");
        
        

        GVmydsr.EditIndex = -1;

        DropDownList ddl1 = GVmydsr.Rows[e.RowIndex].FindControl("ddlclient") as DropDownList;
        TextBox txt1 = GVmydsr.Rows[e.RowIndex].FindControl("txtarea") as TextBox;
        TextBox txt2 = GVmydsr.Rows[e.RowIndex].FindControl("txtcmpnyname") as TextBox;
        TextBox txt3 = GVmydsr.Rows[e.RowIndex].FindControl("txtcname") as TextBox;
        TextBox txt4 = GVmydsr.Rows[e.RowIndex].FindControl("txtmobile") as TextBox;
        TextBox txt5 = GVmydsr.Rows[e.RowIndex].FindControl("txtemail") as TextBox;
        TextBox txt6 = GVmydsr.Rows[e.RowIndex].FindControl("txtaddr") as TextBox;
        TextBox txt7 = GVmydsr.Rows[e.RowIndex].FindControl("txtland") as TextBox;
        TextBox txt8 = GVmydsr.Rows[e.RowIndex].FindControl("txtpresence") as TextBox;
        DropDownList ddl2 = GVmydsr.Rows[e.RowIndex].FindControl("ddlstatus") as DropDownList;
        DropDownList ddl3 = GVmydsr.Rows[e.RowIndex].FindControl("ddlresult") as DropDownList;
        TextBox txt9 = GVmydsr.Rows[e.RowIndex].FindControl("txtcomment") as TextBox;

        string day = DateTime.Today.ToShortDateString().ToString();
        if (date.ToString() == day.ToString())
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("update [dsr_data] set client_type='" + ddl1.Text + "' ,area='" + txt1.Text + "',company_name='" + txt2.Text + "',client_name='" + txt3.Text + "',mobile='" + txt4.Text + "',email='" + txt5.Text + "',address_name='" + txt6.Text + "',landmark='" + txt7.Text + "',curr_presence_appoint='" + txt8.Text + "',status_type='" + ddl2.Text + "', result='" + ddl3.Text + "', comment='" + txt9.Text + "' where id='" + userid + "'", con);
            cmd.ExecuteNonQuery();
            con.Close();

            gvbind();
        }
    }
Posted
Updated 3-Jan-18 19:10pm
Comments
Karthik_Mahalingam 4-Jan-18 1:03am    
what is the value you are getting in
 Label date = (Label)row.FindControl("lblcurrentdate");
ADI@345 4-Jan-18 1:04am    
"01-04-2018 00:00:00"
Karthik_Mahalingam 4-Jan-18 1:07am    
use  Reply  button, to post Comments/query to the user, so that the user gets notified and responds to your text.
ADI@345 4-Jan-18 1:08am    
oky
Karthik_Mahalingam 4-Jan-18 1:11am    
posted the solution.

You can try something like
C#
string day = DateTime.Today.ToString("MM/dd/yyyy");

if (date.ToString("MM/dd/yyyy") == day)
{
    //do something
}

By the way, try utilized parameterized query if possible to avoid potential SQL injection. Here is an example Using Parameterized queries to prevent SQL Injection Attacks in SQL Server[^]
 
Share this answer
 
 
Share this answer
 
First off, make it a habit to put objects that eat resources such as SqlConnection, SqlCommand and SqlDataAdapter within a [^]using Statement to ensure that objects will be properly disposed and closed after they are used.

Second, "DO NOT" append the value of your input fields to your SQL query as it can lead you to SQL Injection attack. Use parameterize query instead.See: Protect Your Data: Prevent SQL Injection[^]

Now back to your original question. Comparing string date is very straight-forward like this:

C#
if (Convert.ToDateTime(date).Equals(DateTime.Now())){
  //do stuff
}


You need to compare a DateTime object and not a string. You just need to make sure that the value of the date from your Label is a valid datetime or else use DateTime.TryParse() instead of Convert.ToDateTime()
 
Share this answer
 
From Comments:
"01-04-2018 00:00:00"

string date =  "01-04-2018 00:00:00";
          string day = DateTime.Today.ToString("MM-dd-yyyy 00:00:00");
          if (date == day)
          {

          }


Note: Concatenating the sql Query string is vulnerable to SQL Injection[^] attacks
always use Parameterized queries to prevent SQL Injection Attacks in SQL Server[^]
 
Share this answer
 
v2
Comments
ADI@345 4-Jan-18 1:14am    
sir, i got same result on date and day, but even if both are same, it does not go inside loop..
Karthik_Mahalingam 4-Jan-18 1:26am    
try
string dbDateString = "01-04-2018 00:00:00";
            DateTime date1 = DateTime.ParseExact(dbDateString.Split(' ')[0], "MM-dd-yyyy", null);
            string day = DateTime.Today.ToString("MM-dd-yyyy");
            string dateDB = date1.ToString("MM-dd-yyyy");
            if (dateDB == day)
            {

            }
Karthik_Mahalingam 4-Jan-18 1:27am    
try trimming the values
ADI@345 4-Jan-18 1:17am    
Label date = (Label)row.FindControl("lblcurrentdate");
i am using label to get date from database.., and then comparing system date with database date from label..
ADI@345 4-Jan-18 1:32am    
String was not recognized as a valid DateTime.
//string date = GVmydsr.Rows[e.RowIndex].Cells[1].Text;
Label date = (Label)row.FindControl("lblcurrentdate");
string cdate = date.ToString();
DateTime date1 = DateTime.ParseExact(cdate.Split(' ')[0], "MM-dd-yyyy", null);
string dateDB = date1.ToString("MM-dd-yyyy");

string day = DateTime.Today.ToString("MM-dd-yyyy");

if (dateDB == day)
{
////
}

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