Tuesday, 16 October 2012




Block the goback button of browser in c#



    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        string sb;
        sb = "<script language=javascript>\n";
        sb += "window.history.forward(1);\n";
        sb += "\n</script>";
        Page.RegisterClientScriptBlock("clientScript", sb);
    }




Validation of Check box List


protected void Button1_Click(object sender, EventArgs e)

    {
        int i = 0;
        if (CheckBoxList1.Items[0].Selected){ i++; }
        if (CheckBoxList1.Items[1].Selected){ i++; }
        if (CheckBoxList1.Items[2].Selected){ i++; }
        if (CheckBoxList1.Items[3].Selected){ i++; }
        if (CheckBoxList1.Items[4].Selected){ i++; }


        if (i > 1)
        {
            Response.Redirect("abc.aspx");
            
        }
        else
        {
            Response.Write("check minimum 2 checkboxes........");
        }
    }

Checking Extension While Uploading



if (FileUpload1.FileName != "")
        {
            string ext = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
            if (!(ext == ".jpg" || ext == ".png" || ext == ".gif"))
            {
                lblMessage.Text = "Sorry! Image can be JPG,PNG,GIF only";
                return;
            }

            if (FileUpload1.FileBytes.Length > 150000)
            {
                lblMessage.Text = "Sorry! Image can be upto 150KB only";
                return;
            }
        }




Binding drop down List from the database


          SqlCommand cmd = new SqlCommand("SELECT date FROM date", Class1.Getconnect());
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        sda.Fill(ds);
        ddlDATE.DataSource = ds;
        ddlDATE.DataTextField = "date";                          
        ddlDATE.DataBind();


Select Value from the Database


cn.Open();
cmd = new SqlCommand("Select * from Student_Recode where EmailID='" + hdnEmail.Value + "'", cn);
dr = cmd.ExecuteReader();
if (dr.HasRows == true)
{
       dr.Read();
       lblname.Text = dr["AluminiName"].ToString()
}
cn.Close();

Allow Numeric


Java Script Validation allow Only Alpha Numeric


<script type="text/javascript">
        $(function () {
            $('input.alpha[$id=tb1]').keyup(function () {
                if (this.value.match(/[^a-zA-Z0-9 ]/g)) {
                    this.value = this.value.replace(/[^a-zA-Z0-9 ]/g, '');
                }
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div class="bigDiv">
        <h2>Allow Only Alphanumeric Characters in a TextBox</h2>
        <asp:TextBox ID="tb1" runat="server" class="alpha"
            Text="Try entering Non alphanumeric char"
            ToolTip="Try entering Non alphanumeric char"/>
         <br /><br />
         Tip: Examples of some non-alphanumeric characters <br />
         are ~!@~#$%^&* and so on.
    </div>
    </form>
</body>

Sunday, 14 October 2012

Matching login Id & Password 



if (cn.State == ConnectionState.Open)
        {
            cn.Close();
        }
        cn.Open();
        cmd = new SqlCommand("SELECT * FROM Student_Recode sr,User_login ul where sr.EmailID='"+ txtemail.Text +"' and ul.Password='"+ txtpass.Text.Trim() +"' and  sr.S_No=ul.S_N and ul.login_Id=sr.EmailID ", cn);
        dr = cmd.ExecuteReader();
        if (dr.HasRows == true)
        {
            Response.Redirect("userinfo.aspx");
            return;
        }
        else
        {
            lblerror.Text = "Sorry User ID And Password Not Corect";
            return;
        }



Saturday, 13 October 2012

Get data from SQL Server database into label


Label2.Text = cmd.ExecuteScalar().ToString() + " " + "Comments";