Saturday, December 19, 2009

sending sms from asp.net

I use the following two functions in my application to send sms from my asp.net application.
I use c# as programming language.
I provide some credential which I got from the sms API provider. we use www.smscountry.com API in application.
I call the setupSMS function to send sms from my application which requires mobileNumber and message as input parameter and returns the response of the message sent.


    private string setupSMS(string mobileNumber, string message)
    {        
        string strPostBody;        
        String strPostResponse;


        //'Give The Respective Variable Values        
        string userName = "UserId"; //'Enter Your User Name provided by API Provider     
        string password = "Password";//'Enter Your Password provided by API Provider        
        string senderid = "senderId";//' Enter Your Senderid provided by API Provider


        string messageType = "N";//' Enter Your Message Type As N for Normal Message, O for Other Laungauge Sms        
        Char deliveryReports = 'Y';//' if U want To Know Delivery Reports Enter Y it Gives JOBID Otherwise N 


        //'****If U R Behind The Proxy Server Uncomment And Give Below Details *****
        //'objProxy = New WebProxy("Proxy Ip Adress", PortNumber)


        //'Call sendSMS Method For Sending The Sms's
        
        message = Server.UrlEncode(message);        
        strPostBody = "User=" + userName + "&passwd=" + password + "&mobilenumber=" + mobileNumber + "&message=" + message + "&sid=" + senderid + "&mtype=" + messageType + "&DR=" + deliveryReports;
        strPostResponse = sendSMS(strPostBody);
        return strPostResponse;
    }


    private string sendSMS(string stringPost)
    {
        
        HttpWebRequest objWebRequest = null;        
        HttpWebResponse objWebResponse;        
        StreamWriter objStreamWriter = null;        
        StreamReader objStreamReader = null;        
        try
        {
            
            string stringResult;
            
            objWebRequest = (HttpWebRequest)(WebRequest.Create("http://www.smscountry.com/smscwebservice.asp"));
            
            objWebRequest.Method = "POST";
            
            if (objProxy != null)
            {
                objWebRequest.Proxy = objProxy;
            }            
            objWebRequest.ContentLength = stringPost.Length;            
            objWebRequest.ContentType = "application/x-www-form-urlencoded";            
            objStreamWriter = new StreamWriter(objWebRequest.GetRequestStream());            
            objStreamWriter.Write(stringPost);            
            objStreamWriter.Flush();            
            objStreamWriter.Close();
            
            objWebResponse = (HttpWebResponse)(objWebRequest.GetResponse());
            objStreamReader = new StreamReader(objWebResponse.GetResponseStream());
            stringResult = objStreamReader.ReadToEnd();
            objStreamReader.Close();
            return stringResult;  //'jobid
            
        }
        catch (Exception ex)
        {
            return ex.ToString();
        }
        finally
        {
            if (objStreamWriter != null)
            {
                objStreamWriter.Close();
            }
            if (objStreamReader != null)
            {
                objStreamReader.Close();
            }
            objWebRequest = null;
            objWebResponse = null;
            objProxy = null;
        }        
    }


hope this post help you in sending SMS through asp.net application.
Enjoy coding.............................................

My family photos


Thursday, December 17, 2009

Print a Datalist partial from an ASP.NET Web Page

script language=javascript>
function CallPrint(strid)
{
var prtContent = document.getElementById(strid);
var WinPrint = window.open('','','letf=0,top=0,width=1,height=1,toolbar=0,scrollbars=0,status=0');
WinPrint.document.write(prtContent.innerHTML);
WinPrint.document.close();
WinPrint.focus();
WinPrint.print();
WinPrint.close();
prtContent.innerHTML=strOldOne;
}


<div id="divPrint">
<asp:datagrid>
.....
.....
.....
</asp:datagrid>
</div>
<asp:button ID="btnPrint" onClick="javascript:CallPrint('divPrint');" Runat=Server />

Thursday, December 10, 2009

how to import csv file in dotnet

In dot net I use the following method to import data from csv file


public static DataTable GetDataFromCSV(string FilePath)
{
    string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                                                        "Data Source=" + FilePath + ";" +
                                                        "Extended Properties=Excel 8.0;";

    using (OleDbConnection cn = new OleDbConnection(ConnectionString))
    {
        cn.Open();

        DataTable dbSchema = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
        if (dbSchema == null || dbSchema.Rows.Count < 1)
        {
                throw new Exception("Error: Could not determine the name of the first worksheet.");
        }

        string WorkSheetName = dbSchema.Rows[0]["TABLE_NAME"].ToString();

        OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM [" + WorkSheetName + "]", cn);
        DataTable dt = new DataTable(WorkSheetName);

        da.Fill(dt);

        return dt;
    }
}

Monday, December 7, 2009

How to Encrypt a section in web.config

To Encrypt on IIS
aspnet_regiis.exe -pe "[Section To Encrypt]" -app "/[Virtual Directory on IIS]"


eg---
aspnet_regiis.exe -pe "connectionStrings" -app "/vitualdirectory"


To decrypt on IIS 
aspnet_regiis.exe -de "[Section To Encrypt]" -app "/[Virtual Directory on IIS]"


eg---
aspnet_regiis.exe -de "connectionStrings" -app "/vitualdirectory"