In this article we are going to learn how to save and retrieve regional languages data from the database. We can use any language data in this example whether Hindi, Bangali, Urdu, French or Chinese.
Introduction
This article has been written as part of my attempt to provide solution to
this Forums post. In this post, author has asked to insert Telegu language data into the database and show on the page.
However as I am not much familiar with Telegu, I am going to write this article with Hindi language data.
Inserting regional language data to the database in ASP.NET
To demonstrate this example, let's create a sample application.
Database structure
Picture - 1
As we have to save the regional languages data to the database so our data type of the textual fields should be of nvarchar (in place of normal varchar), ntext (in place of normal text), nchar (in place of normal char) otherwise regional languages data may not get saved properly.
Watch this video that covers everything covered in this article.
ASPX Page
<div>
<fieldset>
<legend>Enter details</legend>
<p>
Title :
<asp:TextBox ID="txtTitle" runat="server" />
</p>
<p>
Description :
<asp:TextBox ID="txtDescription" runat="server" TextMode="MultiLine" Rows="10" Columns="50" />
</p>
<p>
<asp:Button ID="btnSubmit" runat="server" OnClick="SubmitData" Text="Submit" />
</p>
<asp:Label ID="lblMessage" runat="server" ForeColor="Green" EnableViewState="false" />
</fieldset>
<asp:GridView ID="GridView1" runat="server" EnableViewState="false" />
</div>
In the above code snippet, we have a TextBox with Title and a multi-line textbox Description. We have a button that executes SubmitData server side method. We also have a label control that is used to write the success message after inserting the data to the database.
My ASPX page looks something like below
Picture - 2
Code behind
string _connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
private void BindData()
{
DataTable table = new DataTable();
using (SqlConnection conn = new SqlConnection(_connStr))
{
using (SqlCommand cmd = new SqlCommand("Select * FROM GlobalLanguage", conn))
{
using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
{
ad.Fill(table);
}
}
}
GridView1.DataSource = table;
GridView1.DataBind();
}
protected void SubmitData(object sender, EventArgs e)
{
string title = txtTitle.Text.Trim();
string description = txtDescription.Text.Trim();
using (SqlConnection conn = new SqlConnection(_connStr))
{
string sql = "INSERT INTO GlobalLanguage (Title, Description) VALUES (@Title, @Description)";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
SqlParameter[] prms = new SqlParameter[2];
prms[0] = new SqlParameter("@Title", SqlDbType.NVarChar, 200);
prms[0].Value = title;
prms[1] = new SqlParameter("@Description", SqlDbType.NText);
prms[1].Value = description;
cmd.Parameters.AddRange(prms);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
lblMessage.Text = "Record inserted successfully !";
// rebind the data
this.BindData();
}
In the above code snippet, first we have a page level variable called
_connStr that stores the database connection string from the web.config file.
Page_Load
In the Page_Load event, we are checking for PostBack, if it is not then calling the
BindData method that actually uses ADO.NET code to connect to the database and fetch the records from the database table (in my case GlobalLanguage) and populates into the GridView.
SubmitData
In the SubmitData method, we are storing the data from both textboxes into title and description variables. Then we are using ADO.NET code to insert the data into the database. Notice the SqlParameter data type used. Here we are using SqlDbType.NVarChar and SqlDbType.NText for title and description as our database table data type if nvarchar and ntext respectively. Rest all codes of this method are as usual.
The saved data into the database table looks like below
Picture - 3
In order to type or enable regional language data into the textbox you may need to use some third party scripts. For example for Hindi you can use
HinKhoj.
Conclusion
As you can see in the above Picture -2 that I am able to insert the Hindi language data to the database and display on the page.