Friday, 30 January 2015

Generate automatic random numbers (for Employee Id)

To generate random number automatically for Employee Id/any other Id.
Following code can be used.


static int RandomNumber()
    {
        Random random = new Random();
 return random.Next(1000, 2000000);    //this show the random number between 1000 and 2000000
    }
    int m = RandomNumber();                                                // calling of RandomNumber() function

        txt_emp_name.Text = m.ToString();                           // Show random number in textbox



Summary
In this article I try to expose the Random Number generation. I hope after reading this article you will be able to use this trick in your code. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.

Tuesday, 20 January 2015

Login and Maintaining Session


Stored Proecedure:
Create proc [dbo].[usp_cust_login]
(
@mob_no nvarchar (15),
@password nvarchar (50)
)
as
      begin
if(select count(*) from tbl_cust_details where mob_no=@mob_no and password=@password)>0
      begin
      select name,mob_no , emailid from tbl_cust_details where mob_no=@mob_no and password=@password
      end
     
end

//Select name ,mob_no,emailid… is used for picking the values from table for maintaning session.

In Class file
#region cutomer login
  
    public DataSet customer_login(string mob_no, string password)
    {
      
        var con = new SqlConnection(connectionstring());
        con.Open();
        var cmd = new SqlCommand("usp_cust_login", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Clear();

        cmd.Parameters.AddWithValue("@mob_no", mob_no);
        cmd.Parameters.AddWithValue("@password", password);
        SqlDataAdapter dap = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        dap.Fill(ds);
       
        return ds;

    }
    #endregion

On .cs Page

protected void btn_login_Click(object sender, EventArgs e)
    {
        DataSet ds = cs1.customer_login(txt_mobile.Text.Trim(), txt_password.Text.Trim());
      
        if (ds.Tables[0].Rows.Count > 0)
        {
            Session["cust_mob"] = ds.Tables[0].Rows[0]["mob_no"].ToString();
            Session["cust_email"] = ds.Tables[0].Rows[0]["emailid"].ToString();
            Session["cust_name"] = ds.Tables[0].Rows[0]["name"].ToString();
            //Response.Redirect("~/User/Dashboard.aspx?id=" + Guid.NewGuid().ToString().Substring(0, 9));
            Response.Cookies["admin_name"].Expires = DateTime.Now.AddDays(-1);
            Response.Cookies["password"].Expires = DateTime.Now.AddDays(-1);

        }

        else
        {
            txt_password.Text = "";
            txt_mobile.Text = "";

            string msg = string.Empty;
            msg = "Incorrect mobile number or password";
            ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + msg + "');", true);
               
            return;


        }
    }






Summary
In this article I try to expose Login and Session maintenance. I hope after reading this article you will be able to use this trick in your code. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.

Thursday, 15 January 2015

Select only first row from database

If we want to select first row from database without using where clause.
Following code can be used.

select top 1 * from table_name

To select top 2 rows

select top 2 * from table_name



Summary

In this article I try to expose Database trick. I hope after reading this article you will be able to use this trick in your code. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.

Saturday, 10 January 2015

Blinking Text


Sometimes we want that our text should blink. For the purpose following can be used.

On .aspx page
protected void Timer1_Tick(object sender, EventArgs e)
    {
        if (DateTime.Now.Second % 2==0){
                                lbl_emp_name.Forecolor= Color.Red;
            lbl_emp_name.Text = "Click Here!";
        }
            else {
               lbl_emp_name.Text = "Employee Name";}
}


On .cs page(on the click of Timer1)

 <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
                   <Triggers>
                       <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
                   </Triggers>
                   <ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="5000" ontick="Timer1_Tick">
                             </asp:Timer>
<asp:Label ID="lbl_emp_name" runat="server">Employee Name  </asp:Label>
 </ContentTemplate>
</asp:UpdatePanel>





Following code will work in Mozilla Browser only

lbl_emp_name.ForeColor = Color.Red;

lbl_emp_name.Attributes.Add("style", "text-decoration:blink");




Summary
In this article I try to expose Blinking Text Property. I hope after reading this article you will be able to use this trick in your code. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.

Monday, 5 January 2015

Properties forComapare field validations

Properties of Required field validations for Dropdown list is bit different from textbox.
Following are the properties need to be set in Dropdown list and for Required Field Validator.

<asp:CompareValidator ID="CompareValidator2" runat="server"
ErrorMessage="Same as mobile"  Display="Dynamic" SetFocusOnError="True"
ForeColor="Red" ValidationGroup="sub"                                        ControlToValidate="txt_alt_no" ControlToCompare="txt_mob_no"                          Operator="NotEqual"></asp:CompareValidator>

Terms Used:
ErrorMessage:              Message to be displayed to show error.
SetFocusOnError:        So that cursor remains on the textbox which has error.
ValidationGroup:         It should be same as validation of button on whose click event action   on the textbox will be take.
ControlToValidate:      Name of Textbox on which validation is to be applied.                     
ForeColor:                   Color in which error is displayed.
ControlToCompare:      Name of Textbox from which it has to compare.

Operator:                     Compare operator(compairing for greater than /equivalent/smaller than).


Summary
In this article I try to expose the Compare Field Validation. I hope after reading this article you will be able to use this trick in your code. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.

Saturday, 3 January 2015

Required field validations for Dropdown list


Properties of Required field validations for Dropdown list is bit different from textbox.
Following are the properties need to be set in Dropdown list and for Required Field Validator.

<asp:DropDownList ID="ddl_city" class="form-control" runat="server"                        CausesValidation="False" ValidationGroup="sub">                                <asp:ListItem>-- Select City --</asp:ListItem>                              </asp:DropDownList>
 
<asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server"                     ErrorMessage="*" Display="Dynamic" SetFocusOnError="True" ValidationGroup="sub"  ControlToValidate="ddl_city" ForeColor="Red"                                       InitialValue="-- Select City --"></asp:RequiredFieldValidator>

Terms Used:
ErrorMessage:              Message to be displayed to show error.
SetFocusOnError:        So that cursor remains on the textbox which has error.
ValidationGroup:         It should be same as validation of button on whose click event action   on the textbox will be take.
ControlToValidate:      Name of Textbox on which validation is to be applied.                     
ForeColor:                   Color in which error is displayed.

InitialValue:                 Initial Value in the dropdown list(Select City in above case.)


Summary
In this article I try to expose the Require Field Validation for Dropdown list. I hope after reading this article you will be able to use this trick in your code. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.