Showing posts with label Asp.Net C# Validation. Show all posts
Showing posts with label Asp.Net C# Validation. Show all posts

Saturday, 4 November 2017

Disable TextBox And DropDownList From Code Behind

1. > For DropDownList
DropDownList1.Attributes.Add("disabled", "disabled");

2. > For TextBox
TextBox1.Attributes.Add("readonly","readonly");

3. > To Enable DropDownList Or TextBox
DropDownList1.Attributes.Remove("readonly");
TextBox1.Attributes.Remove("readonly");

Wednesday, 9 August 2017

Upload Data Through Excel Sheet in Asp.Net c#

1.>Put Excel Sheet in Root Directory Of Your Project

2.>Create New Page.
3.>C# Code

using System;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.IO;

public partial class UploadContactData : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ReadExcel();
        }
    }

    public void ReadExcel()
    {
        try
        {
            if (File.Exists(Server.MapPath("~/Import.xls")))
            {
                string connectionString = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Server.MapPath("~/Import.xls") + ";";
                connectionString += "Extended Properties=Excel 8.0;";
                OleDbConnection myConnection = new OleDbConnection(connectionString);

                DataSet ds = new DataSet();
                OleDbCommand myCommand = new OleDbCommand("Select * from [ContactCard$];", myConnection);//Sheet Name

                myConnection.Open();

                OleDbDataAdapter da = new OleDbDataAdapter(myCommand);
                da.Fill(ds);
                myConnection.Close();

                int count = 0;

                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        if (ds.Tables[0].Rows[i]["ReferenceNo"].ToString().Trim().Length > 0)
                        {
                            CommonFunction CF = new CommonFunction();
                            SqlCommand cmd = new SqlCommand("DataImport", CF.GetConnection());
                            cmd.Parameters.AddWithValue("@ReferenceNo", ds.Tables[0].Rows[i]["ReferenceNo"].ToString().Trim());
                            cmd.Parameters.AddWithValue("@ContactFullName", ds.Tables[0].Rows[i]["Name"].ToString().Trim());
                            cmd.Parameters.AddWithValue("@Address", ds.Tables[0].Rows[i]["Address"].ToString().Trim());                        
                            cmd.Parameters.AddWithValue("@IPAddress", CommonFunction.GetIPAddress());
                            cmd.Parameters.AddWithValue("@Browser", CommonFunction.GetWebBrowser());
                            cmd.Parameters.AddWithValue("@WebURL", CommonFunction.GetWebURL());
                            cmd.Parameters.AddWithValue("@CreatedBy", "00001");
                            cmd.Parameters.Add("@msg", SqlDbType.NVarChar, 10000);
                            cmd.Parameters["@msg"].Direction = ParameterDirection.Output;
                            cmd.CommandType = CommandType.StoredProcedure;
                            cmd.CommandTimeout = 1600;
                            cmd.ExecuteNonQuery();
                            CF.CloseConnection();
                            lblMsg.Text += Convert.ToString(cmd.Parameters["@msg"].Value);
                            if (!Convert.ToString(cmd.Parameters["@msg"].Value).Contains("Error"))
                            {
                                count++;
                            }
                        }
                    }
                   
                    lblError.Text += count.ToString() + " Record(s) Imported Successfully from " + ds.Tables[0].Rows.Count + " Record(s)"; ;
                }
                else
                {
                    lblError.Text += "File did not have any Record(s) for Import";
                }
            }
            else
            {
                lblError.Text += "File Does not exist on given path";
            }
        }
        catch (Exception ex)
        {
            lblError.Text = ex.Message.ToString();
        }
    }

}

Saturday, 8 July 2017

Compare dates in dd/mm/yyyy format using ASP.Net CompareValidator.


Compare validator in dd/mm/yyyy format we need to change the Culture property of the page to “en-GB” in the @Page directive of the asp.net Page. if you are using the Master page concept and if the compare validator are not worked for dd/mm/yyyy format then you can set the page culture in code behind page of the aspx page



<%@ Page Language="C#" AutoEventWireup="false"CodeFile="CompareValidator.aspx.cs"
    Inherits="CompareValidator" Culture = "en-GB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Compare Validator for dd/mm/yyyy format in asp.net using compare validator.
    </title>
</head>
<body>
    <form id="form1" runat="server">
    From Date:
    <asp:TextBox ID="txtFromDate" runat="server" Text="18/02/2015"></asp:TextBox>&nbsp;
    To Date:
    <asp:TextBox ID="txtToDate" runat="server" Text="19/12/2016"></asp:TextBox><br />
    <asp:CompareValidator ID="CompareValidator1" ValidationGroup="Date"ForeColor="Red"
        runat="server" ControlToValidate="txtFromDate"ControlToCompare="txtToDate" Operator="LessThan"
        Type="Date" ErrorMessage="Start date must be less than End date."></asp:CompareValidator>
    <br />
    <asp:Button ID="btnCompare" runat="server" Text="Compare"ValidationGroup="Date" />
    </form>
</body>
</html>

React Hooks - custom Hook

  v CustomHook Ø React allows us to create our own hook which is known as custom hook. Example – 1 localStorage Demo Step-1 Create ...