In this article I will explain how to insert (save) Hindi, Marathi, Gujarati, Urdu, Persian, Arabic, Tamil and Telugu languages Text characters in
MySql database using
ASP.Net, C# and VB.Net.
In
MySql database we will need to set
UTF-8 Collation for the Table to store the various languages text characters.
Using MySQL with ASP.Net
Database
For this article I have created a simple table with the following structure. Hindi, Marathi, Gujarati, Urdu, Persian, Arabic, Tamil and Telugu languages Text characters will be saved in the Content column which has a data type VARCHAR.
Note: You can download the database table SQL by clicking the download link below.
HTML Markup
The
HTML Markup consists of:
GridView – For displaying data.
The
GridView consists of two
BoundField columns.
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Language" HeaderText="Language" />
<asp:BoundField DataField="Content" HeaderText="Content" />
</Columns>
</asp:GridView>
<br />
<asp:Button Text="Save" runat="server" OnClick="Save" />
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Configuration;
using MySql.Data.MySqlClient;
VB.Net
Imports System.Data
Imports System.Configuration
Imports MySql.Data.MySqlClient
Binding the GridView
Inside the
Page_Load event handler, an object of
DataTable is created.
Then, three columns are added to the
DataTable Columns collection using the
AddRange method.
The DataColumn is specified which is hold the Language and Content.
Once the schema is ready i.e. all the columns are defined, some rows have been added using the Rows.Add method.
C#
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[2] {
new DataColumn("Language"),
new DataColumn("Content") });
dt.Rows.Add("English", "World is beautiful.");
dt.Rows.Add("Hindi", "दुनिया सुंदर है।");
dt.Rows.Add("Marathi", "जग सुंदर आहे.");
dt.Rows.Add("Gujarati", "વિશ્વ સુંદર છે.");
dt.Rows.Add("Urdu", "دنیا خوبصورت ہے.");
dt.Rows.Add("Persian", "جهانی زیبا است.");
dt.Rows.Add("Arabic", "العالم هو جميل.");
dt.Rows.Add("Tamil", "உலக அழகாக இருக்கிறது.");
dt.Rows.Add("Telugu", "ప్రపంచ అందమైన ఉంది.");
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
VB.Net
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[2] {
new DataColumn("Language"),
new DataColumn("Content") });
dt.Rows.Add("English", "World is beautiful.");
dt.Rows.Add("Hindi", "दुनिया सुंदर है।");
dt.Rows.Add("Marathi", "जग सुंदर आहे.");
dt.Rows.Add("Gujarati", "વિશ્વ સુંદર છે.");
dt.Rows.Add("Urdu", "دنیا خوبصورت ہے.");
dt.Rows.Add("Persian", "جهانی زیبا است.");
dt.Rows.Add("Arabic", "العالم هو جميل.");
dt.Rows.Add("Tamil", "உலக அழகாக இருக்கிறது.");
dt.Rows.Add("Telugu", "ప్రపంచ అందమైన ఉంది.");
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
Insert (save) Hindi, Marathi, Gujarati, Urdu, Persian, Arabic, Tamil and Telugu languages Text characters in MySql Database
When the
Save button is clicked the following event handler is executed inside which a loop is executed over the
GridView Rows and one by one each Language content is inserted into the
MySql database table.
C#
protected void Save(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(constr))
{
foreach (GridViewRow row in gvCustomers.Rows)
{
using (MySqlCommand cmd = new MySqlCommand("INSERT INTO Languages VALUES(@Language, @Content)", con))
{
cmd.Parameters.AddWithValue("@Language", row.Cells[0].Text);
cmd.Parameters.AddWithValue("@Content", row.Cells[1].Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
VB.Net
Protected Sub Save(sender As Object, e As EventArgs)
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New MySqlConnection(constr)
For Each row As GridViewRow In gvCustomers.Rows
Using cmd As New MySqlCommand("INSERT INTO Languages VALUES(@Language, @Content)", con)
cmd.Parameters.AddWithValue("@Language", row.Cells(0).Text)
cmd.Parameters.AddWithValue("@Content", row.Cells(1).Text)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
Next
End Using
End Sub
Screenshot
Downloads