using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class blossomcounty2 : System.Web.UI.Page { string ConnectionString = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]; string myStringVariable; static string MessageURL = "http://sms.digialaya.com/API/SMSApiConnector.aspx";//System.Configuration.ConfigurationManager.AppSettings["MessageURL"]; static string UserName = "iavatarz";// System.Configuration.ConfigurationManager.AppSettings["UserName"]; static string Password = "pwd2017";// System.Configuration.ConfigurationManager.AppSettings["Password"]; static string FromName = "DIGIAL";// System.Configuration.ConfigurationManager.AppSettings["FromName"]; protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { string ConnectionString = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]; string myStringVariable; using (SqlConnection cn = new SqlConnection(ConnectionString)) { try { cn.Open(); SqlCommand command = new SqlCommand(); command.Parameters.Add(new SqlParameter("@Name", SqlDbType.VarChar)).Value = TextBoxName.Text.Trim(); command.Parameters.Add(new SqlParameter("@Email", SqlDbType.VarChar)).Value = TextBoxEmail.Text.Trim(); command.Parameters.Add(new SqlParameter("@Mobile", SqlDbType.VarChar)).Value = TextBoxMobile.Text.Trim(); command.Parameters.Add(new SqlParameter("@Enquiry", SqlDbType.VarChar)).Value = TextBoxEnquiry.Text.Trim(); command.CommandText = "Insert Into logix_county(Name,Email,Mobile,Enquiry) " + "Values(@Name,@Email,@Mobile,@Enquiry)"; command.Connection = cn; command.ExecuteNonQuery(); cn.Close(); if (!CheckOTP(TextBoxMobile.Text.Trim())) { myStringVariable = "Please enter valid and latest OTP."; ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + myStringVariable + "');", true); TextBoxOTP.Focus(); return; } else { UpdateOTP(TextBoxMobile.Text.Trim()); } } catch (Exception ex) { Response.Write(ex.ToString()); return; } } myStringVariable = "Thank you for your details. One of our representative will get back to you shortly."; ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + myStringVariable + "');", true); TextBoxEmail.Text = ""; TextBoxMobile.Text = ""; TextBoxName.Text = ""; TextBoxEnquiry.Text = ""; Response.Redirect("logixthankyou.html"); } bool CheckOTP(String MobileNo) { using (SqlConnection cn = new SqlConnection(ConnectionString)) { try { cn.Open(); SqlCommand command = new SqlCommand(); command.Parameters.Add(new SqlParameter("@Mobile", SqlDbType.VarChar)).Value = MobileNo; command.Parameters.Add(new SqlParameter("@OTPPassword", SqlDbType.VarChar)).Value = TextBoxOTP.Text.Trim(); command.CommandText = "Select Top 1 * from LogixOTP Where Mobile=@Mobile and OTPPassword=@OTPPassword and Verified=0 Order by CreationTime DESC"; command.Connection = cn; DataTable dt = new DataTable(); SqlDataAdapter da = new SqlDataAdapter(command); da.Fill(dt); cn.Close(); if (dt.Rows.Count == 0) return false; else return true; } catch (Exception ex) { Response.Write(ex.ToString()); return false; } } } void UpdateOTP(String MobileNo) { using (SqlConnection cn = new SqlConnection(ConnectionString)) { try { cn.Open(); SqlCommand command = new SqlCommand(); command.Parameters.Add(new SqlParameter("@Mobile", SqlDbType.VarChar)).Value = MobileNo; command.Parameters.Add(new SqlParameter("@OTPPassword", SqlDbType.VarChar)).Value = TextBoxOTP.Text.Trim(); command.CommandText = "Update LogixOTP Set Verified=1 Where Mobile=@Mobile and OTPPassword=@OTPPassword"; command.Connection = cn; command.ExecuteNonQuery(); cn.Close(); } catch (Exception ex) { Response.Write(ex.ToString()); } } } protected void TextBoxMobile_TextChanged(object sender, EventArgs e) { } protected void TextBoxMobile_TextChanged1(object sender, EventArgs e) { if (TextBoxMobile.Text.Trim() == null && TextBoxMobile.Text.Trim() == "") { myStringVariable = "Please enter 10 digit mobile no."; ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + myStringVariable + "');", true); return; } bool validmobile = ValidateMobileNo(TextBoxMobile.Text.Trim()); if (!validmobile) { myStringVariable = "Please enter 10 digit mobile no."; ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + myStringVariable + "');", true); return; } bool status = SendOTP(TextBoxMobile.Text.Trim()); if (status) { myStringVariable = "OTP has been sent to your mobile number. Please enter and Register your interest."; ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + myStringVariable + "');", true); } else { myStringVariable = "OTP has not been sent to your mobile number. Please try again."; ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + myStringVariable + "');", true); } } bool ValidateMobileNo(string mobileno) { try { if (mobileno.Length < 10) return false; else if (mobileno.Length > 12) return false; else if (Int64.Parse(mobileno) == 0) return true; else return true; } catch { return false; } } bool SendOTP(String MobileNo) { Random rand = new Random(); string otpPassword = rand.Next().ToString().Substring(0, 4); string Message = "Your OTP is " + otpPassword; String SMSText = Message;//GenerateSMSFormat(Message); string apiResponse = SendSms(SMSText, MobileNo); bool Status = !apiResponse.ToUpper().Contains("TRANSID"); if (Status) { using (SqlConnection cn = new SqlConnection(ConnectionString)) { try { cn.Open(); SqlCommand command = new SqlCommand(); command.Parameters.Add(new SqlParameter("@Mobile", SqlDbType.VarChar)).Value = MobileNo; command.Parameters.Add(new SqlParameter("@OTPPassword", SqlDbType.VarChar)).Value = otpPassword; command.Parameters.Add(new SqlParameter("@Verified", SqlDbType.Bit)).Value = false; command.CommandText = "Insert Into LogixOTP(Mobile,OTPPassword,Verified) " + "Values(@Mobile,@OTPPassword,@Verified)"; command.Connection = cn; command.ExecuteNonQuery(); cn.Close(); return true; } catch (Exception ex) { Response.Write(ex.ToString()); return false; } } } else { return false; } } public string SendSms(String text, string mobile) { if (mobile.Trim().Length == 0) return ""; String apiResponse = ""; try { apiResponse = SendSMSViaXML(text.Trim(), mobile, FromName); } catch (Exception Wexc) { //TextWriter writerError = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\Debugger\\Exception.txt", true); Response.Write("Error In SendSms function : " + Wexc.Message.ToString()); //writerError.Close(); return ""; } return apiResponse; } public static string SendSMSViaXML(String text, string mobile, string Name) { String apiResponse = ""; System.Net.WebClient webClient = new System.Net.WebClient(); try { //mobile = "91" + mobile; string SBAddress = mobile;//"
"; //string strURL = MessageURL + "?data=" + GetSMSXML(text.Trim(), SBAddress) + "&action=send"; string strURL = MessageURL + "?UserId=" + UserName + "&pwd=" + Password + "&Message=" + text.Trim() + "&Contacts=" + SBAddress + "&SenderId=DIGIAL&StartTime=&ServiceName=SMSOTP&MessageType="; apiResponse = webClient.DownloadString(strURL); } catch (Exception ex) { apiResponse = ex.Message; } return apiResponse; } }