3

How do I add data into a database from an HTML table?

My table is like this:

html += "<table>";
html += "<tr><th>" + "A" + "</th><th>" + "B" + "</th><th>" + "C" + </th></tr>";
html += "<tr><td>" + "0" + "</td><td>" + "1" + "</td><td>" + "2" + </td></tr>";
html += "</table>";

I'm calling the HTML from the server side.

2
  • What do you mean in "I'm calling the HTML from the server side."? Are you creating this html from server side? Please provide more details Commented Jan 22, 2013 at 7:33
  • means I wrote the above code in the .cs page Commented Jan 22, 2013 at 9:05

2 Answers 2

1

if you like plain HTML, you can use a JavaScript framework like Knockout.js along with it. Knockout.js allows you to inject data into HTML using JavaScript View Models. A button click can be assigned to a JavaScript function in the View Model. The JavaScript function can do an AJAX post to call a controller action method on the server side - which will insert the data into the database.

For more information about knockout, check http://knockoutjs.com

Sign up to request clarification or add additional context in comments.

Comments

0

Use System.Text.RegularExpressions (Regex) to search pattern to replace the table tag:

replace <tr><th> and <tr><td> with blank,

replace </th></tr> and </td></tr> with ^^~~~~~~~~~~^^ indicates endline.

replace </td><td> with ||^^^^^^^^^|| indicates delimeters

string html = // your html table goes here
string[] lines = html.Split(new string[] { "^^~~~~~~~~~~^^" }, StringSplitOptions.None);
// now your html table is divided into lines, which means rows

// lines[0] = // the header
// lines[1] = // row 1
// lines[2] = // row 2
// lines[3] = // row 3
// ...
// ...

// line 1 is the header/column name
string[] columns = lines[0].Split(new string[] { "||^^^^^^^^^||" }, StringSplitOptions.None);

// columns[0] = // 1st column name
// columns[1] = // 2nd column name
// columns[2] = // 3rd column name
// ...
// ...

for (int i = 1; i < lines.Length; i++)
{
    string[] data = lines[i].Split(new string[] { "||^^^^^^^^^||" }, StringSplitOptions.None);
    // data[0] = // 1st data
    // data[1] = // 2nd data
    // data[2] = // 3rd data 
    // ...  
    // ...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.