Sunday, July 29, 2012

Compare Dateime Difference in ASP.Net Example





 Design View:-

  <body>
    <form id="form1" runat="server">
    <div>
      <asp:Label ID="Label3" runat="server" Text="DataBase Date :"></asp:Label>
      <asp:Label ID="Label4" runat="server" ></asp:Label>
        <asp:Label ID="Label1" runat="server" Text="Select A Date :"></asp:Label>
     <asp:TextBox ID="TxtDate" runat="server"></asp:TextBox>
        <asp:ImageButton ID="ImaCalender" runat="server"
            ImageUrl="~/Images/Calender.jpg" Height="20px" Width="20px"
            onclick="ImaCalender_Click"/>
                <asp:Calendar ID="Calendar1"
                    runat="server" Visible="False" onselectionchanged="Calendar1_SelectionChanged"
                    ></asp:Calendar>
                <asp:Button ID="ButnComprasion" runat="server" Text="Compare"
            onclick="ButnComprasion_Click" /><br />
        <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>




  C# Code :-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class Projects_Others_DateDifferencet : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand cmd;
    SqlDataReader dr; 
    DateTime getdate;
    static DateTime  selectedDate;
    protected void Page_Load(object sender, EventArgs e)
    {      
            try
            {

                con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString);
                con.Open();
                cmd = new SqlCommand("select convert(varchar(20),date_of_signing,111) as date_of_signing from ufms.fms_companyInfo", con);
                dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    getdate = Convert.ToDateTime(dr["date_of_signing"].ToString());
                    Label4.Text = getdate.ToString("dd/MM/yyyy");
                }

                con.Close();
            }
            catch (Exception ex)
            {

            }
      
    }
    protected void ImaCalender_Click(object sender, ImageClickEventArgs e)
    {
        Calendar1.Visible = true;
    }
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        TxtDate.Text = Calendar1.SelectedDate.ToString("dd/MM/yyyy");
        selectedDate =Convert.ToDateTime( Calendar1.SelectedDate.ToString("MM/dd/yyyy"));
        Calendar1.Visible = false;
    }
    protected void ButnComprasion_Click(object sender, EventArgs e)
    {
        TimeSpan diff = getdate - selectedDate;
        if (diff.Days <= 0)
        {
            Label2.Text = "Please select valid date";
        }
        else
        {
            Label2.Text = "You have " + diff.Days + " days less from DataBase Date";
        }

    }
}

Wednesday, July 25, 2012

GridView data to DataTable in ASP.Net


   In my project I needed this work and I think it is useful in many cases.
 
    Step-1:
       we have to create a table equal to grigview column we want to get
      public DataTable CreateTable()
        {
            dt = new DataTable();
            dt.Columns.Add("Category", typeof(string));
            dt.Columns.Add("DINPANCINFCRN_No", typeof(string));
            dt.Columns.Add("PromotorsName", typeof(string));
            dt.Columns.Add("FatherOrHusbandName", typeof(string));
            ...........................................up to any column
            dt.Columns.Add("Email", typeof(string)); 
            return dt;

        } 

 
Step-2:     
     Then we have to assign value from Gridview to DataTable

      public DataTable InnerData()
        {
            CreateTable();
            DataRow dr;
            foreach (GridViewRow row in GvPrmoter.Rows)
            {
                dr = dt.NewRow();
                dr[0] = row.Cells[0].Text;
                dr[1] = row.Cells[1].Text;
                dr[2] = row.Cells[2].Text;
                dr[3] = row.Cells[3].Text;
               .....................
                dr[n] = row.Cells[n].Text;
                dt.Rows.Add(dr);
            }          
            return dt;
        }

Step-3:     
     Then we can call the  InnerData() method  inside any event
 

Monday, July 23, 2012

How to check all Checkbox inside a Gridview in Asp.net



Design View :-


                       <HeaderTemplate>
                            <asp:CheckBox ID="CheckAll" runat="server" AutoPostBack="True"    oncheckedchanged="CheckAll_CheckedChanged"   />
                            <asp:Label ID="LblApproved" runat="server" Text="Approved All" />
                        </HeaderTemplate>



                      <ItemTemplate>
                            <asp:CheckBox ID="ChkApprove" runat="server" Checked='<%# Eval("Approved") %>' />                          
                        </ItemTemplate>



C# Code :-


 protected void CheckAll_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chk = (CheckBox)GridView1.HeaderRow.FindControl("CheckAll");
            if (chk.Checked)
            {
                for (int i = 0; i < GridView1.Rows.Count; i++)
                {
                    CheckBox chkrow = (CheckBox)GridView1.Rows[i].FindControl("ChkApprove");
                    chkrow.Checked = true;
                }


            }
            else
            {
                for (int i = 0; i < GridViewBankReceipt.Rows.Count; i++)
                {
                    CheckBox chkrow = (CheckBox)GridView1t.Rows[i].FindControl("ChkApprove");
                    chkrow.Checked = false;
                }
            }
        }


Friday, July 20, 2012

How to pass contextKey value dynamically for AutoCompleteExtender in ASP.Net


 Generally we use AJAX AutoCompleteExtender for a TextBox to show data from database through Webservice. Here I faced a problem that to set contextKey  value dyanamically from '.CS' file without fix it in AutoCompleteExtender's  property  that is  ContextKey="value" in design view.
  
Nb : before using this we should have appropriate AjaxControlToolkit.dll for our .Net version.




 Design Code:-
 

<body>
    <form id="form2" runat="server">
    <asp:ToolkitScriptManager runat="server">
        <Services>
            <asp:ServiceReference Path="~/WebService/WebService.asmx" />
        </Services>
    </asp:ToolkitScriptManager>
    <div>
        <fieldset style="height: 200px; width: 365px;">
            <table class="style1">
                <tr>
                    <td style="text-align: right">
                        <asp:Label ID="Label18" runat="server" Text="Designation"></asp:Label>
                    </td>
                    <td>
                        <asp:DropDownList ID="DropDownList1" runat="server" Height="22px" Width="142px" AutoPostBack="True"
                            OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                            <asp:ListItem>Select</asp:ListItem>
                            <asp:ListItem>Software Engineer</asp:ListItem>
                            <asp:ListItem>Designer</asp:ListItem>
                        </asp:DropDownList>
                        &nbsp;
                    </td>
                </tr>
                <tr>
                    <td style="text-align: right">
                        <asp:Label ID="Label19" runat="server" Text="Employee"></asp:Label>
                    </td>
                    <td style="text-align: left">
                        <asp:TextBox ID="TxtEmployee" runat="server" autocomplete="off"></asp:TextBox>
                        <asp:AutoCompleteExtender ID="TxtEmployee_AutoCompleteExtender" runat="server" DelimiterCharacters=""
                            Enabled="True" ServicePath="~/WebService/WebService.asmx" ServiceMethod="GetEmployee"
                            CompletionInterval="100" MinimumPrefixLength="0" TargetControlID="TxtEmployee"
                            UseContextKey="true">
                        </asp:AutoCompleteExtender>
                    </td>
                </tr>
            </table>
        </fieldset>
    </div>
    </form>
</body>


  C#  Code:-




     private void SetContextKey()
        {

            AjaxControlToolkit.AutoCompleteExtender modal = (AjaxControlToolkit.AutoCompleteExtender)TxtEmployee.FindControl("TxtEmployee_AutoCompleteExtender");
            modal.ContextKey = DropDownList1.SelectedItem.ToString();// Any constant value
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetContextKey();
        }



  WebService Code :-




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
            
/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService] // This line should Uncomment
public class WebService : System.Web.Services.WebService {

    public WebService () {

        //Uncomment the following line if using designed components
        //InitializeComponent();
    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }

    [WebMethod]
    public string[] GetEmployee(string prefixText,int count,string contextKey)
    {
        string sql = "Select * from emp_demo Where EmpName like '"+ prefixText +"%' and Designation='"+ contextKey +"' ";
        SqlDataAdapter da = new SqlDataAdapter(sql, ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString);   
        DataTable dt = new DataTable();
        da.Fill(dt);

        string[] country= new string[dt.Rows.Count];
        int i = 0;
        foreach (DataRow dr in dt.Rows)
        {
            country.SetValue(dr["EmpName"].ToString(), i);
                i++;
        }
        return country;
    }
 
}