Click here to Skip to main content
15,891,849 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
This code is run properly ..but only first row is updating from database i want to update all the rows from database while page load..Please help me sir ,Thanks in Advance :)




C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Binddays();
        }
    }


public void Binddays()
    {

        con.Open();

        OleDbDataAdapter da = new OleDbDataAdapter("Select * from SlideShowTable", con);
        DataTable dt = new DataTable();
        da.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            Stratd = dt.Rows[0][11].ToString();
            Endd = dt.Rows[0][12].ToString();
            int Totaldays = duration(Stratd, Endd);
            int Totaldayss = durationn(System.DateTime.Now.ToString("MM/dd/yyyy"), Endd);

            string str = Convert.ToString(Totaldays);
            string str1 = Convert.ToString(Totaldayss);


            OleDbCommand cmd123 = new OleDbCommand("UPDATE SlideShowTable SET Total_Days='" + str + "',Days_remain='" + str1 + "'", con);

            cmd123.ExecuteNonQuery();



        }
    }
    public int duration(string startdate, string enddate)
    {
        DateTime dt1 = DateTime.Parse(Convert.ToDateTime(Stratd).ToString("MM/dd/yyyy"));
        DateTime dt2 = DateTime.Parse(Convert.ToDateTime(Endd).ToString("MM/dd/yyyy"));
        TimeSpan ts = dt2.Subtract(dt1);
        int days = ts.Days;
        return days;
    }
    public int durationn(string startdate, string enddate)
    {
        DateTime dtt1 = DateTime.Parse(System.DateTime.Now.ToString("MM/dd/yyyy"));
        DateTime dtt2 = DateTime.Parse(Convert.ToDateTime(Endd).ToString("MM/dd/yyyy"));
        TimeSpan tss = dtt2.Subtract(dtt1);
        int dayss = tss.Days;
        return dayss;
    }
Posted
Updated 25-Apr-13 1:32am
v2
Comments
PRAKASH9 25-Apr-13 7:37am    
use the for loop for update all rows from database

like

for(int i=0;i<dt.rows.count;i++)
{}
Amirsalgar1 25-Apr-13 7:40am    
thanks sir .. please suggest how it will come
Amirsalgar1 25-Apr-13 7:46am    
Sir,i have tried your for loop but still same thing is happening
SIJUTHOMASP 25-Apr-13 7:42am    
Please try to execute your update statement directly in SQL Management studio and see if it is updating all records?

Updating first row only happening when it is used in the application?
Amirsalgar1 25-Apr-13 7:44am    
ya it is updating ..only first row :(

Please use "for" loop for this. Refer below code-
C#
if (dt.Rows.Count > 0)
       {
         for (i=0; i<=dt.Rows.Count;i++)
          {
               Stratd = dt.Rows[i][11].ToString();
               Endd = dt.Rows[i][12].ToString();
               int Totaldays = duration(Stratd, Endd);
               int Totaldayss = durationn(System.DateTime.Now.ToString("MM/dd/yyyy"), Endd);
               string str = Convert.ToString(Totaldays);
               string str1 = Convert.ToString(Totaldayss);
               OleDbCommand cmd123 = new OleDbCommand("UPDATE SlideShowTable SET Total_Days='" + str + "',Days_remain='" + str1 + "'", con);
               cmd123.ExecuteNonQuery();
           }
       }
 
Share this answer
 
Comments
Amirsalgar1 25-Apr-13 8:00am    
k trying
Amirsalgar1 25-Apr-13 8:09am    
Yup Its Working Sir ,i have debugged it , it is giving me correct values but at the it gives me this error
String was not recognized as a valid DateTime.
Amirsalgar1 25-Apr-13 8:37am    
now it is showing there is now row at position 3
int iRowCount = dt.Rows.Count;
string strPK;                             // string to store Primary Key
if ( iRowCount > 0)
{
      for(int intI=0; intI< iRowCount;intI++)
      {
           Stratd = dt.Rows[intI][11].ToString();
           Endd = dt.Rows[intI][12].ToString();
           strPK= dt.Rows[intI][x].ToString();  // where x is the Primary key column no.
           int Totaldays = duration(Stratd, Endd);
           int Totaldayss = durationn(System.DateTime.Now.ToString("MM/dd/yyyy"), Endd);
 
           string str = Convert.ToString(Totaldays);
           string str1 = Convert.ToString(Totaldayss);
 
 // replace your query with Update SlideShowTable SET Total_Days='" + str + "',Days_remain='" + str1 + "' WHERE Primary_Key='"+ strPK + "'" 
           OleDbCommand cmd123 = new OleDbCommand("UPDATE SlideShowTable SET Total_Days='" + str + "',Days_remain='" + str1 + "'" , con);
 
            cmd123.ExecuteNonQuery();
            }
        }

Hope this helps :)
 
Share this answer
 
v3
Comments
Amirsalgar1 25-Apr-13 8:12am    
Okiz checking .
Amirsalgar1 25-Apr-13 8:16am    
ERROR x is does not exist in current context
sir should i make it as int x?
Sumit Authankar 25-Apr-13 8:39am    
Every table has Primary key column. Please replace x with the column number for primary key.
It might be 0 in your case ;) Replace x with 0
Amirsalgar1 25-Apr-13 11:45am    
oki toki
Amirsalgar1 26-Apr-13 6:37am    
giving error :(

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