往数据库中插入数据时遇到的问题。 (100分)

  • 主题发起人 主题发起人 问题猫
  • 开始时间 开始时间

问题猫

Unregistered / Unconfirmed
GUEST, unregistred user!
需要向ACCESS 数据库中写入 ID,NAME,PASSWORD,三个字段的数据,
关键代码在金色的那一段:private void btnAccept_Click(object sender, System.EventArgs e)中。
为什么我输入数字时能够成功写入数据库,
而输入字符串时就失败了,
[red]错误提示:[/red]
[brown]至少一个参数没有被指定值。
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: 至少一个参数没有被指定值。
Source Error:

Line 76:
Line 77: booldo
redirect = true;
Line 78: cmd.ExecuteNonQuery();
[/brown]
[red]LOGIN。ASPX源码----------------如下[/red]
[blue]<%@ Import Namespace="System.Data.OleDb" %>
<%@ Page language="c#" Codebehind="login.aspx.cs" AutoEventWireup="false" Inherits="myBBS.login1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>login</title>
<link href="Style/nstyle.css" rel="stylesheet" type="text/css">
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="login" method="post" runat="server">
<P align="center"><FONT face="宋体">用户注册</FONT></P>
<P><FONT face="宋体"></FONT>&amp;nbsp;</P>
<DIV align="center">
<TABLE id="Table1" cellSpacing="1" cellPadding="1" width="508" border="0" align="center" style="WIDTH: 508px;
HEIGHT: 115px">
<TR>
<TD style="WIDTH: 95px"><FONT face="宋体">userID:</FONT></TD>
<TD>
<asp:TextBox id="txtID" runat="server" CssClass="TextBox"></asp:TextBox><FONT face="宋体">*</FONT>
<asp:RequiredFieldValidator id="reqID" runat="server" ErrorMessage="需要输入用户ID!" Display="None" ControlToValidate="txtID"></asp:RequiredFieldValidator></TD>
</TR>
<TR>
<TD style="WIDTH: 95px"><FONT face="宋体">userName:</FONT></TD>
<TD>
<asp:TextBox id="txtName" runat="server" CssClass="TextBox"></asp:TextBox><FONT face="宋体">*</FONT>
<asp:RequiredFieldValidator id="reqName" runat="server" ErrorMessage="需要输入用户名" Display="None" ControlToValidate="txtName"></asp:RequiredFieldValidator></TD>
</TR>
<TR>
<TD style="WIDTH: 95px"><FONT face="宋体">password:</FONT></TD>
<TD>
<asp:TextBox id="txtPwd" runat="server" CssClass="TextBox" TextMode="Password"></asp:TextBox><FONT face="宋体">*</FONT>
<asp:RequiredFieldValidator id="reqPwd" runat="server" ErrorMessage="需要输入密码!" Display="None" ControlToValidate="txtPwd"></asp:RequiredFieldValidator></TD>
</TR>
<TR>
<TD style="WIDTH: 95px"><FONT face="宋体">Rpassword:</FONT></TD>
<TD>
<asp:TextBox id="txtRPwd" runat="server" CssClass="TextBox" TextMode="Password"></asp:TextBox><FONT face="宋体">*</FONT>
<asp:CompareValidator id="compRPwd" runat="server" ErrorMessage="密码输入不相配,请重新输入!" Display="None" ControlToValidate="txtRPwd" ControlToCompare="txtPwd"></asp:CompareValidator></TD>
</TR>
<tr>
<td colSpan="2"><FONT face="宋体">
<P align="center">
<asp:Button id="btnAccept" runat="server" CssClass="Button" Text="Accept" Width="97px"></asp:Button></P>
</FONT>
</td>
</tr>
</TABLE>
</DIV>
<P align="center">
<asp:Label id="lblMessage" runat="server"></asp:Label></P>
<P align="center">
<asp:ValidationSummary id="valErrors" runat="server" CssClass="Normal"></asp:ValidationSummary></P>
</form>
</body>
</HTML>
[red]
LOGIN。ASPX。CS--------------------源码如下
[/red]using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Text;
namespace myBBS
{
/// <summary>
/// login1 的摘要说明。
/// </summary>
public class login1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox txtID;
protected System.Web.UI.WebControls.TextBox txtName;
protected System.Web.UI.WebControls.TextBox txtPwd;
protected System.Web.UI.WebControls.RequiredFieldValidator reqID;
protected System.Web.UI.WebControls.RequiredFieldValidator reqName;
protected System.Web.UI.WebControls.RequiredFieldValidator reqPwd;
protected System.Web.UI.WebControls.Label lblMessage;
protected System.Web.UI.WebControls.ValidationSummary valErrors;
protected System.Web.UI.WebControls.TextBox txtRPwd;
protected System.Web.UI.WebControls.Button btnAccept;
protected System.Web.UI.WebControls.CompareValidator compRPwd;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnAccept.Click += new System.EventHandler(this.btnAccept_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
[gold]private void btnAccept_Click(object sender, System.EventArgs e)
{
if(Page.IsValid)
{
//save user to the database
OleDbConnection con;
string sql;
OleDbCommand cmd;

//set query
sql = "INSERT INTO users (U_ID,U_name,pwd) VALUES("+txtID.Text+","+txtName.Text+","+txtPwd.Text+")";
//Connect and execute the query
con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;
Data Source="+ Server.MapPath("db1.mdb"));
cmd = new OleDbCommand(sql, con);
con.Open();
booldo
redirect = true;
try
{
cmd.ExecuteNonQuery();
}
catch
{
doredirect = false;
this.lblMessage.Visible=true;
this.lblMessage.Text="Insert couldn't be performed.User name may be already taken.";
}
finally
{
con.Close();
}
if(doredirect)
{
Response.Redirect("index.aspx");

}
else
{
lblMessage.Text = "Fix the following errors and retry:";
}
[/gold]

}
}
}
}[/blue]
 
可怜的猫。。。
 
[red]错误:
sql = "INSERT INTO users (U_ID,U_name,pwd) VALUES("+txtID.Text+","+txtName.Text+","+txtPwd.Text+")";[/red]
[blue]正确:
sql = "INSERT INTO users (U_ID,U_name,pwd) VALUES('"+txtID.Text+"','"+txtName.Text+"','"+txtPwd.Text+"')";[/blue]
 
接受答案了.
 
后退
顶部