Reputation: 1407
I need update the GridView
after send email to list of users registered in the table userTable
.
The GridView
is populated from 25 users at a time.
I send the email to 25 first users, updated the field SendEmail
for ID user
and now I need update the GridView
and show the next 25 users and so on.
I have tried this solution but after send email and update field SendEmail
for ID user
I see always first 25 users.
What am I missing?
What's wrong with this code?
Thank you in advance.
My code c# asp net:
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;
}
}
}
Upvotes: 0
Views: 2331
Reputation: 218827
btnBind.DataBind();
This doesn't bind the grid, it binds the button. You need to re-bind the grid itself. First abstract that logic into its own method:
private void BindGrid()
{
// basically all the code from btnBind_Click
}
Then call it from your handlers. For starters:
protected void btnBind_Click(object sender, EventArgs e)
{
BindGrid();
}
Then also in your logic after sending the emails:
cmd.ExecuteNonQuery();
Response.Write(sql1 + "<br /><br />");
BindGrid();
Upvotes: 1
Reputation: 136
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);
}
}
Upvotes: 1