Muhammad Ali

Muhammad Ali

  • 2k
  • 26
  • 1.7k

I want to update the leftdays, Row by Row .not all at once.

Feb 5 2017 12:42 AM
I want to Update the Leftdays Column Row by Row but the Statement which  i am using for Update .its updates the whole Column with same value .. as datareader loop calculates the different value while looping ... how i can make work properly ..please comment the Code .. 

private void button3_Click(object sender, EventArgs e)
string selectSql = "select * from employTable2";
SqlCommand com = new SqlCommand(selectSql, myconnection);
using (SqlDataReader read = com.ExecuteReader())
while (read.Read())
string first1 = read["salarydate"].ToString();
string Second1 = read["expiredate"].ToString();
TimeSpan t1 = Convert.ToDateTime(Second1) - DateTime.Now;
int daysleft = t1.Days;
SqlConnection myconnection1 = 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;");
SqlCommand cmd1 = new SqlCommand("UPDATE employTable2 SET daysleft='" + daysleft.ToString() + "'", myconnection1);
MessageBox.Show(t1.Days +"", "Days Left");

Answers (13)