Tuesday, 27 November 2012

Asp.Net(C#) inline coding Eval if statement

Use the tertiary expression as below,

<%#Eval("Present").ToString()=="True"?"P":"A"%></td>

Random Number Generator using Asp.net with C#



Using this class you can generate random numbers like passwords and identificators, but the main feature is that you can customise it to fit your needs. To do that you don't need to modify the code, it's enough to set some settings and you'll change the behavior of the generator.

Write the following classes in your C# Page,
int RandNo = 0;

private int RandomNumber(int min, int max)
        {
            Random random = new Random();
            return random.Next(min, max);
        }

Then call the following method where ever you require,
protected void Page_Load(object sender, EventArgs e)
        {
            RandNo = RandomNumber(10000000, 99999999);
            Response.Write(RandNo);
        }

Enjoy the coding...

Random String Generator using Asp.net with C#



Using this class you can generate random strings like passwords and identificators, but the main feature is that you can customise it to fit your needs. To do that you don't need to modify the code, it's enough to set some settings and you'll change the behavior of the generator.

Write the following classes in your C# Page,
private static Random random = new Random((int)DateTime.Now.Ticks);//thanks to McAden
private string RandomString(int size)
    {
        StringBuilder builder = new StringBuilder();
        char ch;
        for (int i = 0; i < size; i++)
        {
            ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));                
            builder.Append(ch);
        }

        return builder.ToString();
    }

Then call the following method where ever you require,
// get random string where everyou require
string Rand = RandomString(4);

Enjoy the coding...

Wednesday, 21 November 2012

Simple 3 tier architecture example in asp.net with C#

Introduction

Here I will explain about uses of 3-Tier architecture and how to create or implement 3-tier architecture for our project in asp.net 
Code for Business Entity Layer (BEL)
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace _3Tier
{
    public class Insert_BEL
    {
        public string UserName{get; set;}
        public string Password{get; set;}
     }
}

Code for Business Logic Layer (BLL)
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace _3Tier
{
    public class Insert_BLL
    {
        Insert_DAL insert_dal = new Insert_DAL();

        public string Insert(Insert_BEL insert_bel)
        {
            try
            {
                return insert_dal.Insert(insert_bel);
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }
    }
}


Code for Data Access Layer (DAL)
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;


namespace _3Tier
{
    public class Insert_DAL
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connstrname"].ConnectionString);

        public string Insert(Insert_BEL insert_bel)
        {
            string OutPut = "";
            try
            {
                SqlCommand cmd = new SqlCommand("Insert into Tbl_UserLoginDetails (UserName,Password) values (@UserName,@Password)", con);
                cmd.Parameters.AddWithValue("@UserName", insert_bel.UserName);
                cmd.Parameters.AddWithValue("@Password", insert_bel.Password);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                OutPut = "0";
            }
            catch (Exception ex)
            {
                OutPut = "1";
            }
            return OutPut;
        }
    }
}

 Code for Presentation Layer (UI Layer)
In .aspx 
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="_3Tier._Default" %>

<!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 runat="server">
    <title>Untitled Page</title>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
        .style2
        {
            width: 69px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <table class="style1">
            <tr>
                <td class="style2">
                    UserName:</td>
                <td>
                    <asp:TextBox ID="Txt_UserName" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Password:</td>
                <td>
                    <asp:TextBox ID="Txt_Password" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    &nbsp;</td>
                <td>
                    <asp:Label ID="Lbl_Status" runat="server" Text=""></asp:Label>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    &nbsp;</td>
                <td>
                    <asp:Button ID="Btn_Insert" runat="server" Text="Insert"
                        onclick="Btn_Insert_Click" />
                </td>
            </tr>
        </table>
   
    </div>
    </form>
</body>
</html>

In .aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace _3Tier
{
    public partial class _Default : System.Web.UI.Page
    {
        Insert_BEL insert_bel = new Insert_BEL();
        Insert_BLL insert_bll = new Insert_BLL();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Btn_Insert_Click(object sender, EventArgs e)
        {
            string OutPut = "";

            insert_bel.UserName = Txt_UserName.Text;
            insert_bel.Password = Txt_Password.Text;

            OutPut = insert_bll.Insert(insert_bel);

            if (OutPut == "0")
            {
                Lbl_Status.Text = "Data Inserted!";
            }
            else
            {
                Lbl_Status.Text = "Data NotInserted (Due to error)!";
            }
        }
    }
}
In web.config 
Remove or delete </connectionStrings>
Now Add below code
<connectionStrings>
    <add name="connectionname" connectionString="Data Source=servername;Initial Catalog=databasename;Integrated Security=True;" providerName="System.Data.SqlClient"/>
  </connectionStrings>

Try it now :-)
Please give your valuable feedback
Thank U!

Monday, 19 November 2012

Storing and Retrieving Non-English Unicode Characters (Hindi, Czech, Arabic etc.) in SQL Server

If you have a requirement to store and retrieve any other language characters in SQL Server besides English, you must do the following -
  1. Use a Unicode compatible data-type for the table column. NVACHAR, NCHAR, NTEXT are the data-types in SQL Server that can be used for storing non-English characters.
  2. Precede the Unicode data values with a N (capital letter) to let the SQL Server know that the following data is from Unicode character set. More details in this MSDN article. The N should be used even in the WHERE clause.
If the correct data-type is not used or the data is not preceded with a N, the table will save it as ‘?’ or other garbled character.
I have left out far-east languages like Japanese and Chinese from the following example on purpose because those languages have a few other considerations that I’ll save for another blog post.
I have used Google Translate to get the characters of other languages.
01DROP TABLE dbo.unicodeData
02GO
03CREATE TABLE dbo.unicodeData
04( languageUsed VARCHAR(50)
05, unicodeData NVARCHAR(200)
06, nonUnicodeData VARCHAR(200) -- same data in a normal VARCHAR column for comparison
07, comments VARCHAR(100)
08)
09GO
10INSERT INTO dbo.unicodeData (languageUsed, unicodeData, nonUnicodeData, comments)
11VALUES
12 ('English', N'This is an example', N'This is an example', NULL)
13, ('Hindi', N'यह एक उदाहरण है.', N'यह एक उदाहरण है.', 'Using the preceding N in both strings but VARCHAR is still a ?')
14, ('Hindi', 'यह एक उदाहरण है.', 'यह एक उदाहरण है.', 'Not using the preceding N in both strings so both are a ?')
15, ('Kannada', N'ಈ ಒಂದು ಉದಾಹರಣೆಯಾಗಿದೆ.', N'ಈ ಒಂದು ಉದಾಹರಣೆಯಾಗಿದೆ.', NULL)
16, ('Arabic', N'هذا مثال على ذلك.', N'هذا مثال على ذلك.', NULL)
17, ('Czech', N'To je příklad.', N'To je příklad.', NULL);
18GO
19SELECT * FROM dbo.unicodeData;
20GO
21-- Example of using N' in the WHERE clause
22SELECT * FROM dbo.unicodeData
23WHERE unicodeData like N'%एक%';
Unicode Results

Unicode Results
Further reading:
Reference: http://aalamrangi.wordpress.com/2012/05/13/storing-and-retrieving-non-english-unicode-characters-hindi-czech-arabic-etc-in-sql-server/

Asp.net Microsoft Certification Dump(Microsoft70-515_3-5-2012)