I have getting mntion error.
Error occured: Fill: SelectCommand.Connection property has not been initialized.
protected void btnSendMail_Click(object sender, EventArgs e)
{
string Id = string.Empty;
DataTable dt = new DataTable();
try
{
foreach (GridViewRow row in GridView1.Rows)
{
//get Current EMAIL_ID from the DataKey
Id = Convert.ToString(GridView1.DataKeys[row.RowIndex].Value);
CheckBox cb = (CheckBox)row.FindControl("chkSelect");
string flag = cb.Checked ? "Y" : "N";
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
SqlCommand cmd = new SqlCommand("UPDATE MESSAGE_SYSTEM SET flag = @flag where Id=@Id");
cmd.Connection = con;
cmd.Parameters.AddWithValue("@flag", flag);
cmd.Parameters.AddWithValue("@Id", Id);
con.Open();
string isUpdated = Convert.ToString(cmd.ExecuteNonQuery());
con.Close();
}
if (cb.Checked)
{
SqlCommand cmd = new SqlCommand("select VEN_EMP_EMAIL,EIC_MAIL_ID, PARTICULARS, REF_NO,AMT, VEN_EMP_PHNO, EIC_PH_NO from MESSAGE_SYSTEM where Id=" + Id + "", con);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
//Fill datatable with EMAIL_ID corresponding to Current EMP_ID
adp.Fill(dt);
//Get EMAIL_ID into variable
string emailId = dt.Rows[0]["VEN_EMP_EMAIL"].ToString();
string emailId1 = dt.Rows[0]["EIC_MAIL_ID"].ToString();
string PARTICULARS = dt.Rows[0]["PARTICULARS"].ToString();
string AMT = dt.Rows[0]["AMT"].ToString();
string REF_NO = dt.Rows[0]["REF_NO"].ToString();
string[] num = REF_NO.Split('&').Select(n => (n.Trim())).ToArray();
string phoneNo = dt.Rows[0]["VEN_EMP_PHNO"].ToString();
string phoneNo1 = dt.Rows[0]["EIC_PH_NO"].ToString();
//write code to send mail
// string message = "$#%^&*()?/|";
SendEmailUsingGmail(emailId, PARTICULARS, AMT, REF_NO);
SendEmailUsingGmail(emailId1, PARTICULARS, AMT, REF_NO);
sendSMS(phoneNo.Trim(), "Amount of Rs " + AMT + " for " + PARTICULARS + " is sent to your Bank account against "+ REF_NO +" ");
sendSMS(phoneNo1.Trim(), "Amount of Rs " + AMT + " for " + PARTICULARS + " is sent to your Bank account against " + REF_NO + " ");
dt.Clear();
dt.Dispose();
}
}
// ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), Guid.NewGuid().ToString(), "alert('Payment Details Sent Successfully');", true);
}
catch (Exception ex)
{
Response.Write("Error occured: " + ex.Message.ToString());
}
finally
{
Id = string.Empty;
}
System.Threading.Thread.Sleep(5000);
}