2014-01-13 41 views
0

我很開心在asp.net中。我創建了一個表單。當我提交表單時,我需要將表單字段值插入到Oracle數據庫模式的表中。我正在使用Visual Studio 2012 Express。我已經將我的數據庫模式添加到數據庫瀏覽器。現在我需要一個代碼來從表單獲取數據並插入到表中。asp.net form submition into 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 /> 
       <br /> 
       <input id="SubmitButton" type="submit" value="Submit" /><asp:Button ID="ResetButton" runat="server" OnClick="ResetButton_Click" Text="Reset" /> 
       <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 void Page_Load(object sender, EventArgs e) 
     { 
      var Activity_type = ""; 
      var Ticket_no = ""; 
      var tkt_category = ""; 
      var tkt_desc = ""; 
      var Act_desc = ""; 
      var module = ""; 
      var priority = ""; 
      var resource = ""; 
      var creation_date = ""; 
      var Assigned_date = ""; 
      var status = ""; 
      var completed_date = ""; 
      var remarks = ""; 
      var Efforts = ""; 
      if (IsPostBack) 
      { 
       Activity_type = Request.Form["ActivityTypeDropDown"]; 
       Ticket_no = Request.Form["TicketNoTextBox"]; 
       tkt_category = Request.Form["TicketCategoryTextBox"]; 
       tkt_desc = Request.Form["TicketDescriptionTextBox"]; 
       Act_desc = Request.Form["activityDescriptionTextBox"]; 
       module = Request.Form["ModuleTextBox"]; 
       priority = Request.Form["PriorityDropDown"]; 
       resource = Request.Form["ResourceDropDown"]; 
       resource = Request.Form["ResourceDropDown"]; 
       creation_date = Request.Form["CreationDateTextBox"]; 
       Assigned_date = Request.Form["AssignmentDateTextBox"]; 
       status = Request.Form["StatusDropDown"]; 
       completed_date = Request.Form["CompletionDateTextBox"]; 
       remarks = Request.Form["RemarksTextBox"]; 
       Efforts = Request.Form["EffortsTextBox"]; 




      } 
     } 
     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; 
      } 
     } 
    } 
} 

請幫我填寫表格的操作。

+0

首先得到asp.net的正確知識,並嘗試實現你編碼 –

回答

2

這裏是插入數據的簡單方法,你可以將它修改爲您的需求

public void InsertToOracle(string value) 
     { 
      string oradb = "Data Source=ORCL;User Id=user;Password=password;"; 

      using (OracleConnection conn = new OracleConnection(oradb)) 
      { 
       conn.Open(); 
       OracleCommand cmd = new OracleCommand(); 
       cmd.Connection = conn; 
       cmd.CommandText = "Insert into Table1(ColumnName) VALUES(@ParameterName)"; 
       cmd.Parameters.Add(new OracleParameter("@ParameterName", value)); 
       var rowsUpdated = cmd.ExecuteNonQuery(); 

      } 
     } 

另外在asp.net web表單沒有使用需求的Request.Form

,而不是

tkt_category = Request.Form["TicketCategoryTextBox"]; 

使用

tkt_category = TicketCategoryTextBox.Text; 

代替

Activity_type = Request.Form["ActivityTypeDropDown"]; 

使用

Activity_type = ActivityTypeDropDown.SelectedValue; 

所有項目標有RUNAT =服務器已經準備好使用作爲一個對象變量