Click here to Skip to main content
15,901,666 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Can anyone show me the code to take a simple web form and insert it into a MS SQL db. I don't mind the language just a simple example, somthing like having a webpage with 2 text fields name, age and a button submit. Upon hitting the Submit button have the data be inserted into a MS SQL DB with fields Name and Age.
Posted

Have you tried Googling MSSQL and insertion? I did.

Code project article right here:
Beginners guide to accessing SQL Server through C#[^]
 
Share this answer
 
This is about as simple as it gets:
ASP.NET
<%@ Page Language="C#" %>

<html>
<head>
    <title>Web Form to DB</title>
</head>
<body>
    <form id="myForm" runat="server">
        Name:
        <asp:TextBox runat="server" ID="txtName" />
        <br />
        Age:
        <asp:TextBox runat="server" ID="txtAge" />
        <br />
        <asp:Button runat="server" onclick="HandleInsert" Text="Save" />
    </form>
</body>
</html>

<script runat="server">
    public void HandleInsert(object sender, EventArgs e)
    {
        string name = txtName.Text;
        int age = int.Parse(txtAge.Text);
        string strCommand = "INSERT INTO People (PersonName, PersonAge) VALUES (@PersonName, @PersonAge)";
        string strConnection = @"Data Source=MyMachineName\SQL2005;Initial Catalog=TestDB;Integrated Security=True";
        var conn = new System.Data.SqlClient.SqlConnection(strConnection);
        var cmd = new System.Data.SqlClient.SqlCommand(strCommand, conn);
        cmd.Parameters.AddWithValue("PersonName", name);
        cmd.Parameters.AddWithValue("PersonAge", age);
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
    }
</script>

You'll have to make sure the "People" table exists first. And modify the connection string to point to your database.
 
Share this answer
 
v2
Comments
AspDotNetDev 12-Jan-11 17:53pm    
Note that this doesn't follow best practices. This code is meant just to answer the question with a small code snippet.
AspDotNetDev 12-Jan-11 18:55pm    
I edited the answer to change the "null" to "conn".
RobertPlant23 12-Jan-11 19:59pm    
Thank you very much for you response, could explain how I can apply this code? I am using Notepad ++ and have saved it as a HTML. When I 'run' this code in FireFox it simply display the words: Name and Age. I apologize but this is my first stab at something like this.
AspDotNetDev 12-Jan-11 21:28pm    
That is ASP.Net code. I'm not going to describe how to program in ASP.Net. If you don't have that fundamental understanding, it's going to take a bit of work before you are able to run the code I gave you. Pick up a book on ASP.Net and start there. If you can, find one that focuses on C# rather than VB.Net (the code I gave you has some C#).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900