0

How can I, using asp.net and HTML, use a form to create a new table row in an SQL Table?

Would I use javascript to retrieve the HTML? Should I directly submit SQL or should I create a stored procedure? Essentially, I want to know how to get the data from a form to my SQL.

3 Answers 3

2

Take a look at the example provided in the documentation for the SqlCommand class.

In here they provide a basic example for connecting to a database, executing a query and processing the results. Here is a slightly modified version for doing an insert:

string queryString = 
    "INSERT INTO MyTable (Column1) Values ('test');";
using (SqlConnection connection = new SqlConnection(
           connectionString))
{
    SqlCommand command = new SqlCommand(
        queryString, connection);
    connection.Open();
    command.ExecuteNonQuery();
}

Make sure that you use a Parameterized Query when you use insert values from your web page into your database or you will be vulnerable to SQL Injection attacks.

There are several other methods for doing this such as LINQ-to-SQL, but feel this is the most straight forward for beginners.

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

2 Comments

Good answer, but I think it would be better if you included a sample connection string. Maybe for the sake of this example, you could dimension and set the connectionString variable?
How could I get the data from, say, an <input></input> into MyTable? I know SQL well enough, I just don't get the transition.
0

Check out this for adding data: http://www.w3schools.com/ado/ado_add.asp

And check out this for updating data: http://www.w3schools.com/ado/ado_update.asp

You should use stored procedures to prevent security risks.

Comments

0

I strongly suggest you read:

ASP.NET Data Access to understand the basics, and then part 2 of step by step mySQL data access using ASP.NET

2 Comments

"Essentially, I want to know how to get the data from a form to my SQL." - I guess I read it as "mySQL" Thanks for the downvote without a regard to possible misinterpretion and completely ignoring that I linked a basic data access guide on MSDN that the OP should read without blindly trying to show "data on html"
I guess I can se where you got it mixed up. I read the tags first, so it didn't confuse me, but I can see where you're coming from.

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.