How to send SMS using ASP.NET through HTTP [c#, from website]

protected void buttonSendOnClick(object sender, EventArgs e)
{
//are required fields filled in:
if (textboxRecipient.Text == “”)
{
textboxError.Text += “Recipient(s) field must not be empty!\n”;
textboxError.Visible = true;
return;
}

//we creating the necessary URL string:
string ozSURL = “http://127.0.0.1”; //where the SMS Gateway is running
string ozSPort = “9501”; //port number where the SMS Gateway is listening
string ozUser = HttpUtility.UrlEncode(“admin”); //username for successful login
string ozPassw = HttpUtility.UrlEncode(“abc123”); //user’s password
string ozMessageType = “SMS:TEXT”; //type of message
string ozRecipients = HttpUtility.UrlEncode(textboxRecipient.Text); //who will get the message
string ozMessageData = HttpUtility.UrlEncode(textboxMessage.Text); //body of message

string createdURL = ozSURL + “:” + ozSPort + “/httpapi” +
“?action=sendMessage” +
“&username=” + ozUser +
“&password=” + ozPassw +
“&messageType=” + ozMessageType +
“&recipient=” + ozRecipients +
“&messageData=” + ozMessageData;

try
{
//Create the request and send data to the SMS Gateway Server by HTTP connection
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(createdURL);

//Get response from the SMS Gateway Server and read the answer
HttpWebResponse myResp = (HttpWebResponse)myReq.GetResponse();
System.IO.StreamReader respStreamReader = new System.IO.StreamReader(myResp.GetResponseStream());
string responseString = respStreamReader.ReadToEnd();
respStreamReader.Close();
myResp.Close();

//inform the user
textboxError.Text = responseString;
textboxError.Visible = true;
}
catch (Exception)
{
//if sending request or getting response is not successful the SMS Gateway Server may do not run
textboxError.Text = “The SMS Gateway Server is not running!”;
textboxError.Visible = true;
}

}

Create random password automatically in asp.net c#

#region Random Password Generation
//passwordLength is length of password you want i.e; 8 to 12

        public string CreateRandomPassword(int passwordLength)
        {
            string allowedChars = “abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789!@$?_-“;
            char[] chars = new char[passwordLength];
            Random rd = new Random();
            for (int i = 0; i < passwordLength; i++)
            {
                chars[i] = allowedChars[rd.Next(0, allowedChars.Length)];
            }
            return new string(chars);
        }

        #endregion

Ajax Web Method in Asp.net C#

HTML PAGE
<div>
Your Name :
<asp:TextBoxID=”txtUserName” runat=”server”></asp:TextBox>
<inputid=”btnGetTime”type=”button”value=”Show Current Time”
    onclick=”ShowCurrentTime()”/>
</div>
JAVASCRIPT
<script src=”scripts/jquery-1.3.2.min.js”type=”text/javascript”></script>
<script type=”text/javascript”>
function ShowCurrentTime() {
    $.ajax({
        type: “POST”,
        url: “Default.aspx/GetCurrentTime”,
        data: ‘{name: “‘ + $(“#<%=txtUserName.ClientID%>”)[0].value + ‘” }’,
        contentType: “application/json; charset=utf-8”,
        dataType: “json”,
        success: OnSuccess,
        failure: function(response) {
            alert(response.d);
        }
    });
}
function OnSuccess(response) {
    alert(response.d);
}
</script>
SERVER SIDE
[System.Web.Services.WebMethod]
publicstaticstring GetCurrentTime(string name)
{
    return”Hello ” + name + Environment.NewLine + “The Current Time is: “
        + DateTime.Now.ToString();
}

How to insert html content in sql server and asp.net

To Encode the html text and store in sqlserver.

  • string htmlencoded = Server.HtmlEncode(<html>dhaval</html>);
  • Now pass this string as an argument to the method which is used to insert data in sql database.

 

To retrieve from sqlserver or to decode html.

You have to use Literal Control from toolkit.(Id=’literal1′)

  • literal1.Text = HttpUtility.HtmlDecode(htmlencoded);