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