You need to initialize the string. Currently, any initialization or assignment is done in the if block. The compiler detects this and believes that it is never initialized.
string st = string.Empty;
As an additional note, it is much safer to use similar TryParse () methods for chains to ensure that you will not have any unexpected exceptions caused by an incorrect formatting error. The method will return true if it was successfully converted, which makes it clean:
if (dt!=null) { if(dt.rows.count> 0) { st = dt.Rows[3]["timeslot_StartTime"].ToString(); } } DateTime dt = DateTime.MinValue; if (DateTime.TryParse(st, out dt)) { //was successful and do something here }
Feisty mango
source share