Click here to Skip to main content
15,903,540 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
.. how i can do that .. please watch my code below and comment the code portion for that update statement ..
<pre lang="c#">private void button3_Click(object sender, EventArgs e)
        {

            myconnection.Open();
            displayData();
            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;");
                    myconnection1.Open();
                    SqlCommand cmd1 = new SqlCommand("UPDATE employTable2 SET daysleft='" + daysleft.ToString() + "'", myconnection1);
                   
                    cmd1.ExecuteNonQuery();
                    myconnection1.Close();
                  MessageBox.Show(t1.Days +"", "Days Left");
                }
                myconnection.Close();

            } 
        }


What I have tried:

i tried to update the leftdays column in DB table .. but its updates all at once .. with last value that come through loop of datareader .. i want to update it row by row .. how i can do that please help... comment me code for that portion ..
Posted
Updated 4-Feb-17 20:41pm

What for? Why do you want to store a value that is changing every day and can be easily derivable from some constants such as the expiredate? It is like the age of a person, you just need to store his date of birth to know his age at any time, don't you think so?
 
Share this answer
 
Peter Leow is right, however if you want to do it row by row you will need to add a WHERE statement to the UPDATE command to only update the record you just read.

You can also use the SqlConnection .Clone to copy the connection settings rather than repeat your connection string, you still have to open and close the clone as it only copies the settings from the source.

C#
SqlConnection myconnection1 = myconnection.Clone()


You still have the myconnection.close inside the using with the .open outside the using, one day you will be trying to debug a piece of code wondering why it fails when everything looks right, keeping matching lines of code at the same level helps prevent future bugs.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900