Friday 24 August 2012

sample program for using foreach loop in asp.net


write the fallowing code in .aspx source file
<h2 style="color:Black">foreach loop</h2>  
        <asp:Label ID="Label1" runat="server" Font-Size="Large" ForeColor="red"></asp:Label>  
        <br /><br />  
        <asp:CheckBoxList   
            ID="CheckBoxList1"  
            runat="server"  
            ForeColor="AntiqueWhite"  
            BackColor="Black"  
            BorderColor="Orange"  
            BorderWidth="2"  
            BorderStyle="Double"  
            RepeatColumns="3"> 
            <asp:ListItem>Volly Ball</asp:ListItem>  
            <asp:ListItem>Cricket</asp:ListItem>  
            <asp:ListItem>Hockey</asp:ListItem>  
            <asp:ListItem>Basket Ball</asp:ListItem>  
            <asp:ListItem>Foot Ball</asp:ListItem>  
            <asp:ListItem>Swimming</asp:ListItem>
            </asp:CheckBoxList>  
        <br /><br />  
        <asp:Button ID="Button1" runat="server" Text="Select color" ForeColor="Blue" 
            onclick="Button1_Click" />

Write Fallowing code in button_click event

 protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = "You are selected: ";
        foreach (ListItem LI in CheckBoxList1.Items)
        {
            if (LI.Selected == true)
            {
                Label1.Text += LI+" "+"&"+" ";
            }
        }
    }


basic information about VSTO(visual studio tools for office)


(Visual Studio Tools for Office) Development tools that are part of Visual Studio for enhancing Office applications. Based on Microsoft's .NET environment, VSTO is used to create Office "add-ins" that perform functions such as automatically filling in fields in a Word or Excel document and e-mailing the files or uploading them to a SharePoint server. As of VSTO 2005, support for enhancing Outlook was added.
Prior to VSTO, macros and Visual Basic for Applications (VBA) were the common methods used to customize Office programs. Although they are still used, VSTO makes it easier to code many types of enhancements, which reside as separate DLLs in the user's computer.

Sunday 19 August 2012

3-tier architecture sample program in asp.net

hello buddies now i am going to explain bout ,how to create objects and how to pass values from one layer to other layer and creating proc for this app..simply fallow the code...i have taken 3 class files in App_Code  and taken 1 .aspx file.let's start from BEL.
creating properties in BEL.

public class BEL
{
public BEL()
{
//
// TODO: Add constructor logic here
//
}
    private string _fname;

    public string Fname
    {
        get { return _fname; }
        set { _fname = value; }
    }

    private string _lname;

    public string Lname
    {
        get { return _lname; }
        set { _lname = value; }
    }
    private int _age;

    public int Age
    {
        get { return _age; }
        set { _age = value; }
    }
}
Code In Web.Config file

<connectionStrings>
<add name="ConStr" connectionString="Data Source=SHANKAR;Initial Catalog=ltc_gamble;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>


write the fallowing code in DAL

public class PersonDAL3
{
    string connStr = ConfigurationManager.ConnectionStrings["ConStr"].ToString();
    public PersonDAL3()
    { }
    public int Insert(BEL objBEL)
    {
        SqlConnection conn = new SqlConnection(connStr);
        conn.Open();
        SqlCommand dCmd = new SqlCommand("InsertData", conn);
        dCmd.CommandType = CommandType.StoredProcedure;
        try
        {
            dCmd.Parameters.AddWithValue("@fname",objBEL.Fname);
            dCmd.Parameters.AddWithValue("@lname", objBEL.Lname);
            dCmd.Parameters.AddWithValue("@age",objBEL.Age);
            return dCmd.ExecuteNonQuery();
        }
        catch
        {
            throw;
        }
        finally
        {
            dCmd.Dispose();
            conn.Close();
            conn.Dispose();
        }
    }
}

Write the fallowing code in BAL


public class PersonBAL3
{
public PersonBAL3()
{}
    public int Insert(BEL objBELinbl)
    {
        PersonDAL3 pDAL = new PersonDAL3();
        try
        {
            return pDAL.Insert(objBELinbl);
        }
        catch
        {
            throw;
        }
        finally
        {
            pDAL = null;
        }
    }
  

}
Write the fallowing source code in .aspx source file with in the head tag

 <asp:Label ID="lblMessage" runat="Server" ForeColor="red" EnableViewState="False"></asp:Label>
        <table style="border: 2px solid #cccccc;">
            <tr style="background-color: #C0C0C0; color: White;">
                <th colspan="3">
                    Add Records
                </th>
            </tr>
            <tr>
                <td>
                    First Name:
                </td>
                <td>
                    <asp:TextBox ID="txtFirstName" runat="Server"></asp:TextBox>
                </td>
                <td>
                    <asp:RequiredFieldValidator ID="req1" runat="Server" Text="*" ControlToValidate="txtFirstName"
                        Display="dynamic"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td>
                    Last Name:
                </td>
                <td>
                    <asp:TextBox ID="txtLastName" runat="Server"></asp:TextBox>
                </td>
                <td>
                    <asp:RequiredFieldValidator ID="req2" runat="Server" Text="*" ControlToValidate="txtLastName"
                        Display="dynamic"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td>
                    Age:
                </td>
                <td>
                    <asp:TextBox ID="txtAge" runat="Server" Columns="4"></asp:TextBox>
                </td>
                <td>
                    <asp:RequiredFieldValidator ID="req3" runat="Server" Text="*" ControlToValidate="txtAge"
                        Display="dynamic"></asp:RequiredFieldValidator>
                    <asp:CompareValidator ID="Comp1" runat="Server" Text="Only integer" ControlToValidate="txtAge"
                        Operator="DataTypeCheck" Type="Integer"></asp:CompareValidator>
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;
                </td>
                <td>
                    <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="AddRecords" />
                </td>
            </tr>
        </table>

Write this code in .aspx.cs file button_click event

public partial class Spider : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void AddRecords(object sender, EventArgs e)
    {      
        if (!Page.IsValid)
            return;
        int intResult = 0;
        // Page is valid, lets go ahead and insert records

        // Instantiate BAL object
        BEL objBEL = new BEL();
        PersonBAL3 pBAL = new PersonBAL3();
        // Instantiate the object we have to deal with
        objBEL.Fname = txtFirstName.Text;
        objBEL.Lname = txtLastName.Text;
        objBEL.Age = Int32.Parse(txtAge.Text);
        try
        {
            intResult = pBAL.Insert(objBEL);
            if (intResult > 0)
                lblMessage.Text = "New record inserted successfully.";
            else
                lblMessage.Text = "FirstName [<b>"+ txtFirstName.Text +"</b>] alredy exists, try another name";
        }
        catch (Exception ee)
        {
            lblMessage.Text = ee.Message.ToString();
        }
        finally
        {
            pBAL = null;
        }    
    }
}

Here is the table and procedure for fallowing App.

CREATE TABLE INSERTING
(
PERSONID INT PRIMARY KEY IDENTITY(101,1),
FNAME VARCHAR(30),
LNAME VARCHAR(30),
AGE INT
)
ALTER PROC InsertData
@FNAME VARCHAR(30),
@LNAME VARCHAR(30),
@AGE INT
AS
BEGIN
INSERT INTO INSERTING(FNAME,LNAME,AGE)VALUES(@FNAME,@LNAME,@AGE)
END

EXEC InsertData 'SHANKAR','PARSHIMONI',24

Enjoy the coading,Thanks for visiting this blog,





Thursday 16 August 2012

validation for start date should not be less than system date and end date should be greater than start date in java script

Write the code in .aspx source file within the head tag


<script type="text/javascript">
       function ChkDate(sender, arg) {
           var selectedDate = new Date();
           selectedDate = sender._selectedDate;
           var todayDate = new Date();
           var txtDate1 = document.getElementById('txtStartDatePop').value;
           var tostartDate = new Date(txtDate1);
           if (selectedDate.getDateOnly() < todayDate.getDateOnly()) {
               sender._selectedDate = todayDate;
               sender._textbox.set_Value(sender._selectedDate.format(sender._format));
               alert("Start Date should not be less than Current Date.");
           }
       }
       function CheckStartDate(sender, args) {
           var txtDate1 = document.getElementById('txtStartDatePop').value;          
           var tostartDate = new Date(txtDate1);         
           var enddateDate = new Date(sender._selectedDate);       
           if (txtDate1.length > 0) {            
               if (enddateDate < tostartDate) {                
                   sender._selectedDate = tostartDate;               
                   sender._textbox.set_Value(sender._selectedDate.format(sender._format));                
                   alert("End Date must greater then or Equal start date");                 
               }              
           }
       }
</script>

Then after write the statement in calender extender controls
1.OnClientDateSelectionChanged="ChkDate"
2.OnClientDateSelectionChanged=" CheckStartDate "

enjoy the coading

Saturday 11 August 2012

Creating Controls dynamically in asp.net

for creating dynamic controls in asp.net , we should take panel control, write the code in default.aspx source 

default.aspx file
<div>
    <asp:Panel ID="pnlinfo" runat="server" Height="36px" Width="1349px">
    </asp:Panel>
</div>

default.aspx.cs file
protected void Page_Load(object sender, EventArgs e)
    {
        Table tbldynamic = new Table();
        TableCell tc = new TableCell();
        TableCell tc1 = new TableCell();
        TableRow tr = new TableRow();
        Label lblName = new Label();
        lblName.ID = "lblName";
        lblName.Text = "UserName:";
        tc.Controls.Add(lblName);
        TextBox txtName = new TextBox();
        txtName.ID = "txtName";
        tc1.Controls.Add(txtName);
        tr.Cells.Add(tc);
        tr.Cells.Add(tc1);
        tbldynamic.Rows.Add(tr);
        tc = new TableCell();
        tc1 = new TableCell();
        tr = new TableRow();
        Label lblEmail = new Label();
        lblEmail.ID = "lblEmail";
        lblEmail.Text = "Email:";
        tc.Controls.Add(lblEmail);
        TextBox txtEmail = new TextBox();
        txtEmail.ID = "txtEmail";
        tc1.Controls.Add(txtEmail);
        tr.Cells.Add(tc);
        tr.Cells.Add(tc1);
        tbldynamic.Rows.Add(tr);
        tc = new TableCell();
        tc1 = new TableCell();
        tr = new TableRow();
        Button btnSubmit = new Button();
        btnSubmit.ID = "btnSubmit";
        btnSubmit.Text = "Submit";
        btnSubmit.Click += new System.EventHandler(btnSubmit_click);
        tc1.Controls.Add(btnSubmit);
        tr.Cells.Add(tc);
        tr.Cells.Add(tc1);
        tbldynamic.Rows.Add(tr);
        pnlinfo.Controls.Add(tbldynamic);

        Table tbl = new Table();
        for (int i = 0; i < 5; i++)
        {
            TableCell tcell = new TableCell();
            TableRow trow = new TableRow();
            CheckBox _checkbox = new CheckBox();
            _checkbox.ID = "chkDynamicCheckBox" + i;
            _checkbox.Text = "Checkbox" + i;
            tcell.Controls.Add(_checkbox);
            trow.Cells.Add(tcell);
            tbl.Rows.Add(trow);
        }
        pnlinfo.Controls.Add(tbl);
    }
    protected void btnSubmit_click(object sender, EventArgs e)
    {
        TextBox txtUserName = (TextBox)pnlinfo.FindControl("txtName");
        TextBox txtEmail = (TextBox)pnlinfo.FindControl("txtEmail");
        Response.Write("UserName: " + txtUserName.Text + "; " + "Email: " + txtEmail.Text);
    }


build application and run in browser, finally you will get respected output

Sending Messages to mobile in asp.net

sending messages to mobile by using way2sms credentials.

Code in message.aspx source 


<body>
    <form id="form1" runat="server">
    <div>
        <center>
        <asp:Panel ID="pnldetails" runat="server" BackColor="#FFCCFF" Height="300px" 
                Width="520px">
            <table class="style1">
                <tr>
                    <td colspan="2">
                        <asp:Label ID="lblstatement" runat="server" Font-Size="Large" Font-Bold="true" Text="Message Sending...."
                            ForeColor="Blue"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td class="style2">
                        <asp:Label ID="lbluserid" runat="server" Font-Bold="true" Text="UserId:"></asp:Label>
                    </td>
                    <td class="style3">
                        <asp:TextBox ID="txtuserid" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="style2">
                        <asp:Label ID="lblpassword" runat="server" Text="PassWord:" Font-Bold="true"></asp:Label>
                    </td>
                    <td class="style3">
                        <asp:TextBox ID="txtpassword" runat="server" TextMode="Password"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="style2">
                        <asp:Label ID="lblmobileno" runat="server" Text="MobileNumber:" Font-Bold="true"></asp:Label>
                    </td>
                    <td class="style3">
                        <asp:TextBox ID="txtmobileno" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="style2">
                        <asp:Label ID="lblmessage" runat="server" Text="Message:" Font-Bold="true"></asp:Label>
                    </td>
                    <td class="style3">
                            <asp:TextBox ID="txtmessage" runat="server" TextMode="MultiLine" onkeyup="Javascript:CharactersCount();"
                            Style="overflow: hidden;" Height="80px" Width="300px" MaxLength="160"></asp:TextBox>
                            <div style="float: right;">
                            <span style="font-family: Verdana; font-size: 12px; color:Blue;">Left:</span>
                            <asp:Label ID="lblChar" runat="server" Text="160" ForeColor="Red"></asp:Label>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td class="style2">
                        <asp:Button ID="btnsend" runat="server" ForeColor="Blue" Font-Bold="true" Text="Send"
                            OnClick="btnsend_Click" OnClientClick="return validate()" />
                    </td>
                    <td class="style3">
                        &nbsp;
                    </td>
                </tr>
            </table>
            </asp:Panel>
        </center>
    </div>
    </form>
</body>

code in message.aspx.cs file

before writing code in message.cs file we should write the namespaces

using System.Net;
using System.IO;



protected void btnsend_Click(object sender, EventArgs e)
    {

        if (txtuserid.Text != null & txtpassword.Text != null & txtmobileno.Text != null & txtmessage.Text != null)
        {

            sending(txtuserid.Text, txtpassword.Text, txtmobileno.Text, txtmessage.Text);
            Response.Write("'<script>alert('Message sent Successfully.....')</script>'");
        }
    }

    public void sending(string Uid, string Password, string Number, string Message)
    {
        btnsend.Attributes.Add("onclick", "return validate()");
        // Create a request using a URL that can receive a post.
        HttpWebRequest myReq =(HttpWebRequest)WebRequest.Create("http://ubaid.tk/sms/sms.aspx?uid=" + Uid + "&pwd=" + Password +
        "&phone=" + Number + "&msg=" + Message + "&provider=way2sms");
        // Get the response.
        HttpWebResponse myResp = (HttpWebResponse)myReq.GetResponse();
        System.IO.StreamReader respStreamReader = new System.IO.StreamReader(myResp.GetResponseStream());
        string responseString = respStreamReader.ReadToEnd();
        // Close the Stream object.
        respStreamReader.Close();
        myResp.Close();
        txtuserid.Text = string.Empty;
        txtpassword.Text = string.Empty;
        txtmobileno.Text = string.Empty;
        txtmessage.Text = string.Empty;
    }



i have written validations for controls in javascript, write this code in message.aspx source file,with in the head tag.


<script language="javascript" type="text/javascript">
        function validate() {
            if (document.getElementById("<%=txtuserid.ClientID%>").value == "") {
                alert("Should be entered UserId");
                document.getElementById("<%=txtuserid.ClientID%>").focus();
                return false;
            }
            if (document.getElementById("<%=txtpassword.ClientID%>").value == "") {
                alert("Enter your Password");
                document.getElementById("<%=txtpassword.ClientID%>").focus();
                return false;
            }
            if (document.getElementById("<%=txtmobileno.ClientID%>").value == "") {
                alert("Enter Mobile number");
                document.getElementById("<%=txtmobileno.ClientID%>").focus();
                return false;
            }
            if (document.getElementById("<%=txtmessage.ClientID%>").value == "") {
                alert("Enter Message Body");
                document.getElementById("<%=txtmessage.ClientID%>").focus();
                return false;
            }
            return true;
        }

        function CharactersCount() {
            var CharLength = '<%=txtmessage.MaxLength %>';
            var txtMsg = document.getElementById('txtmessage');
            var lblCount = document.getElementById('lblChar');
            if (txtMsg.value.length > CharLength) {
                txtMsg.value = txtMsg.value.substring(0, CharLength);
            }
            lblCount.innerHTML = CharLength - txtMsg.value.length;

        }
    </script>