/* ------------------------ My Meta Content Here SEO ------------------------ */

Pages

Main Menu

Friday, August 31, 2012

C Sharp dot net Send Bulk SMS (Sending sms to mutiple recipient.)

Handling Web Request in c sharp dot net

Code Here:

///
/// Method used to send bulk sms
///

///

public void SendMsg(string URL)
{
try
{
// Create a request using a URL that can receive a post.
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(URL);

webRequest.Method = WebRequestMethods.Http.Get;

webRequest.AllowAutoRedirect = true;

webRequest.PreAuthenticate = true;

webRequest.AuthenticationLevel = System.Net.Security.AuthenticationLevel.None;

webRequest.KeepAlive = false; // Set Keep alive to false (not to make persistent connection with server)

webRequest.ContentType = "application/x-www-form-urlencoded";

HttpWebResponse res = (HttpWebResponse)(webRequest.GetResponse());

System.IO.StreamReader sr = new System.IO.StreamReader(res.GetResponseStream(), Encoding.GetEncoding("UTF-8"));

StringBuilder responseText = new StringBuilder(String.Empty);

responseText.Append(sr.ReadToEnd());

webRequest = null;
}
catch (Exception _ex)
{}
}
________________________________________________________________________________

///
/// Method for sending sms to all user that belong to the hierarchy selected from the hierarchy chain
///
    private void SendSMS()
    {
        //Do not change
        string FeedID = "287089";
        //Your number
        string sendTo = "";
        //Do not change
        string Uname = "9833664117";
        //Do not change
        string Pwd = "awajw";

        string Query = "";

        string msg = "";

        dsRecord = ObjComFunc.GetAnnouncement(Convert.ToInt32(Request.QueryString["id"].ToString()), "SI");
        if (dsRecord != null)
        {
            if (dsRecord.Tables[0].Rows.Count > 0)
            {
                msg = Convert.ToString(dsRecord.Tables[0].Rows[0]["S_Brief_Desc"].ToString());
            }
        }
        else
        {
            msg = "";
        }
        
        //string msg = "We have received your request for verification code. Your code is 1234. If you have not requested code, kindly ignore this message.";

        //Gett All Center Numbers
        DataTable dtSendTo = GetCentersPhoneNumbers();

        if (dtSendTo != null)
        {
            if (dtSendTo.Rows.Count > 0)
            {
                for (int i = 0; i < dtSendTo.Rows.Count; i++)
                {
                    sendTo = dtSendTo.Rows[i][0].ToString().Trim();
                    Query = "http://bulkpush.mytoday.com/BulkSms/SingleMsgApi?feedid=" + FeedID + "&To=" + sendTo + "&UserName=" + Uname + "&Password=" + Pwd + "&Text=" + msg;
                    ObjComFunc.SendMsg(Query);
                    //Response.Redirect(Query);
                }
            }
        }
    }
________________________________________________________________________________
Read More »

Wednesday, August 22, 2012

ASP.NET Sorting in GridView By Columns Header In Asp.Net Ascending Descending


http://csharpdotnetfreak.blogspot.com/2012/06/sorting-gridview-columns-headers-aspnet.html

Sorting GridView By Columns Header In Asp.Net Ascending Descending

This is example of how to enable Sorting In GridView By Clicking on Columns Or Headers In Ascending Or Descending Direction In Asp.Net Using C# And VB

If you have Populated GridView With ObjectDataSource or SqlDataSOurce to Insert Update Edit Delete record , then You just have toset AllowSorting property to True andSortExpression property of columns to respective field from database to sort.

Setting these properties changes column names to hyperlink and No coding is required

Write html source as mentioned below 

<asp:GridView ID="gvDetails" runat="server" 
              AutoGenerateColumns="False" 
              onsorting="gvDetails_Sorting">
<Columns>
<asp:TemplateField HeaderText="First Name" SortExpression="FirstName">
<ItemTemplate>
<asp:Label ID="lblFname" runat="server" Text='<%#Eval("FirstName")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name" SortExpression="LastName">
<ItemTemplate>
<asp:Label ID="lblLname" runat="server" Text='<%#Eval("LastName")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location" SortExpression="Location">
<ItemTemplate>
<asp:Label ID="lblLocation" runat="server" Text='<%#Eval("Location")%>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


IF SQLCONNECTION AND SQLCOMMAND IS USED IN CODE BEHIND

If data is populated from database using code behind, then to sortwe need to write custom codeto save direction (Ascending or Descending) in ViewState and writecode in Sorting Event of Grid.

Populate and load GridView on Page_Load Event

BindGridView Method fetches data from database and return it as DataTable.

C# CODE

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            gvDetails.DataSource = BindGridView();
            gvDetails.DataBind();
        }
    }

    private DataTable BindGridView()
    {
        DataTable dtGrid = new DataTable();
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        string strSelect = "SELECT FirstName,LastName,Location FROM Details";
        SqlCommand cmd = new SqlCommand(strSelect, con);
        SqlDataAdapter dAdapter = new SqlDataAdapter(cmd);
        dAdapter.Fill(dtGrid);
        return dtGrid;
    }
VB.NET CODE

Protected Sub Page_Load(sender As Object, e As EventArgs)
 If Not Page.IsPostBack Then
  gvDetails.DataSource = BindGridView()
  gvDetails.DataBind()
 End If
End Sub

Private Function BindGridView() As DataTable
 Dim dtGrid As New DataTable()
 Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
 Dim strSelect As String = "SELECT FirstName,LastName,Location FROM Details"
 Dim cmd As New SqlCommand(strSelect, con)
 Dim dAdapter As New SqlDataAdapter(cmd)
 dAdapter.Fill(dtGrid)
 Return dtGrid
End Function
Create Public Property of SortDirection type and store direction in ViewState.
C# CODE
public SortDirection dir
    {
        get
        {
            if (ViewState["dirState"] == null)
            {
                ViewState["dirState"] = SortDirection.Ascending;
            }
            return (SortDirection)ViewState["dirState"];
        }
        set
        {
            ViewState["dirState"] = value;
        }
    }
VB.NET CODE
Public Property dir() As SortDirection
 Get
  If ViewState("dirState") Is Nothing Then
   ViewState("dirState") = SortDirection.Ascending
  End If
  Return DirectCast(ViewState("dirState"), SortDirection)
 End Get
 Set
  ViewState("dirState") = value
 End Set
End Property
Check Gridview's current direction from ViewState and set new sort direction in Sorting Event.
C# CODE
protected void gvDetails_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sortingDirection = string.Empty;
        if (dir == SortDirection.Ascending)
        {
            dir = SortDirection.Descending;
            sortingDirection = "Desc";
        }
        else
        {
            dir = SortDirection.Ascending;
            sortingDirection = "Asc";
        }
        
        DataView sortedView = new DataView(BindGridView());
        sortedView.Sort = e.SortExpression + " " + sortingDirection;
        gvDetails.DataSource = sortedView;
        gvDetails.DataBind();
    }
VB.NET CODE
Protected Sub gvDetails_Sorting(sender As Object, e As GridViewSortEventArgs)
 Dim sortingDirection As String = String.Empty
 If dir = SortDirection.Ascending Then
  dir = SortDirection.Descending
  sortingDirection = "Desc"
 Else
  dir = SortDirection.Ascending
  sortingDirection = "Asc"
 End If

 Dim sortedView As New DataView(BindGridView())
 sortedView.Sort = Convert.ToString(e.SortExpression) & " " & sortingDirection
 gvDetails.DataSource = sortedView
 gvDetails.DataBind()
End Sub
Read More »

Tuesday, August 21, 2012

SQL SERVER IN query between two comma separated column using split method

Split Method for split column value . . .


CREATE FUNCTION dbo.Split(@String varchar(8000), @Delimiter char(1))
returns @temptable TABLE (items varchar(8000))
as
begin
        declare @idx int
        declare @slice varchar(8000)
   

        select @idx = 1
                if len(@String)<1 is="is" nbsp="nbsp" null="null" or="or" p="p" return="return" tring="tring">   

        while @idx!= 0
        begin
                set @idx = charindex(@Delimiter,@String)
                if @idx!=0
                        set @slice = left(@String,@idx - 1)
                else
                        set @slice = @String
               

                if(len(@slice)>0)
                        insert into @temptable(Items) values(@slice)


                set @String = right(@String,len(@String) - @idx)
                if len(@String) = 0 break
        end
return
end
__________________________________________________________________________


WITH testdata
(
CommaColumn,
I_AddContest_Id,
I_ModeratorID,
S_Moderator,
S_ContestTitle,
S_Contest_Image,
S_Brief_Desc,
I_Hierarchy_Detail_ID,
I_Hierarchy_Master_ID
)
AS
(

SELECT
(',' + cast(I_ModeratorID as varchar) +',' + S_ZoneID +','+ S_AreaId +','+ S_CityId +','+ S_CentreID +',') as [FK_I_Hierarchy_Detail_ID],
A.I_AddContest_Id,
A.I_ModeratorID,
A.S_Moderator,
A.S_ContestTitle,
A.S_Contest_Image,
A.S_Brief_Desc,
B.I_Hierarchy_Detail_ID,
B.I_Hierarchy_Master_ID
FROM PULSEMKTG.T_Contest_Master AS A
LEFT OUTER JOIN T_User_Hierarchy_Details AS B
ON B.I_Hierarchy_Master_ID = A.I_ModeratorID
WHERE
A.Active=1
AND A.Approve=0 AND B.I_Status <> 0
AND GETDATE() >= ISNULL(B.Dt_Valid_From,GETDATE())
AND GETDATE() <= ISNULL(B.Dt_Valid_To,GETDATE())
AND B.I_User_ID = @I_User_ID  
)

SELECT
MAX(I_AddContest_Id) AS I_AddContest_Id  
,MAX(I_ModeratorID) AS I_ModeratorID
,MAX(S_Moderator) AS S_Moderator
,MAX(S_ContestTitle) AS S_ContestTitle  
,MAX(S_Contest_Image) AS S_Contest_Image
,MAX(cast(S_Brief_Desc as varchar(max))) AS S_Brief_Desc
FROM
(
SELECT D.items AS SplitValue,* FROM testdata AS C
CROSS APPLY dbo.Split(C.CommaColumn,',') AS D
WHERE
C.I_Hierarchy_Detail_ID = D.items
OR
C.I_Hierarchy_Master_ID = C.I_ModeratorID
) AS E

GROUP BY E.I_AddContest_Id
ORDER BY E.I_AddContest_Id DESC


Read More »

ASP.NET Getting files from directory


How to get files from directory on the server map path ?

GetFilesFromDirectory(MapPath("~/temp/"));
private void GetFilesFromDirectory(string DirPath)
{
     try
     {
     int id = 0;

     DirectoryInfo Dir = new DirectoryInfo(DirPath);         

     FileInfo[] FileList = Dir.GetFiles("*.*");

    dsFiles = comfunc.GetAllUserDetails(Convert.ToInt32(Request.QueryString["id"].ToString()), "SFILE");

    if (dsFiles.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < dsFiles.Tables[0].Rows.Count; i++)
                {
                    foreach (FileInfo FI in FileList)
                    {
                        if (dsFiles.Tables[0].Rows[i]["fname"].ToString() == FI.FullName)
                        {
                           FI.Delete();
                        }
                        id++;
                        //
                    }
                }
            }
            FillClientDetails();
        }
        catch (Exception ex)
        {

        }
    }
Read More »

My Blog List