コード例 #1
0
ファイル: SMSEntry.aspx.cs プロジェクト: mayankan/res-asp-erp
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string SMSTemplate = lstSMSTemplate.SelectedItem.ToString();
            int    count       = 0;

            foreach (GridViewRow item in grdAttendance.Rows)
            {
                DropDownList ddlSelection = item.FindControl("ddlSelection") as DropDownList;
                if (ddlSelection.SelectedValue == "0")
                {
                    int                    attendanceId  = Convert.ToInt32(grdAttendance.DataKeys[item.RowIndex].Value.ToString());
                    AttendanceCL           getAttendance = attendanceBLL.viewAttendanceById(attendanceId);
                    Collection <StudentCL> studentCol    = studentBLL.viewStudentsByClassId(getAttendance.classId);
                    foreach (StudentCL x in studentCol)
                    {
                        AttendanceCL attendanceCL = attendanceBLL.viewAttendanceByStudentIdandDate(x.id, getAttendance.date);
                        if (attendanceCL.studentLeaveTypeId == getAttendance.studentLeaveTypeId && x.studentCategoryId != 6)
                        {
                            //Your authentication key
                            string authKey = "136481ASa0LIdW5870d589";
                            //Multiple mobiles numbers separated by comma
                            string mobileNumber = x.fatherMobileNumber;
                            //Sender ID,While using route4 sender id should be 6 characters long.
                            string senderId = "RAINBO";
                            //Your message to send, Add URL encoding here.
                            string message = SMSTemplate.Replace("<&admNo>", x.admissionNo.ToString()).Replace("<&studentName>", x.studentName).Replace("<&fatherName>", x.fatherName).Replace("<&motherName>", x.motherName).Replace("<&class>", x.classSection).Replace("<&date>", getAttendance.date.ToString("dd MMM yyyy"));

                            //Prepare you post parameters
                            StringBuilder sbPostData = new StringBuilder();
                            sbPostData.AppendFormat("authkey={0}", authKey);
                            sbPostData.AppendFormat("&mobiles={0}", mobileNumber);
                            sbPostData.AppendFormat("&message={0}", message);
                            sbPostData.AppendFormat("&sender={0}", senderId);
                            sbPostData.AppendFormat("&route={0}", "4");

                            try
                            {
                                //Call Send SMS API
                                string sendSMSUri = "https://control.msg91.com/api/sendhttp.php";
                                //Create HTTPWebrequest
                                HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(sendSMSUri);
                                //Prepare and Add URL Encoded data
                                UTF8Encoding encoding = new UTF8Encoding();
                                byte[]       data     = encoding.GetBytes(sbPostData.ToString());
                                //Specify post method
                                httpWReq.Method        = "POST";
                                httpWReq.ContentType   = "application/x-www-form-urlencoded";
                                httpWReq.ContentLength = data.Length;
                                using (Stream stream = httpWReq.GetRequestStream())
                                {
                                    stream.Write(data, 0, data.Length);
                                }
                                //Get the response
                                HttpWebResponse response       = (HttpWebResponse)httpWReq.GetResponse();
                                StreamReader    reader         = new StreamReader(response.GetResponseStream());
                                string          responseString = reader.ReadToEnd();

                                //Close the response
                                reader.Close();
                                response.Close();
                                attendanceBLL.addSMSEntry(new SMSEntryCL
                                {
                                    attendanceId  = attendanceCL.id,
                                    dateCreated   = DateTime.Now,
                                    isDeleted     = false,
                                    smsTemplateId = Convert.ToInt32(lstSMSTemplate.SelectedValue),
                                });
                            }
                            catch (SystemException ex)
                            {
                                throw (new Exception(ex.Message));
                            }
                            count++;
                        }
                    }
                }
            }
            if (count == 0)
            {
                lblUpdate.Text = "Attendance Not Selected.";
            }
            else
            {
                lblUpdate.Text = "SMS sent to " + count + "Entries/Columns. The page will redirect in 10 seconds.";
                Response.AppendHeader("Refresh", "10;url=index.aspx");
            }
        }