2012-03-03 54 views
1

我試圖創建一個用戶提交個人信息並將數據保存在Access數據庫中的小應用程序。連接到ASP.Net中的自定義數據庫

返回的錯誤沒有任何意義,因爲我試圖將正確數量的值插入表中。

這些值是第一個,最後一個,年齡和性別。

這是我的代碼,然後按我提交按鈕時得到的錯誤。

感謝您的幫助。

<pre> 
<%@ Page Language="C#" %> 
<%@ Import Namespace="System.Data" %> 
<%@ Import Namespace="System.Data.OleDb" %> 

<script runat="server"> 
public void btnSubmit_Click(object sender, EventArgs e) 
{ 
    // Create and configure connection string. 
    OleDbConnection c = new OleDbConnection(); 
    c.ConnectionString = 
     "Provider=Microsoft.ACE.OLEDB.12.0;" + 
     "Data Source=d:/ectserver/gnicolai/Database/application.accdb;"; 

    // Open connection. 
    c.Open(); 

    // Get data from textboxes. 
    string last = txtLastName.Text; 
    string first = txtFirstName.Text; 
    string gender = txtGender.Text; 
    int age = int.Parse(txtAge.Text); 

    // Compose SQL command string. 
    string sql = "INSERT INTO Applicant VALUES" + 
     "('" + last + "', '" + first + 
     "', '" + gender + "'," + age + ");"; 

    // Show SQL insert statement. 
    litMessage.Text = 
     "Data submitted with SQL query string<br />" + sql; 

    // Create command object and execute insert statement. 
    OleDbCommand command = new OleDbCommand(sql, c); 
    command.ExecuteNonQuery(); 

    // Close connection. 
    c.Close(); 
} 
</script> 

<html> 

<head> 
<title>grocery-display3.aspx Example</title> 
<link rel="stylesheet" class="text/css" 
         href="../examples.css" /> 
<style type="text/css"> 
td { padding-left:0.15cm; 
     padding-right:0.15cm; 
     padding-top:0.15cm; 
     padding-bottom:0.15cm; } 
</style> 
</head> 

<body> 

<h2>PersonInfo3 Example</h2> 

<p>Submit age, gender, and age for a person; 
    store this information in a database. 
    Retrieve this data with the ViewPersons page. </p> 

<form id="frmSelect" runat="server"> 

<table> 
<tr> 
    <td class="r">Last Name</td> 
    <td><asp:TextBox ID="txtFirstName" runat="server" 
      CssClass="ctrl" /></td> 
</tr> 
<tr> 
    <td class="r">First Name</td> 
    <td><asp:TextBox ID="txtLastName" runat="server" 
      CssClass="ctrl" /></td> 
</tr> 
<tr> 
    <td class="r">Gender</td> 
    <td><asp:TextBox ID="txtGender" runat="server" 
      CssClass="ctrl" /></td> 
</tr> 
<tr> 
    <td class="r">Age</td> 
    <td><asp:TextBox ID="txtAge" runat="server" 
      CssClass="ctrl" /></td>  
</tr> 
<tr> 
    <td> </td> 
    <td><asp:Button ID="btnSubmit" runat="server" 
      Text="Submit" CssClass="ctrl" Width="128px" 
      OnClick="btnSubmit_Click" /></td> 
</tr> 
</table> 

<p><asp:RangeValidator ID="RangeValidator1" Type="Integer" runat="server" 
     ControlToValidate="txtAge" Display="Static" MinimumValue="0" 
     MaximumValue="130" ErrorMessage="Age must be between 0 and 130" /></p> 

<p><asp:Literal ID="litMessage" runat="server" /></p> 
</form> 

</body> 
</html>   

</pre> 


<strong>Error</strong> 

<pre> 
Server Error in '/' Application. 

Number of query values and destination fields are not the same. 

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.OleDb.OleDbException: Number of query values and destination fields are not the same. 

Source Error: 


Line 32:  // Create command object and execute insert statement. 
Line 33:  OleDbCommand command = new OleDbCommand(sql, c); 
Line 34:  command.ExecuteNonQuery(); 
Line 35:   
Line 36:  // Close connection. 

Source File: d:\DePaul\Winter 2012\IT 330\Projects\Proj5-Nicolaides\Proj5-Nicolaides\Default.aspx Line: 34 

Stack Trace: 


[OleDbException (0x80004005): Number of query values and destination fields are not the same.] 
    System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr) +992124 
    System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) +255 
    System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) +188 
    System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) +58 
    System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) +161 
    System.Data.OleDb.OleDbCommand.ExecuteNonQuery() +113 
    ASP.default_aspx.btnSubmit_Click(Object sender, EventArgs e) in d:\DePaul\Winter 2012\IT 330\Projects\Proj5-Nicolaides\Proj5-Nicolaides\Default.aspx:34 
    System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111 
    System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110 
    System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 
    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 
    System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36 
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565 


</pre> 

回答

0

我在數據庫表上的ID字段與我的insert語句混淆。在設計視圖中查看錶格之後,我刪除了「ID」字段,並且我的插入語句按原樣工作。

1

您可能需要指定你想在你的表填充明確列。你想要插入的列的名稱是什麼?

試着改變你的代碼如下:

string sql = "INSERT INTO Applicant" + 
    "(LastName, FirstName, Gender, Age)" + 
    "VALUES" + 
    "('" + last + "', '" + first + 
    "', '" + gender + "'," + age + ");"; 

更換名字,與你等列名。

+0

謝謝,但這不起作用 – Geo 2012-03-03 06:28:22

+0

您的訪問表中可能有一些需要插入值的列。他們中的任何一個是否設置爲非空?還是有一個主鍵需要一個值?您在上面提到了「ID」字段,但主鍵不會插入默認值,除非您已將其設置爲「自動編號」列。 – Bauhaus 2012-03-03 06:37:40

+0

ID正在搞亂我的INSERT。感謝您的幫助 – Geo 2012-03-05 00:51:32

2

最可能的問題是您的表定義有4列以上。您需要爲額外的列提供默認插入值(如果您的數據庫支持它)或更改插入代碼以匹配。

還值得指出的是,此代碼受到sql注入攻擊。你永遠不應該建立一個像這樣的sql語句直接提供給數據庫。而是把它變成一個SQL參數,然後發送。

+0

我知道這會受到攻擊,但不會以這種方式發佈。把這看成是一個粗略的草案。我從來沒有做過這樣的事情,所以我只是對基礎進行建模。我的桌子上有一個「ID」列。這很重要嗎?我認爲主鍵會自動生成。 – Geo 2012-03-03 06:18:40

+0

只要把它作爲一個起點就夠了。確保您的Id字段設置爲自動編號。您可能還需要修改插入語句以包含其他人提到的字段名稱。 – toddles2000 2012-03-03 20:30:41

+0

我修好了。謝謝您的幫助 – Geo 2012-03-05 00:51:47