2014-10-02 98 views
0

後自動刷新ASP.NET的GridView我需要更新GridView發送電子郵件後,在表userTable註冊用戶的列表。如何更新

GridView從25個用戶一次填充。

我發送電子郵件至25個用戶第一,更新領域SendEmailID user,現在我需要更新GridView和顯示下一個25個用戶等等。

我已經試過這個解決方案,但後發送電子郵件和更新領域SendEmailID user我看到的總是先25個用戶。

我錯過了什麼?

這段代碼有什麼問題?

預先感謝您。

我的代碼C#ASP網:

protected void btnSend_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     SmtpClient smtpClient = new SmtpClient(); 
     System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage(); 

     lbltotalcount.Text = string.Empty; 

     foreach (GridViewRow grow in grvCustomers.Rows) 
     { 
      try 
      { 
       ID = grow.Cells[0].Text.Trim(); 
       name = grow.Cells[1].Text.Trim(); 
       email = grow.Cells[2].Text.Trim(); 

       //Send email; 

       using (OdbcConnection conn = 
            new OdbcConnection(ConfigurationManager.ConnectionStrings["cs"].ConnectionString)) 
       { 
        sql1 = " UPDATE userTable SET `SendEmail` = 1 WHERE ID = ?; "; 

        using (OdbcCommand cmd = new OdbcCommand(sql1, conn)) 
        { 
         try 
         { 
          conn.Open(); 
          cmd.Parameters.AddWithValue("param1", ID.ToString());         
          cmd.ExecuteNonQuery(); 
          Response.Write(sql1 + "<br /><br />"); 
          btnBind.DataBind(); 
         } 
         catch (Exception ex) 
         { 
          Response.Write("Send Email Failed." + ex.Message); 
         } 
         finally 
         { 
          conn.Close(); 
         } 
        } 
       } 
      } 
      catch (Exception ex) 
      { 
       Response.Write(ex.Message); 
      } 
     } 
    } 
    catch (Exception ex) 
    { 
     Response.Write(ex.Message); 
    } 
} 

protected void btnBind_Click(object sender, EventArgs e) 
{ 
    sql = " SELECT * from userTable WHERE `SendEmail` = 0 LIMIT 25; "; 

    using (OdbcConnection conn = 
         new OdbcConnection(ConfigurationManager.ConnectionStrings["cs"].ConnectionString)) 
    { 
     conn.Open(); 
     using (OdbcCommand cmd = new OdbcCommand(sql, conn)) 
     { 
      try 
      { 
       OdbcDataAdapter adp = new OdbcDataAdapter(cmd); 
       DataSet ds = new DataSet(); 
       adp.Fill(ds); 
       grvCustomers.DataSource = ds; 
       grvCustomers.DataBind(); 
       lbltotalcount.Text = grvCustomers.Rows.Count.ToString(); 
      } 
      catch (Exception ex) 
      { 
       Response.Write("Send Email Failed." + ex.Message); 
      } 
      finally 
      { 
       conn.Close(); 
      } 

      btnBind.Visible = false; 
     } 
    } 
} 

edit #1

protected void btnBind_Click(object sender, EventArgs e) 
{ 
    BindGrid(); 
} 



protected void btnSend_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     SmtpClient smtpClient = new SmtpClient(); 
     System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage(); 

     lbltotalcount.Text = string.Empty; 

     foreach (GridViewRow grow in grvCustomers.Rows) 
     { 
      try 
      { 
       ID = grow.Cells[0].Text.Trim(); 
       name = grow.Cells[1].Text.Trim(); 
       email = grow.Cells[2].Text.Trim(); 

       //Send email; 

       using (OdbcConnection conn = 
            new OdbcConnection(ConfigurationManager.ConnectionStrings["cs"].ConnectionString)) 
       { 
        sql1 = " UPDATE userTable SET `SendEmail` = 1 WHERE ID = ?; "; 

        using (OdbcCommand cmd = new OdbcCommand(sql1, conn)) 
        { 
         try 
         { 
          conn.Open(); 
          cmd.Parameters.AddWithValue("param1", ID.ToString());         
          cmd.ExecuteNonQuery(); 
          Response.Write(sql1 + "<br /><br />"); 

          BindGrid(); 

         } 
         catch (Exception ex) 
         { 
          Response.Write("Send Email Failed." + ex.Message); 
         } 
         finally 
         { 
          conn.Close(); 
         } 
        } 
       } 
      } 
      catch (Exception ex) 
      { 
       Response.Write(ex.Message); 
      } 
     } 
    } 
    catch (Exception ex) 
    { 
     Response.Write(ex.Message); 
    } 
} 

protected void BindGrid(); 
{ 
    sql = " SELECT * from userTable WHERE `SendEmail` = 0 LIMIT 25; "; 

    using (OdbcConnection conn = 
         new OdbcConnection(ConfigurationManager.ConnectionStrings["cs"].ConnectionString)) 
    { 
     conn.Open(); 
     using (OdbcCommand cmd = new OdbcCommand(sql, conn)) 
     { 
      try 
      { 
       OdbcDataAdapter adp = new OdbcDataAdapter(cmd); 
       DataSet ds = new DataSet(); 
       adp.Fill(ds); 
       grvCustomers.DataSource = ds; 
       grvCustomers.DataBind(); 
       lbltotalcount.Text = grvCustomers.Rows.Count.ToString(); 
      } 
      catch (Exception ex) 
      { 
       Response.Write("Send Email Failed." + ex.Message); 
      } 
      finally 
      { 
       conn.Close(); 
      } 

      btnBind.Visible = false; 
     } 
    } 
} 
+0

是您提交後重新綁定再次抓住你的數據嗎? – Kritner 2014-10-02 18:53:02

+0

發送電子郵件後,您需要重新綁定GridView。 – 2014-10-02 18:53:13

+0

重新綁定網格對此無濟於事。發送郵件後..獲取下25條記錄並將這25行再次綁定到網格。 – Rahul 2014-10-02 18:54:13

回答

1
btnBind.DataBind(); 

這不綁定電網,它結合了按鈕。你需要重新綁定網格本身。首先將該邏輯抽象爲自己的方法:

private void BindGrid() 
{ 
    // basically all the code from btnBind_Click 
} 

然後從您的處理程序中調用它。對於初學者:

protected void btnBind_Click(object sender, EventArgs e) 
{ 
    BindGrid(); 
} 

然後還你的邏輯發送郵件後:

cmd.ExecuteNonQuery(); 
Response.Write(sql1 + "<br /><br />"); 
BindGrid(); 
+0

謝謝,我已經編輯我的代碼與您的建議,但現在我有這個錯誤'發送電子郵件失敗。ConnectionString屬性尚未初始化。請在第一篇文章中閱讀編輯#1。 – 2014-10-02 19:05:39

1
protected void btnSend_Click(object sender, EventArgs e) 
{ 
after send mail and update table. then rebind data to gridview, 
For example: 
    try 
    { 
     //send email 

    //it will bind next 25 records 
    btnBind_Click(sender, e); 
    } 
}