0

How does one take user inputted values in an ASP.NET WebForm (Textboxes/Checkboxes/DDLs/etc.) and insert them into a database (Oracle, in my case, but not picky to which is explained).

<asp:TextBox id="textbox1" runat="server"></asp:TextBox> INSERT into db field "Name"
<asp:CheckBox id "checkbox1" runat="server"></asp:CheckBox> INSERT into db field "Gender"
<asp:DropDownList id "dropdownlist1" runat="server"></DropDownList> INSERT into db field "ComputerType"

Above are just sample controls to get an understanding of this topic.

1
  • 1
    @George Stocker - I attempted to improve this question to make it more of a real question, and also more likely helpful to any other users that stumble upon this QA looking for help. Commented Sep 9, 2013 at 18:17

1 Answer 1

2

This is pretty straight forward:

var sql = "INSERT INTO table ('Name', 'Gender', 'ComputerType') VALUES (@Name, @Gender, @ComputerType)";
using (OracleConnection c = new OracleConnection("{cstring}"))
{
    c.Open();
    using (OracleCommand cmd = new OracleCommand(sql, c))
    {
        cmd.Parameters.AddWithValue("@Name", textbox1.Text);
        cmd.Parameters.AddWithValue("@Gender", /* not sure how [checkbox1] maps here */);
        cmd.Parameters.AddWithValue("@ComputerType", dropdownlist1.SelectedValue);

        cmd.ExecuteNonQuery();
    }
}

the sql statement is parameterized, you open a connection and a new command with that connection, set the parameters based off the values in your controls, and execute the query.

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

2 Comments

@NickVielbig, not a problem, I'm glad I could be of assistance!
Now that I think about it. As it's an oracle DB, aren't the parameters supposed to be prefixed with : rather than @? (Also, I am not finding AddWithValue as an available from Parameters, but Add does it properly on its own).

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.