Muhammad Ali

Muhammad Ali

  • NA
  • 26
  • 2.1k

how to make this code work .. i want to show leftdays

Jan 28 2017 9:12 PM
SqlConnection myconnection = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=c:\documents and settings\muhammad\my documents\visual studio 2010\Projects\ProjectClock\ProjectClock\ClockDB.mdf;Integrated Security=True;User Instance=True");
private void button1_Click(object sender, EventArgs e)
{
myconnection.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into employTable(employname,ismarred,salarydate,expiredate) values('" + textBox1.Text + "','" + checkBox1.Checked + "','" + dateTimePicker1.Value.ToString("M/d/yyyy") + "','" + dateTimePicker1.Value.AddDays(365).ToString("M/d/yyyy") + "')";
cmd.Connection = myconnection;
cmd.ExecuteNonQuery();
string selectSql = "select * from employTable";
SqlCommand com = new SqlCommand(selectSql, myconnection);
string first;
string Second;
DateTime salaryDate;
DateTime expireDate;
int daysleft;
 
 
////////////////this block of code is not showing the messagebox it dont't gives error /////////////////////////// 
using (SqlDataReader read = cmd.ExecuteReader())
{
while (read.Read())
{
first = (read["salarydate"].ToString());
Second = (read["expiredate"].ToString());
salaryDate = DateTime.ParseExact(first, "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture);
expireDate = DateTime.ParseExact(Second, "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture);
TimeSpan t1 = expireDate - salaryDate;
daysleft = t1.Days;
MessageBox.Show(daysleft.ToString(), "Days Left");
}
/===================/
}
myconnection.Close();
}
}

Answers (14)