Send SMS to users which GridView CheckBox is Checked using C# in ASP.Net

Last Reply 8 months ago By indradeo

Posted 8 months ago

how to send bulk sms using asp c#

namespace Email
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection(@"Data Source=(localdb)\Projects;Initial Catalog=Employee;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindEmpGrid();
            }
        }
        protected void BindEmpGrid()
        {
            SqlCommand cmd = new SqlCommand("select * from email", con);
            DataTable dt = new DataTable();
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            adp.Fill(dt);
            grEmp.DataSource = dt;
            grEmp.DataBind();
        }
        protected void Button2_Click(object sender, System.EventArgs e)
        {

            string Id = string.Empty;
            DataTable dt = new DataTable();
            try
            {
                foreach (GridViewRow row in grEmp.Rows)
                {
                    CheckBox cb = (CheckBox)row.FindControl("chkSelect");

                    if (cb.Checked == true)
                    {
                        if (cb != null && cb.Checked)
                        {
                            //get Current EMAIL_ID from the DataKey
                            Id = Convert.ToString(grEmp.DataKeys[row.RowIndex].Value);
                            SqlCommand cmd = new SqlCommand("select phone from email 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 phone = dt.Rows[0]["phone"].ToString();
                            //write code to send mail
                            SendEmailUsingGmail(phone);
                            dt.Clear();
                            dt.Dispose();
                        }
                    }
                    {
                        string SMSurl = "http://pay4sms.in/sendsms/?token=d9279885httrtrta4ceeehfduytfjhk6784567654sdfsd392df8cf4208d0fc78&credit=7&sender=SAM&message=" + Message + "&number=" + Number1 + "";
                        WebRequest request = HttpWebRequest.Create(SMSurl);
                        WebResponse response = request.GetResponse();
                        string retString = response.ToString();
                        response.Close();
                        return retString;
                    }

                }
                ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), Guid.NewGuid().ToString(), "alert('SMS sent successfully');", true);
            }
            catch (Exception ex)
            {
                Response.Write("Error occured: " + ex.Message.ToString());
            }
            finally
            {
                Id = string.Empty;
            }
        }
    }
}

 

Posted 8 months ago

 

namespace Email
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection(@"Data Source=(localdb)\Projects;Initial Catalog=Employee;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindEmpGrid();
            }
        }
        protected void BindEmpGrid()
        {
            SqlCommand cmd = new SqlCommand("select * from email", con);
            DataTable dt = new DataTable();
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            adp.Fill(dt);
            grEmp.DataSource = dt;
            grEmp.DataBind();
        }

        protected void Button2_Click(object sender, System.EventArgs e)
        {
            string Id = string.Empty;
            DataTable dt = new DataTable();
            try
            {
                foreach (GridViewRow row in grEmp.Rows)
                {
                    CheckBox cb = (CheckBox)row.FindControl("chkSelect");
                    if (cb.Checked)
                    {
                        //get Current EMAIL_ID from the DataKey
                        Id = Convert.ToString(grEmp.DataKeys[row.RowIndex].Value);
                        SqlCommand cmd = new SqlCommand("select phone from email 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 phone = dt.Rows[0]["phone"].ToString();
                        string txn_desc = dt.Rows[0]["txn_desc"].ToString();
                        //write code to send sms
                        Sendsms(phone, txn_desc);
                        dt.Clear();
                        dt.Dispose();
                    }
                }
                ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), Guid.NewGuid().ToString(), "alert('SMS sent successfully');", true);
            }
            catch (Exception ex)
            {
                Response.Write("Error occured: " + ex.Message.ToString());
            }
            finally
            {
                Id = string.Empty;
            }

        }
        private void Sendsms(string phone, string txn_desc)
        {
            try
            {
                string SMSurl = "my gate way" + txn_desc + "&phone=" + txn_desc + "";
                WebRequest request = HttpWebRequest.Create(SMSurl);
                WebResponse response = request.GetResponse();
                string retString = response.ToString();
                response.Close();
            }
            catch (Exception ex)
            {
                Response.Write("Error occured: " + ex.Message.ToString());
            }
        }
    }
}

 

<fieldset style="width: 978px; height: 404px;">
    <legend>Send Mail and SMS multiple users </legend>
    <table class="auto-style2">
        <tr>
            <td>
                <asp:GridView ID="grEmp" runat="server" AllowPaging="True" AutoGenerateColumns="False"
                    DataKeyNames="Id" GridLines="None" Width="170%" CellPadding="4" ForeColor="#333333"
                    CssClass="auto-style1" Height="244px">
                    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                    <Columns>
                        <asp:BoundField DataField="vn_emp_cd" HeaderText="vn_emp_cd" />
                        <asp:BoundField DataField="ven_emp_name" HeaderText="ven_emp_name" />
                        <asp:BoundField DataField="txn_desc" HeaderText="txn_desc" />
                        <asp:BoundField DataField="amt" HeaderText="amt" />
                        <asp:BoundField DataField="phone" HeaderText="phone" />
                        <asp:BoundField DataField="email" HeaderText="Email Id" />
                        <asp:TemplateField HeaderText="CheckAll">
                            <HeaderTemplate>
                                <asp:CheckBox ID="chkSelectAll" runat="server" AutoPostBack="true" OnCheckedChanged="chkSelectAll_CheckedChanged" />Send
                                Mail To All ?
                            </HeaderTemplate>
                            <ItemTemplate>
                                <asp:CheckBox ID="chkSelect" runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <EditRowStyle BackColor="#999999" />
                    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                    <SortedAscendingCellStyle BackColor="#E9E7E2" />
                    <SortedAscendingHeaderStyle BackColor="#506C8C" />
                    <SortedDescendingCellStyle BackColor="#FFFDF8" />
                    <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
                </asp:GridView>
            </td>
        </tr>
    </table>
    <asp:Button ID="btnSendMail" runat="server" Text="Send Email" OnClick="btnSendMail_Click"
        CausesValidation="false" />
    <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Send SMS" CausesValidation="false" />
</fieldset>