사용자가 개인 정보를 제출하고 데이터가 Access 데이터베이스에 저장되는 작은 응용 프로그램을 만들려고합니다.ASP.Net에서 사용자 지정 데이터베이스에 연결
정확한 값을 테이블에 삽입하려고하므로 반환되는 오류가 발생하지 않습니다.
값은 first, last, age 및 gender입니다.
이것은 제출 버튼을 누르면 나타나는 오류 코드입니다.
도움 주셔서 감사합니다.
<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>
감사합니다. 그러나 작동하지 않습니다. – Geo
액세스 테이블에 값을 입력해야하는 열이있을 수 있습니다. 그들 중 어떤 것도 null이 아니게 설정되어 있습니까? 아니면 가치가 필요한 기본 키가 있습니까? 위의 "ID"필드는 언급했지만 기본 키는 'autonumber'열로 설정하지 않으면 기본값을 삽입하지 않습니다. – Bauhaus
ID가 내 INSERT를 엉망으로 만들고 있습니다. 도움을 주셔서 감사합니다 – Geo