我向access数据库插入数据时,向表中插入九个字段时没错误,但插入十个字段时就出错了,急求高手解决
using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Text;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class UC_Reg : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{ } protected void cmdReg_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
string con = "Provider=Microsoft.Jet.Oledb.4.0;Data Source="+Server.MapPath("App_Data\\DB_CustomReg.mdb");
StringBuilder sqltext = new StringBuilder("Insert into CustomReg (Name,TrueName,Sex,PWD,Email,Phone,Address,LikeSort,Usefor,Work) Values ('" + txtName.Text + "'");
string str = Request.Params["txttruename"];
if (str == "")
{
str = "没填写";
sqltext .Append (",'"+ str +"'"); }
else
{
sqltext.Append(",'" + str + "'");
}
string sex;
if (RadioButton1.Checked)
{
sex = RadioButton1.Text;
sqltext.Append(",'" + sex+ "'");
}
else if(RadioButton2.Checked )
{
sex = RadioButton2.Text;
sqltext.Append(",'" + sex+ "'");
}
else
{
sex="没有填写";
sqltext.Append(",'" + sex+ "'");
} sqltext.Append(",'" + txtpwd .Text + "'");
string email = Request.Params["txtemail"];
if (email == "")
{
email = "没有填写";
sqltext.Append(",'" + email + "'");
}
else
{
sqltext.Append(",'" + email + "'");
}
string phone = Request.Params["txtphone"];
if (phone == "")
{
phone = "没有填写";
sqltext.Append(",'" + phone + "'");
}
else
{
sqltext.Append(",'" + email + "'");
}
string address = Request.Params["txtadd"];
if (address == "")
{
address = "没有填写";
sqltext.Append(",'" + address + "'");
}
else
{
sqltext.Append(",'" + address + "'");
}
string sort = lstSort.SelectedValue;
string use = lstUse.SelectedValue;
string work = lstWork.SelectedValue;
sqltext.Append(",'"+ sort +"'");
sqltext.Append(",'"+ use +"'");
sqltext.Append(",'"+ work +"'"); sqltext.Append(")");
bool success = insertDetail(con, sqltext.ToString());
if (success)
{
Response.Write("<script>alert('用户注册成功');Window.location('Default.aspx');</script>");
}
else
{
Response.Write("<script>alert('注册失败!');Window.location('Register.aspx');</script>");
}
}
else
{
Response.Write("<script>alert('用户资料填写错误!!');Window.location('Register.aspx');</script>");
}
}
protected void cmdCancel_Click(object sender, EventArgs e)
{
txtName.Text = "";
txtpwd.Text = "";
txtrpwd.Text = "";
}
protected bool insertDetail(string context, string sql)
{
OleDbConnection con = new OleDbConnection(context);
OleDbCommand cmd = new OleDbCommand(sql, con);
try
{
if (con.State != System.Data .ConnectionState .Closed)
{
con.Close();
}
con.Open();
int i = cmd.ExecuteNonQuery();
return true;
}
catch (Exception e1)
{
string str = e1.Message;
return false;
}
finally
{
con.Close();
}
}
}
using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Text;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class UC_Reg : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{ } protected void cmdReg_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
string con = "Provider=Microsoft.Jet.Oledb.4.0;Data Source="+Server.MapPath("App_Data\\DB_CustomReg.mdb");
StringBuilder sqltext = new StringBuilder("Insert into CustomReg (Name,TrueName,Sex,PWD,Email,Phone,Address,LikeSort,Usefor,Work) Values ('" + txtName.Text + "'");
string str = Request.Params["txttruename"];
if (str == "")
{
str = "没填写";
sqltext .Append (",'"+ str +"'"); }
else
{
sqltext.Append(",'" + str + "'");
}
string sex;
if (RadioButton1.Checked)
{
sex = RadioButton1.Text;
sqltext.Append(",'" + sex+ "'");
}
else if(RadioButton2.Checked )
{
sex = RadioButton2.Text;
sqltext.Append(",'" + sex+ "'");
}
else
{
sex="没有填写";
sqltext.Append(",'" + sex+ "'");
} sqltext.Append(",'" + txtpwd .Text + "'");
string email = Request.Params["txtemail"];
if (email == "")
{
email = "没有填写";
sqltext.Append(",'" + email + "'");
}
else
{
sqltext.Append(",'" + email + "'");
}
string phone = Request.Params["txtphone"];
if (phone == "")
{
phone = "没有填写";
sqltext.Append(",'" + phone + "'");
}
else
{
sqltext.Append(",'" + email + "'");
}
string address = Request.Params["txtadd"];
if (address == "")
{
address = "没有填写";
sqltext.Append(",'" + address + "'");
}
else
{
sqltext.Append(",'" + address + "'");
}
string sort = lstSort.SelectedValue;
string use = lstUse.SelectedValue;
string work = lstWork.SelectedValue;
sqltext.Append(",'"+ sort +"'");
sqltext.Append(",'"+ use +"'");
sqltext.Append(",'"+ work +"'"); sqltext.Append(")");
bool success = insertDetail(con, sqltext.ToString());
if (success)
{
Response.Write("<script>alert('用户注册成功');Window.location('Default.aspx');</script>");
}
else
{
Response.Write("<script>alert('注册失败!');Window.location('Register.aspx');</script>");
}
}
else
{
Response.Write("<script>alert('用户资料填写错误!!');Window.location('Register.aspx');</script>");
}
}
protected void cmdCancel_Click(object sender, EventArgs e)
{
txtName.Text = "";
txtpwd.Text = "";
txtrpwd.Text = "";
}
protected bool insertDetail(string context, string sql)
{
OleDbConnection con = new OleDbConnection(context);
OleDbCommand cmd = new OleDbCommand(sql, con);
try
{
if (con.State != System.Data .ConnectionState .Closed)
{
con.Close();
}
con.Open();
int i = cmd.ExecuteNonQuery();
return true;
}
catch (Exception e1)
{
string str = e1.Message;
return false;
}
finally
{
con.Close();
}
}
}
解决方案 »
- 自定义控件年月日的问题
- 新手提问,怎么用c#实现soap功能?数据包如下,请各位指教!
- C#利用sql脚本生成数据库
- 一个关于宏的问题
- 怎样隐藏datagrid控件中的某一列?
- 急!关于DataGrid关闭autogeneratecolumns的换页问题
- 我想创建个TXT文件,我想改写这个TXT文件,我想保存为其他后缀的文件
- 是不是我的页面打开有问题,请大家指教
- C#操作数据库敏感字的问题
- TheAres(班门斧)请进,刚才贸然的就把分给了,结果照着你说给的那篇文章做了下,不可以,说没有可放置在工具箱的组件
- 解压缩软件RAR是怎么获得当前文件的路径的阿?
- 纸张设置对话框,为什么一点确定页边距就会自动减小一点直到变为零
if (phone == "")
{
phone = "没有填写";
sqltext.Append(",'" + phone + "'");
}
else
{
sqltext.Append(",'" + email + "'");
}这一句错了email 应该是 phone 吧