locked
How pass datetime parameter from query? RRS feed

  • Question

  • User647458646 posted

    I am trying to get my datetime from database, into the variable datetime "dat", however, the current code throws a compiling error of ---> Use of unassigned local variable 'dat'

      public static string content()
            {
                string xml = "";
                string title = "";
                string content = "";
                DateTime dat = new DateTime();
           
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["#######"].ConnectionString);
                string commandtext = "###########";
                SqlCommand command = new SqlCommand(commandtext, con);
                con.Open();
                command.Parameters.Add(new SqlParameter("title", title));
                command.Parameters.Add(new SqlParameter("Body", content));
                command.Parameters.Add(new SqlParameter("ACTIVEDATE", dat));
    
                SqlDataReader reader = command.ExecuteReader();
    
                dat.ToString("dd-MM-yyyy");
    
    			//XElement code
    			
                while (reader.Read())
                {
                    string top = reader.GetString(0);
                    string dtt = reader.GetString(1);
                    string body = reader.GetString(2);
    
                    var result = top + "<br />" + dtt + "<br />" + body + "<br />";
    
                    string data = result.ToString();
              
                    xeSendTo1.Add(new XElement("value", (new XCData(data))));
         
                }
    			con.Close();
                           
                return xml = xDoc.ToString(); 
            }

    I tried changing the assignment statement to the following, but they further resulted in more compiling errors.

    DateTime dat = new DateTime();

    DateTime.TryParseExact(...)

    is their a correct approach to declare the Datetime variable, for the sqlParameter. 

    Please advice. 

    Many thanks

    Tuesday, October 14, 2014 11:18 AM

Answers

  • User647458646 posted

    I manage to get this solved be changing the declaring statement to --> DateTime dat = DateTime.Now;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 14, 2014 12:25 PM