Wednesday, October 23, 2019

How to Add Different Languages in Asp.net

Adding Different Languages in Asp.net

In this article , We will Learn about How languages are in websites in Asp.net


# Language Code for Different Languages

  1. English ------- en-us (us)
  2. French ------- fr-be (Belgium)
  3. Germen ------- de-at (Austria)

# How to find other languages with its code

Goto Internet Explorer > Tools > Internet Option > Languages > Add

# We have to make txt file for languages

*Number of languages = number of txt file

Base name=abc   culture name=en    region name=us
abc.en-us.txt
abc.fr-be.txt
abc.de-at.txt

# After making txt files, convert them to resource file

abc.en-us.resources
abc.fr-be.resources
abc.de-at.resources

# Resources file = compressed  (common bitmap, icon, images, common string)

# Open Visual Studio and Make new Project

*Website >  Add new Item > Text file > abc.en-us.txt
a= Welcome
b= Hello
c= English
d= Hi
key =a          value = Welcome

Key is used to access and it should be in English.

*Website >  Add new Item > Text file > abc.fr-be.txt
a= Bienvenu
b= Bonjour
c= French
d= salut

*Website >  Add new Item > Text file > abc.de-at.txt
a= Herzlich willkommen
b= Hallo
c= Germen
d= Hallo

*Open Developer command prompt for VS 2017 and run as admin


Resgen – utility to convert txt to resource file
*Add new webform > then drop downlist
DropdownList> Tasklist> Edit Item> add > Text= English > Value = en-us
add > Text= French > Value = fr-be
add > Text= Germen> Value = de-at
*Enable autopostback
*Add 4 Label


#Code File

using System.Globalization;
using System.Resources;
using System.Threading;
Globalization – culture specific classes
Resources – to read/write information from resource file
Threading – multi-threading concept

#Global Application Class

-         one project one file
-         to manage application and session level events
#Website > Add new Item > Global Application Class
<%@ Application Language="C#" %>
<%@ Import Namespace="System.Globalization"%>
<%@ Import Namespace="System.Resources"%>
<%@ Import Namespace="System.Threading" %>

void Application_Start(object sender, EventArgs e)
-         fire at first and one time only when project runs
void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        Application["rm"] = ResourceManager.CreateFileBasedResourceManager("abc", Server.MapPath("."), null);
    }
Rm= Global Variable (can be used in whole project)
. = current path
Null = current resource setting

# Make new method to fire the event everytime when request is send to server

void Application_BeginRequest(object sender, EventArgs e)
    {
        try
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo(Request.UserLanguages[0].ToString());
        }

        catch
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-us");
        }
        Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;

    }

Application_BeginRequest- Number of times is request send to server , the event should be fired same number of time
try, catch – Structure error handling (error handling)
on error goto – Unstructured error handling
try’s error will come in catch
Thread.CurrentThread- Current instance of the browser
CurrentCulture- to change the format not the culture
CultureInfo- it is a class of System.Globalization
CurrentUICulture- just to change the culture not the format
Format- it is date , time format and others etc

# Code File

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using System.Resources;
using System.Threading;

public partial class _Default : System.Web.UI.Page
{
    ResourceManager rm;
    protected void Page_Load(object sender, EventArgs e)
    {
        rm = (ResourceManager)(Application["rm"]);
        if (Page.IsPostBack==false)
        {
            Label1.Text = rm.GetString("a").ToString();
            Label2.Text = rm.GetString("b").ToString();
            Label3.Text = rm.GetString("c").ToString();
            Label4.Text = rm.GetString("d").ToString();
        }
    }

    private void Set_Cul(String d)
    {
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(d);
        Label1.Text = rm.GetString("a").ToString();
        Label2.Text = rm.GetString("b").ToString();
        Label3.Text = rm.GetString("c").ToString();
        Label4.Text = rm.GetString("d").ToString();
    }
}

String d- to pass culture code

# Design

*Double click DropDownList
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Set_Cul(DropDownList1.SelectedValue);
    }


Monday, October 21, 2019

How to Work with FormView Control in Asp.net


FormView Control In Asp.net


This Article explain the FormView Control and its use in Asp.net program



# FormView

-         It is similar to DetailView
-         In it we can display single record
-         No rows or Column (Customised view and all Feature details)
-         Paging is inbuilt

# Templates applied

-         Item Template
-         Header Template
-         Footer Template
-         Edit Item Template
-         Pager Template
-         Insert Item Template
-         Empty Data Template

# Open Visual Studio and Make new Project

*Add new webform
*Toolbox > Data > FormView

#Coding

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.Configuration;
using Microsoft.ApplicationBlocks.Data;


public partial class Default2 : System.Web.UI.Page
{
    String psg;
    protected void Page_Load(object sender, EventArgs e)
    {
        psg = ConfigurationManager.ConnectionStrings["ps"].ConnectionString;
        if (Page.IsPostBack == false)
        {
            Form_Bind();
        }
    }

    private void Form_Bind()
    {
        DataSet ds = SqlHelper.ExecuteDataset(psg, "dispemp");
        FormView1.DataSource = ds;
        FormView1.DataBind();
    }
}

# Design

*FormView> Tasklist> Edit Template

# Item template

Empno : Label > Edit Data Binding > Eval(“empno”)
Name : Label > Edit Data Binding > Eval(“ename”)
Address : Label > Edit Data Binding > Eval(“eadd”)
Salary : Label > Edit Data Binding > Eval(“esal”)
Add Link Button > Property > Text = Edit > Command Name= edit
Add Link Button > Property > Text = Delete> Command Name= delete
Add Link Button > Property > Text = New> Command Name= new

# Edit Item template

Empno : Label > Edit Data Binding > Eval(“empno”)
Name : TextBox > Edit Data Binding > Eval(“ename”)
Address : TextBox > Edit Data Binding > Eval(“eadd”)
Salary : TextBox > Edit Data Binding > Eval(“esal”)
Add Link Button > Property > Text = Update > Command Name= update
Add Link Button > Property > Text = Cancel> Command Name= cancel

# Insert Item template

Empno : TextBox
Name : TextBox
Address : TextBox
Salary : TextBox
Add Link Button > Property > Text = Insert > Command Name= insert
Add Link Button > Property > Text = Cancel> Command Name= cancel

# Empty Data Template

Add Button > Text = Add More Data > Command Name= new
#FormView > Properties> Allow paging= True

# FormView > Properties> Events > Page Index Changing

protected void FormView1_PageIndexChanging(object sender, FormViewPageEventArgs e)
    {
        FormView1.PageIndex = e.NewPageIndex;
        Form_Bind();
    }

 # Drawbacks of Inbuilt Paging

-         It will bind all the records in the table for example 200,000 but show one record
-         Low performance

# Mode Concept in FormView

In FormView there are 3 modes=
1.     Edit Mode= It will call Edit Item Template
2.     Insert Mode = It will call Insert Item Template
3.     Read Only Mode = It will call Item Template

# FormView > Properties> Events > Mode Changing

protected void FormView1_ModeChanging(object sender, FormViewModeEventArgs e)
    {
        FormView1.ChangeMode(e.NewMode);
        Form_Bind();
    }

# Save Code

* FormView > Properties> Events > Item Inserting
protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
    {
        Int32 eno, es;
        String en, ed;
        eno = Convert.ToInt32(((TextBox)(FormView1.FindControl("TextBox4"))).Text);
        en = ((TextBox)(FormView1.FindControl("TextBox5"))).Text;
        ed = ((TextBox)(FormView1.FindControl("TextBox6"))).Text;
        es = Convert.ToInt32(((TextBox)(FormView1.FindControl("TextBox7"))).Text);
        Object[] ar = new Object[4];
        ar[0] = eno;
        ar[1] = en;
        ar[2] = ed;
        ar[3] = es;
        SqlHelper.ExecuteNonQuery(psg, "insemp", ar);
        FormView1.ChangeMode(FormViewMode.ReadOnly);
        Form_Bind();
    }

# For Update

* FormView > Properties> Events > Item Updating
protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
    {
        Int32 eno, es;
        String en, ed;
        eno = Convert.ToInt32(((Label)(FormView1.FindControl("Label5"))).Text);
        en = ((TextBox)(FormView1.FindControl("TextBox1"))).Text;
        ed = ((TextBox)(FormView1.FindControl("TextBox2"))).Text;
        es = Convert.ToInt32(((TextBox)(FormView1.FindControl("TextBox3"))).Text);
        Object[] ar = new Object[4];
        ar[0] = eno;
        ar[1] = en;
        ar[2] = ed;
        ar[3] = es;
        SqlHelper.ExecuteNonQuery(psg, "updemp", ar);
        FormView1.ChangeMode(FormViewMode.ReadOnly);
        Form_Bind();
    }

# For Delete

* FormView > Properties> Events > Item Deleting
protected void FormView1_ItemDeleting(object sender, FormViewDeleteEventArgs e)
    {
        Int32 eno;
        eno = Convert.ToInt32(((Label)(FormView1.FindControl("Label1"))).Text);
        Object[] ar = new Object[1];
        ar[0] = eno;
        SqlHelper.ExecuteNonQuery(psg, "delemp", ar);
        Form_Bind();
    }