Hi Firuz,
HTML
<div>
<asp:Button ID="btnImport" runat="server" Text="Import" OnClick="Import" />
<br />
<br />
<asp:GridView ID="gvQuestions" runat="server" AutoGenerateColumns="true" OnRowDataBound="gvQuestions_RowDataBound" />
</div>
C#
protected void Import(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(constr);
Application word = new Application();
Document doc = new Document();
string filePath = Server.MapPath("~/Files/Test.docx");
object missing = System.Type.Missing;
object fileName = filePath;
doc = word.Documents.Open(ref fileName,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing);
String read = string.Empty;
List<string> data = new List<string>();
for (int i = 0; i < doc.Paragraphs.Count; i++)
{
string temp = doc.Paragraphs[i + 1].Range.Text.Trim();
if (temp != string.Empty)
data.Add(temp);
}
doc.Close(ref missing, ref missing, ref missing);
System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.AddRange(new System.Data.DataColumn[] {
new System.Data.DataColumn("Id",typeof(int)),
new System.Data.DataColumn("Question"),
new System.Data.DataColumn("Answer1"),
new System.Data.DataColumn("Answer2"),
new System.Data.DataColumn("Answer3"),
new System.Data.DataColumn("Answer4"),
new System.Data.DataColumn("CorrectAnswer")
});
int totalQuestionCount = 0;
int rowNo = 1;
System.Data.DataRow dr = dt.NewRow();
for (int i = 0; i < data.Count; i++)
{
string type = string.Empty;
if (data[i].Contains("$") || data[i].Contains("©"))
{
type = "option";
if (i != 0)
{
if ((!data[i - 1].Contains("@")))
{
type = "new line option";
}
}
}
else
{
type = "question";
}
int optionCount = 2;
switch (type)
{
case "question":
dr[0] = rowNo;
dr[1] = data[i].Replace("@", "").Trim();
rowNo++;
break;
case "option":
foreach (string option in data[i].Split(';'))
{
if (!string.IsNullOrEmpty(option))
{
if (option.Contains("$"))
{
dr[optionCount] = option.Replace("$", "").Trim();
}
else if (option.Contains("©"))
{
//* Option*//
dr[optionCount] = option.Replace("©", "").Trim();
dr[6] = option.Split(')')[1].Trim();
}
optionCount++;
}
}
break;
case "new line option":
foreach (string option in data[i].Split(';'))
{
if (!string.IsNullOrEmpty(option))
{
int colIndex = 0;
if (option.Contains("$A") || option.Contains("©A"))
{
colIndex = 2;
}
if (option.Contains("$B") || option.Contains("©B"))
{
colIndex = 3;
}
if (option.Contains("$C") || option.Contains("©C"))
{
colIndex = 4;
}
if (option.Contains("$D") || option.Contains("©D"))
{
colIndex = 5;
}
dt.Rows[dt.Rows.Count - 1][colIndex] = option.Replace("$", "").Trim();
if (option.Contains("©"))
{
dt.Rows[dt.Rows.Count - 1][colIndex] = option.Replace("©", "").Trim();
dt.Rows[dt.Rows.Count - 1][6] = option.Split(')')[1].Trim();
}
}
}
break;
}
if (i != 0 && data[i - 1].Contains("@"))
{
dt.Rows.Add(dr);
totalQuestionCount++;
dr = dt.NewRow();
}
}
((_Application)word).Quit();
foreach (DataRow row in dt.Rows)
{
SqlCommand cmd = new SqlCommand("INSERT INTO Encrypt VALUES(@Id,@Question,@Answer1,@Answer2,@Answer3,@Answer4,@CorrectAnswer)", con);
cmd.Parameters.AddWithValue("@Id", row["Id"]);
cmd.Parameters.AddWithValue("@Question", Encrypt(row["Question"].ToString()));
cmd.Parameters.AddWithValue("@Answer1", Encrypt(row["Answer1"].ToString()));
cmd.Parameters.AddWithValue("@Answer2", Encrypt(row["Answer2"].ToString()));
cmd.Parameters.AddWithValue("@Answer3", Encrypt(row["Answer3"].ToString()));
cmd.Parameters.AddWithValue("@Answer4", Encrypt(row["Answer4"].ToString()));
cmd.Parameters.AddWithValue("@CorrectAnswer", Encrypt(row["CorrectAnswer"].ToString()));
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
GetResult();
}
private string Encrypt(string clearText)
{
string EncryptionKey = "MAKV2SPBNI99212";
byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(clearBytes, 0, clearBytes.Length);
cs.Close();
}
clearText = Convert.ToBase64String(ms.ToArray());
}
}
return clearText;
}
private string Decrypt(string cipherText)
{
string EncryptionKey = "MAKV2SPBNI99212";
byte[] cipherBytes = Convert.FromBase64String(cipherText);
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(cipherBytes, 0, cipherBytes.Length);
cs.Close();
}
cipherText = Encoding.Unicode.GetString(ms.ToArray());
}
}
return cipherText;
}
private void GetResult()
{
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand("SELECT * FROM Encrypt", con);
DataSet getdt = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(getdt);
gvQuestions.DataSource = getdt;
gvQuestions.DataBind();
}
protected void gvQuestions_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[1].Text = Decrypt(e.Row.Cells[1].Text);
e.Row.Cells[2].Text = Decrypt(e.Row.Cells[2].Text);
e.Row.Cells[3].Text = Decrypt(e.Row.Cells[3].Text);
e.Row.Cells[4].Text = Decrypt(e.Row.Cells[4].Text);
e.Row.Cells[5].Text = Decrypt(e.Row.Cells[5].Text);
e.Row.Cells[6].Text = Decrypt(e.Row.Cells[6].Text);
}
}
Screenshot
I hope works for you.