In this article I will explain with an example, how to insert (save) Hindi, Marathi, Gujarati, Urdu, Persian, Arabic, Tamil and Telugu languages Text characters in
SQL Server database in
ASP.Net Core (.Net Core 8)
MVC.
Database
I have made use of the following table Languages with the schema as follow.
Content column is specified with NVARCHAR data type for saving Hindi, Marathi, Gujarati, Urdu, Persian, Arabic, Tamil and Telugu languages Text characters.
Note: You can download the database table SQL by clicking the download link below.
Installing Newtonsoft package using Nuget
Model
The Model class consists of following properties.
public class LanguageModel
{
public string Language { get; set; }
public string Content { get; set; }
}
Namespaces
You will need to import the following namespaces.
using Newtonsoft.Json;
using System.Data.SqlClient;
Controller
The Controller consists of following Action methods.
Action method for handling GET operation
Inside this Action method, simply the View is returned.
Action method for handling Post operation
Inside this Action method, the
JSON string is received from the View.
The
JSON string is deserialized into Generic List of
LanguageModel class objects.
Then loop I executed over the Generic List of
LanguageModel class objects and one by one it is inserted into the
SQL Server database table.
Finally, the Generic List of LanguageModel class objects is returned to the View.
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Index(string languageJSON)
{
List<LanguageModel> languages = JsonConvert.DeserializeObject<List<LanguageModel>>(languageJSON);
string constr = @"Data Source= .\SQL2019;Initial Catalog=AjaxSamples;uid=sa;pwd=pass@123";
string query = "INSERT INTO LanguagesVALUES(@Language, @Content)";
using (SqlConnection con = new SqlConnection(constr))
{
con.Open();
foreach (LanguageModel language in languages)
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.Parameters.AddWithValue("@Language", language.Language);
cmd.Parameters.AddWithValue("@Content", language.Content);
cmd.ExecuteNonQuery();
}
}
con.Close();
}
return View(languages);
}
}
View
HTML Markup
The View consists of an
HTML Table, a Form with an
HiddenField and a
Button.
The
HTML Table is created with some rows containing text in different languages.
Inside the View, following
jQuery file is inherited.
1. jquery.min.js
JavaScript Implementation
Inside the Save
JavaScript function, first a
JavaScript Array is created. Then the
HTML Table is referenced and then a for loop is executed over its rows.
Inside the for loop, a
JSON object is created for storing the Language and Content values.
Note: It is very important that the properties of the Language
JSON object and the
LanguageModel must be exact same.
The Language
JSON object is then pushed into the Languages
JavaScript Array.
Finally, the
JSON object is serialized into a string and saved into a
Hidden Field and the Form is submitted.
@addTagHelper*,Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<table id="tblLanguages" cellpadding="0" cellspacing="0">
<tr>
<th>Language</th>
<th>Content</th>
</tr>
<tr>
<td>English</td>
<td>World is beautiful.</td>
</tr>
<tr>
<td>Hindi</td>
<td>दुनिया सुंदर है।</td>
</tr>
<tr>
<td>Marathi</td>
<td>जग सुंदर आहे.</td>
</tr>
<tr>
<td>Gujarati</td>
<td>વિશ્વ સુંદર છે.</td>
</tr>
<tr>
<td>Urdu</td>
<td>دنیا خوبصورت ہے.</td>
</tr>
<tr>
<td>Persian</td>
<td>جهانی زیبااست.</td>
</tr>
<tr>
<td>Arabic</td>
<td>العالم هوجميل.</td>
</tr>
<tr>
<td>Tamil</td>
<td>உலக அழகாக இருக்கிறது.</td>
</tr>
<tr>
<td>Telugu</td>
<td>ప్రపంచ అందమైన ఉంది.</td>
</tr>
</table>
<hr />
<form method="post" asp-controller="Home" asp-action="Index">
<input type="hidden" name="languageJSON" value="" />
<input type="button" value="Save" onclick="Save()" />
</form>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script type="text/javascript">
function Save(){
var languages = [];
var tblLanguages = $("#tblLanguages");
for(var i = 1; i < tblLanguages[0].rows.length; i++){
var row = tblLanguages[0].rows[i];
var language = {};
language.Language = row.cells[0].innerText;
language.Content = row.cells[1].innerText;
languages.push(language);
}
$("[name=languageJSON]").val(JSON.stringify(languages));
document.forms[0].submit();
}
</script>
</body>
</html>
Screenshot
The GridView
The inserted records in the database table
Downloads