在asp.net中将表单值插入数据库

时间:2014-01-14 07:05:47

标签: asp.net oracle

我正在尝试将表单字段valus插入到oracle数据库表中。我已经尝试了以下代码。但我得到的错误像“缺少表达”。请帮帮我。

Default.aspx代码:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" 
    CodeBehind="Default.aspx.cs" Inherits="Demo._Default" %>

<asp:Content runat="server" ID="FeaturedContent" ContentPlaceHolderID="FeaturedContent">
            </asp:Content>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">

        <table align="center" class="auto-style1">
        <tr>
            <td class="auto-style2">Activity Type</td>
            <td>
                <asp:DropDownList ID="ActivityTypeDropDown" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ActivityTypeDropDown_SelectedIndexChanged" >
                    <asp:ListItem Text="--Select" Value="" />
                    <asp:ListItem>Ticket</asp:ListItem>
                    <asp:ListItem>Non-Ticket</asp:ListItem>
                </asp:DropDownList>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="ActivityTypeDropDown" ErrorMessage="Please select Activity Type">*</asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td class="auto-style2">
                <asp:Label ID="TktnoLable" runat="server" Text="Ticket NO"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="TicketNoTextBox" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" ControlToValidate="TicketNoTextBox" ErrorMessage="Please enter the ticket number">*</asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td class="auto-style2">Ticket Category</td>
            <td>
                <asp:DropDownList ID="TicketCategoryTextBox" runat="server">
                    <asp:ListItem>--Select--</asp:ListItem>
                    <asp:ListItem>Adhoc</asp:ListItem>
                    <asp:ListItem>Batch Run</asp:ListItem>
                    <asp:ListItem>Bug Fix</asp:ListItem>
                    <asp:ListItem>CR</asp:ListItem>
                    <asp:ListItem>Enhancement</asp:ListItem>
                    <asp:ListItem>Issue</asp:ListItem>
                    <asp:ListItem>Others</asp:ListItem>
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td class="auto-style2">Ticket Description</td>
            <td>
                <asp:TextBox ID="TicketDescriptionTextBox" TextMode="MultiLine" runat="server" Width="200px"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" ControlToValidate="TicketDescriptionTextBox" ErrorMessage="Please provide ticket description">*</asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td class="auto-style7">Activity Description</td>
            <td class="auto-style8">
                <asp:TextBox ID="activityDescriptionTextBox" TextMode="MultiLine" runat="server" Width="200px"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator10" runat="server" ControlToValidate="activityDescriptionTextBox" ErrorMessage="Please provide activity description">*</asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td class="auto-style2">Module</td>
            <td>
                <asp:TextBox ID="ModuleTextBox" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="auto-style3">Priority</td>
            <td class="auto-style4">
                <asp:DropDownList ID="PriorityDropDown" runat="server">
                    <asp:ListItem Text="--Select--" Value="" />
                    <asp:ListItem>High</asp:ListItem>
                    <asp:ListItem>Medium</asp:ListItem>
                    <asp:ListItem>Low</asp:ListItem>
                </asp:DropDownList>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="PriorityDropDown" ErrorMessage="Please select priority">*</asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td class="auto-style2">Resource</td>
            <td>
                <asp:DropDownList ID="ResourceDropDown" runat="server">
                    <asp:ListItem text="--Select--" Value="" />
                    <asp:ListItem>Apporva</asp:ListItem>
                    <asp:ListItem>Arun</asp:ListItem>
                    <asp:ListItem>Harshal</asp:ListItem>
                    <asp:ListItem>Kiran</asp:ListItem>
                    <asp:ListItem>Nitin</asp:ListItem>
                    <asp:ListItem>Vagmita</asp:ListItem>
                </asp:DropDownList>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="ResourceDropDown" ErrorMessage="Please Select a resource">*</asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td class="auto-style2">Creation Date</td>
            <td>
                <asp:TextBox ID="CreationDateTextBox" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="CreationDateTextBox" ErrorMessage="Please provide creation date">*</asp:RequiredFieldValidator>
                <asp:CompareValidator ID="creationdatevalidator" runat="server" ControlToValidate="CreationDateTextBox" ErrorMessage="Please enter valid creation date" Operator="DataTypeCheck" Type="Date">*</asp:CompareValidator>
            </td>
        </tr>
        <tr>
            <td class="auto-style2">Assignment Date</td>
            <td>
                <asp:TextBox ID="AssignmentDateTextBox" runat="server"></asp:TextBox>
                <asp:CompareValidator ID="assignmentdatevalidator" runat="server" ControlToValidate="AssignmentDateTextBox" ErrorMessage="please enter valid assignment date" Operator="DataTypeCheck" Type="Date">*</asp:CompareValidator>
            </td>
        </tr>
        <tr>
            <td class="auto-style3">Status</td>
            <td class="auto-style4">
                <asp:DropDownList ID="StatusDropDown" runat="server" AutoPostBack="True" OnSelectedIndexChanged="StatusDropDown_SelectedIndexChanged">
                    <asp:ListItem Text="--select--" Value="" />
                    <asp:ListItem>WIP</asp:ListItem>
                    <asp:ListItem>Completed</asp:ListItem>
                </asp:DropDownList>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ControlToValidate="StatusDropDown" ErrorMessage="Please select status">*</asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td class="auto-style2">
                <asp:Label ID="CompletionDateLable" runat="server" Text="Completion Date"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="CompletionDateTextBox" runat="server"></asp:TextBox>
                <asp:CompareValidator ID="completiondatevalidator" runat="server" ErrorMessage="Please enter valid completion date" Operator="DataTypeCheck" Type="Date" ControlToValidate="CompletionDateTextBox">*</asp:CompareValidator>
            </td>
        </tr>
        <tr>
            <td class="auto-style5">Remarks</td>
            <td class="auto-style6">
                <asp:TextBox ID="RemarksTextBox" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="auto-style2">Efforts</td>
            <td>
                <asp:TextBox ID="EffortsTextBox" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="auto-style2">&nbsp;</td>
            <td>
                <asp:ValidationSummary ID="ValidationSummary1" runat="server" />
                <br />
                <asp:Button ID="SubmitButton" runat="server" OnClick="Button1_Click" Text="Submit" />
                <asp:Button ID="ResetButton" runat="server" OnClick="ResetButton_Click" Text="Reset" />
                <br />
                <asp:Label ID="Label1" runat="server"></asp:Label>
            </td>
        </tr>
    </table>

</asp:Content>
<asp:Content ID="Content1" runat="server" contentplaceholderid="HeadContent">
    <style type="text/css">
        .auto-style1 {
            width: 100%;
        }
        .auto-style2 {
            width: 119px;
        }
        .auto-style3 {
            width: 119px;
            height: 31px;
        }
        .auto-style4 {
            height: 31px;
        }
    .auto-style5 {
        width: 119px;
        height: 28px;
    }
    .auto-style6 {
        height: 28px;
    }
        .auto-style7 {
            width: 119px;
            height: 47px;
        }
        .auto-style8 {
            height: 47px;
        }
    </style>
</asp:Content>

default.aspx.cs代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Oracle.DataAccess;
using Oracle.DataAccess.Client;

namespace Demo
{
    public partial class _Default : Page
    {

        protected Boolean insert_details()
        {
            var Activity_type = ActivityTypeDropDown.SelectedValue;
            var Ticket_no = TicketNoTextBox.Text;
            var tkt_category = TicketCategoryTextBox.Text;
            var tkt_desc = TicketDescriptionTextBox.Text;
            var Act_desc = activityDescriptionTextBox.Text;
            var module = ModuleTextBox.Text;
            var priority = PriorityDropDown.SelectedValue;
            var resource = ResourceDropDown.SelectedValue;
            var creation_date = CreationDateTextBox.Text;
            var Assigned_date = AssignmentDateTextBox.Text;
            var status = StatusDropDown.SelectedValue;
            var completed_date = CompletionDateTextBox.Text;
            var remarks = RemarksTextBox.Text;
            var Efforts = EffortsTextBox;

            string oradb = "Data Source=OracleServerHost;Persist Security Info=True;User ID=scott;password=tiger";

            using (OracleConnection conn = new OracleConnection(oradb))
            {
                conn.Open();
                OracleCommand id_cmd = new OracleCommand();
                id_cmd.Connection=conn;
                id_cmd.CommandText="select NVL(Max(Activity_Id),0)+1 from Activity_Main_372640";
                var id=id_cmd.ExecuteNonQuery();
                OracleCommand cmd = new OracleCommand();
                cmd.Connection = conn;
                cmd.CommandText = "Insert into Activity_Main_372640(ACTIVITY_ID,ACTIVITY_TYPE,TICKET_ID,TICKET_CATEGORY,TICKET_DESCRIPTION,ACTIVITY_DESCRIPTION,MODULE,PRIORITY,RESOURCE_NAME,CREATION_DATE,ASSIGNMENT_DATE,COMPLITION_DATE,STATUS,REMARKS,EFFORTS) VALUES(@Parameter1,@Parameter2,@Parameter3,@Parameter4,@Parameter5,@Parameter6,@Parameter7,@Parameter8,@Parameter9,@Parameter10,@Parameter11,@Parameter12,@Parameter13,@Parameter14,@Parameter15)";
                cmd.Parameters.Add(new OracleParameter("@Parameter1",id));
                cmd.Parameters.Add(new OracleParameter("@Parameter2", Activity_type));
                cmd.Parameters.Add(new OracleParameter("@Parameter3", Ticket_no));
                cmd.Parameters.Add(new OracleParameter("@Parameter4", tkt_category));
                cmd.Parameters.Add(new OracleParameter("@Parameter5", tkt_desc));
                cmd.Parameters.Add(new OracleParameter("@Parameter6", Act_desc));
                cmd.Parameters.Add(new OracleParameter("@Parameter7", module));
                cmd.Parameters.Add(new OracleParameter("@Parameter8", priority));
                cmd.Parameters.Add(new OracleParameter("@Parameter9", resource));
                cmd.Parameters.Add(new OracleParameter("@Parameter10", creation_date));
                cmd.Parameters.Add(new OracleParameter("@Parameter11", Assigned_date));
                cmd.Parameters.Add(new OracleParameter("@Parameter12", completed_date));
                cmd.Parameters.Add(new OracleParameter("@Parameter13", status));
                cmd.Parameters.Add(new OracleParameter("@Parameter14", remarks));
                cmd.Parameters.Add(new OracleParameter("@Parameter15", Efforts));
                var rowsUpdated = cmd.ExecuteNonQuery();
                if (rowsUpdated > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void ActivityTypeDropDown_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ActivityTypeDropDown.SelectedValue == "Ticket")
            {
                TktnoLable.Visible = true;
                TicketNoTextBox.Visible = true;
            }
            else
            {
                TktnoLable.Visible = false;
                TicketNoTextBox.Visible = false;
            }
        }

        protected void ResetButton_Click(object sender, EventArgs e)
        {
            ClearInputs(Page.Controls);
        }
        void ClearInputs(ControlCollection ctrls)
        {
            foreach (Control ctrl in ctrls)
            {
                if (ctrl is TextBox)
                    ((TextBox)ctrl).Text = string.Empty;
                ClearInputs(ctrl.Controls);
            }
        }

        protected void StatusDropDown_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (StatusDropDown.SelectedValue == "Completed")
            {
                CompletionDateLable.Visible = true;
                CompletionDateTextBox.Visible = true;
            }
            else
            {
                CompletionDateLable.Visible = false;
                CompletionDateTextBox.Visible = false;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            insert_details();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

对于Oracle,绑定变量将以查询中的colon (:)开头,并且在设置时,您只需提及不带冒号的绑定变量名称。

            cmd.CommandText = "Insert into Activity_Main_372640(ACTIVITY_ID,ACTIVITY_TYPE,TICKET_ID,TICKET_CATEGORY,TICKET_DESCRIPTION,ACTIVITY_DESCRIPTION,MODULE,PRIORITY,RESOURCE_NAME,CREATION_DATE,ASSIGNMENT_DATE,COMPLITION_DATE,STATUS,REMARKS,EFFORTS) VALUES(:Parameter1,:Parameter2,:Parameter3,:Parameter4,:Parameter5,:Parameter6,:Parameter7,:Parameter8,:Parameter9,:Parameter10,:Parameter11,:Parameter12,:Parameter13,:Parameter14,:Parameter15)";
            cmd.Parameters.Add(new OracleParameter("Parameter1",id));
            cmd.Parameters.Add(new OracleParameter("Parameter2", Activity_type));
            cmd.Parameters.Add(new OracleParameter("Parameter3", Ticket_no));
            cmd.Parameters.Add(new OracleParameter("Parameter4", tkt_category));
            cmd.Parameters.Add(new OracleParameter("Parameter5", tkt_desc));
            cmd.Parameters.Add(new OracleParameter("Parameter6", Act_desc));
            cmd.Parameters.Add(new OracleParameter("Parameter7", module));
            cmd.Parameters.Add(new OracleParameter("Parameter8", priority));
            cmd.Parameters.Add(new OracleParameter("Parameter9", resource));
            cmd.Parameters.Add(new OracleParameter("Parameter10", creation_date));
            cmd.Parameters.Add(new OracleParameter("Parameter11", Assigned_date));
            cmd.Parameters.Add(new OracleParameter("Parameter12", completed_date));
            cmd.Parameters.Add(new OracleParameter("Parameter13", status));
            cmd.Parameters.Add(new OracleParameter("Parameter14", remarks));
            cmd.Parameters.Add(new OracleParameter("Parameter15", Efforts));
            var rowsUpdated = cmd.ExecuteNonQuery();