Click here to Skip to main content
15,887,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to save the current date to a database called Deadline but it is not getting saved, the database also has Id column but I have set identity(1,1) for it. Still not inserting also please let me know the code to compare the saved time to current time thanks.

The data type I used for the column date is DateTime.
Here is the code I used.

protected void Button1_Click(object sender, EventArgs e)
    {

        string constr = ConfigurationManager.ConnectionStrings["facultylogConnectionString"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "Insert into Deadline (Date) values (@Date)";
                {
                    cmd.Parameters.AddWithValue("@Date", DateTime.Now);
                    cmd.Connection = con;
                    con.Open();
                    cmd.ExecuteReader();
                    con.Close();
                }
            }
        }


    }


What I have tried:

Tried this as well

String strConnString = ConfigurationManager.ConnectionStrings["facultylogConnectionString"].ConnectionString;
       String strQuery = "Insert into Deadline (Date) values (GetDate())";
       SqlConnection con = new SqlConnection(strConnString);
       SqlCommand cmd = new SqlCommand();
       cmd.CommandType = CommandType.Text;
       cmd.CommandText = strQuery;
       cmd.Connection = con;
       con.Open();
       cmd.ExecuteNonQuery();
       con.Close();
Posted
Updated 6-Jul-20 5:26am
v3

I see two problems:

1.
You can't modify your database with ExecuteReader; that method only reads something from the database, it does never write anything to it. Use ExecuteNonQuery for inserts, updates, deletes...

2.
A lot of words (list) are reserved in SQL and "Date" is one of them. Two solutions: (a) avoid using them as field names, that is the easiest way; (b) escape them in your SQL statement; the exact way to do that depends on the database being used, candidates are brackets as in [Date] and double quotes, which is somewhat harder to get right inside a string!

:)
 
Share this answer
 
v3
Comments
PIEBALDconsult 2-Jul-20 21:54pm    
1. Of course you can. In ADO.net, everything goes through ExecuteReader.
2. Yes.
3. Hey, long time, so no see.
Luc Pattyn 2-Jul-20 22:15pm    
Yep. I'm still somewhat active here, more on C# forum though.
candijen 3-Jul-20 5:38am    
I did not understand. Why is the second code not working then? I used ExecuteNonQuerry
Check other columns in database are nullable or not.

After this try code given below-
cmd.Parameters.AddWithValue("@Date", DateTime.Now.ToShortDateString());
if (con.State == ConnectionState.Closed)
{
con.Open();
}

result = cmd.ExecuteNonQuery();

cmd.Dispose();

if (result > 0)
{
return result;
}
else
{
return 0;
}
 
Share this answer
 
what is data type of Date column in database? Is it datetime or varchar?
second thing Getdate() is function in SQL server Not in c#.

Can you explain more about this?
 
Share this answer
 
Comments
candijen 3-Jul-20 13:51pm    
it is datetime

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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