2011-10-19 51 views
0

我是新來的ASP。ASP按鈕onClick發送數據從文本框到數據庫

我想從asp:文本框插入數據到SQL Server數據庫。 我已經在處理數據庫連接和插入方法的APP_CODE文件夾中創建了一個類。

當button1被點擊時,我希望它從asp:文本框中獲取數據並使用APP_CODE中的questionnaireSQL.cs文件來更新數據庫。

我該怎麼辦?

questionnaireSQL.cs

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Data.SqlClient; 
using System.Data; 

namespace Devworks 
{ 

    public class OscarSQL 
    { 
     private string _productConnectionString; 
     private SqlConnection _productConn; 

     public OscarSQL() 
     { 
      _productConn = new SqlConnection(); 
      _productConnectionString += "data source=mssql.dev-works.co.uk; Initial Catalog=devworks_oscar;User ID=myusername;Password=mypassword"; 
      _productConn.ConnectionString = _productConnectionString; 
     } 

    // Insert New Questionnaire: 

     public int InsertQuestionnaire(string QuestName, int CustomerID, int NumberOfQuest) 
     { 
      int retVal = 0; 
      SqlCommand myCommand = new SqlCommand("NewQuestionnaire", _productConn); 
      myCommand.CommandType = CommandType.StoredProcedure; 
      myCommand.Parameters.Add(new SqlParameter("@QUESTNAME", SqlDbType.NVarChar)); 
      myCommand.Parameters.Add(new SqlParameter("@CUSTID", SqlDbType.Int)); 
      myCommand.Parameters.Add(new SqlParameter("@NUMQUEST", SqlDbType.Int)); 
      myCommand.Parameters.Add("@QUEST_ID", SqlDbType.Int, 0, "QUEST_ID"); 
      myCommand.Parameters["@QUEST_ID"].Direction = ParameterDirection.Output; 
      myCommand.Parameters[0].Value = QuestName; 
      myCommand.Parameters[1].Value = CustomerID; 
      myCommand.Parameters[2].Value = NumberOfQuest; 
      _productConn.Open(); 
      myCommand.ExecuteNonQuery(); 
      retVal = (int)myCommand.Parameters["@QUEST_ID"].Value; 
      _productConn.Close(); 
      return retVal; 
     } 


     // SQL DATAREADER, DATATABLE & NON QUERY UPDATE: 

     private void insert(SqlCommand myCommand) 
     { 
      _productConn.Open(); 
      myCommand.ExecuteNonQuery(); 
      _productConn.Close(); 
     } 

     private SqlDataReader getData(SqlCommand myCommand) 
     { 
      _productConn.Open(); 
      SqlDataReader myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection); 
      return myDataReader; 
     } 

     private DataTable createDataTable(SqlDataReader data) 
     { 
      DataTable retVal = new DataTable(); 
      retVal.Load(data); 
      return retVal; 
     } 
    } 
} 

newquestionnaire.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Main.Master" AutoEventWireup="true" CodeBehind="new_questionnaire.aspx.cs" Inherits="new_questionnaire" %> 
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"> 
</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"> 
    <div class="container"> 
     <div class="main"> 
      <h2 class="new">Add Your Questionnaire</h2> 
      <h3>Give your questionnaire a name.</h3> 
      <asp:TextBox ID="QuestName" runat="server"></asp:TextBox> 
      <h3>CustomerID</h3> 
      <asp:TextBox ID="CustomerID" runat="server"></asp:TextBox> 
      <h3>Number Of Questions</h3> 
      <asp:TextBox ID="NumberOfQuest" runat="server"></asp:TextBox> 
      <br /> 
      <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Create" /> 
     </div> <!-- end main --> 
    </div> <!-- end container --> 
</asp:Content> 

newquestionnaire.aspx.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data; 
using Devworks; 

    public partial class new_questionnaire : System.Web.UI.Page 
    { 


      protected void Page_Load(object sender, EventArgs e) 
      { 

      } 

      protected void Button1_Click(object sender, EventArgs e) 
      { 

      } 
     } 

在此先感謝

回答

2

你已經發布了所有這些代碼,但確切地說你不能做什麼? 一個人不得不懷疑你是否編寫了DAL(數據訪問層 - 你的db代碼)和Web UI,你怎麼不能寫一個諷刺地叫做Button1的按鈕的點擊事件?

您將文本框的值和點擊按鈕的值插入到數據庫中。 當你有一個大任務將它分成較小的任務,並且完成每一件直到你完成拼圖。

根據你的代碼

OscarSQL c = new OscarSQL(); //btw: not sure why you are not using a static class for this 
int result = c.InsertQuestionnaire(textBox.Text ...); //add all parameters here 

If (result > 0) 
    //good id 
else 
    //display error message 

將所有的button1_click事件代碼的背後代碼。

+0

謝謝。這有我想要的結果。 我的目標是使用.aspx頁面中的值並解析App_Code中.cs文件中的方法 – HGomez90

相關問題