Nick
Nick

Reputation: 886

Web Form to Oracle DB Insert

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.

Upvotes: 0

Views: 4820

Answers (1)

Mike Perrenoud
Mike Perrenoud

Reputation: 67898

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.

Upvotes: 2

Related Questions