往数据库中插入数据时遇到的问题

laomao213 2003-12-12 09:48:06
需要向ACCESS 数据库中写入 ID,NAME,PASSWORD,三个字段的数据,
关键代码在金色的那一段:private void btnAccept_Click(object sender, System.EventArgs e)中。
为什么我输入数字时能够成功写入数据库,
而输入字符串时就失败了,
错误提示:
至少一个参数没有被指定值。
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: bool doredirect = true;
Line 78: cmd.ExecuteNonQuery();


LOGIN。ASPX。CS--------------------源码如下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

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();

bool doredirect = 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:";
}

}
}
}
}
...全文
100 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
laomao213 2003-12-12
  • 打赏
  • 举报
回复
多谢两位大哥指点
我的问题解决了
laomao213 2003-12-12
  • 打赏
  • 举报
回复
好感动啊!!!!!!
多谢 Programmersheaven(Finally) 老大的指点!!!
brightheroes 2003-12-12
  • 打赏
  • 举报
回复
如上所说
既然都是String类型的
那肯定是要加上'的

INSERT INTO users (U_ID,U_name,pwd) VALUES('"+txtID.Text+"','"+txtName.Text+"','"+txtPwd.Text+"')";
就没有问题了
laomao213 2003-12-12
  • 打赏
  • 举报
回复
都是以String 类型的
brightheroes 2003-12-12
  • 打赏
  • 举报
回复
你的这三个字段对应的数据类型是什么?

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧