public void funinsert(object serder, EventArgs e)
{
string sqlinsert;
sqlinsert="insert user_info values('"+txtid.Text+"','"+txtname.Text+"','"+txtpsw.Text+"','"+txtrme.Text+"','"+txtsex.Text+"',"+txtage.Text+",'"+txtpurview.Text+"'"+")";
//sqlinsert = "insert user_info values('00011','huang','huang0','黄超','m',24,'游客')";
SqlConnection sqlconn = new SqlConnection(strConnstring);
sqlconn.Open();
SqlCommand sqlcomm = new SqlCommand(sqlinsert, sqlconn);
sqlcomm.ExecuteNonQuery();
sqlconn.Close();
sqlconn = null;
sqlcomm = null;
bindgrid();
}
sqlinsert="insert user_info values('"+txtid.Text+"','"+txtname.Text+"','"+txtpsw.Text+"','"+txtrme.Text+"','"+txtsex.Text+"',"+txtage.Text+",'"+txtpurview.Text+"'"+")";
这到底哪里错了啊,txtid,txtname等都是txetbox控件的ID
在数据据里除了age是int 外其它都char
异常详细信息: System.Data.SqlClient.SqlException: 第 1 行: ',' 附近有语法错误。
源错误:
行 37: sqlconn.Open();
行 38: SqlCommand sqlcomm = new SqlCommand(sqlinsert, sqlconn);
行 39: sqlcomm.ExecuteNonQuery();
行 40: sqlconn.Close();
行 41: sqlconn = null;
快疯了啊 ,我把这一句注释掉,改为 //sqlinsert = "insert user_info values('00011','huang','huang0','黄超','m',24,'游客')"; 这一句程序就没错了,走路路过的都帮偶瞧瞧啊,,急啊
--------------------------------------------------------------------------------------------------------
全部代码如下
default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Debug="true" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>实验六-3</title>
<style >
hr {height:2px;color:black;}
.StdText {font-family:verdana;font-size:9pt;font-weight:bold;}
.StdTextBox {font-family:verdana;font-size:9pt;border:solid 1px black;filter:progid:DXImageTransform.Microsoft.dropshadow(OffX=2, OffY=2, Color='gray', Positive='true');}
.Shadow {filter:progid:DXImageTransform.Microsoft.dropshadow(OffX=2, OffY=2, Color='gray', Positive='true');}
</style>
</head>
<body>
<h4>三、在插入记录时,用户输入的信息使用验证控件进行检查。</h4>
<hr style="border-color:Gray "/>
<form id="form1" runat="server">
<table style="font-size:x-small">
<tr >
<td align="left" colspan="4" style="height: 1px" >
<h5>插入记录</h5></td>
</tr>
<tr>
<td align="right" style="width: 77px" >
编号:</td>
<td><asp:TextBox ID="txtid" runat="server" CssClass="stdTextBox"/></td>
<td align="right" style="width: 79px">
妮称:</td>
<td><asp:TextBox ID="txtname" runat="server" CssClass="stdTextBox" /></td>
</tr>
<tr>
<td align="right" style="width: 77px" >
密码:</td>
<td><asp:TextBox ID="txtpsw" runat="server" CssClass ="stdTextBox"/></td>
<td align="right" style="width: 79px" >
真实姓名:</td>
<td><asp:TextBox ID="txtrme" runat="server" CssClass="stdTextBox" /></td>
</tr>
<tr>
<td align="right" >
性别:</td>
<td ><asp:TextBox ID="txtsex" runat="server" CssClass ="stdTextBox" /></td>
<td align="right" style="width: 79px" >
年龄:</td>
<td><asp:TextBox ID="txtage" runat="server" CssClass="stdTextBox" /></td>
</tr>
<tr>
<td align="right" >
权限:</td>
<td><asp:TextBox ID="txtpurview" runat="server" CssClass="stdTextBox" /></td>
</tr>
<tr>
<td><asp:Button ID="btsumbit" runat="server" text="插入记录" OnClick="funinsert" /></td>
</tr>
</table>
<hr style="border-color:Gray "/>
<asp:DataGrid id="dg" runat="server"
AutoGenerateColumns="false"
CssClass="shadow" BackColor="white"
CellPadding="2" CellSpacing="2" GridLines="none"
BorderStyle="solid" BorderColor="black" BorderWidth="1"
font-size="x-small" font-names="verdana"
DataKeyField="user_userid">
<AlternatingItemStyle BackColor="palegoldenrod" />
<ItemStyle BackColor="beige" />
<HeaderStyle ForeColor="white" BackColor="brown" Font-Bold="true" />
<Columns >
<asp:BoundColumn DataField="user_userid" ReadOnly="true" HeaderText="编号" />
<asp:BoundColumn DataField="user_username" ReadOnly="true" HeaderText="妮称" />
<asp:BoundColumn DataField="user_password" ReadOnly="true" HeaderText="密码" />
<asp:BoundColumn DataField="user_rname" ReadOnly="true" HeaderText="姓名" />
<asp:BoundColumn DataField="user_sex" ReadOnly="true" HeaderText="性别" />
<asp:BoundColumn DataField="user_age" ReadOnly="true" HeaderText="年龄" />
<asp:BoundColumn DataField="user_purview" ReadOnly="true" HeaderText="权限" />
</Columns>
</asp:DataGrid>
</form>
</body>
</html>
.CS代码如下
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
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 _Default : System.Web.UI.Page
{
protected string strConnstring = "Data Source=127.0.0.1;uid=sa;pwd=;Initial Catalog=SY2040570庄文鹏";
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack) bindgrid();
}
public void bindgrid()
{
SqlConnection sqlconn = new SqlConnection(strConnstring);
SqlDataAdapter sqld = new SqlDataAdapter("select * from user_info", sqlconn);
DataSet ds = new DataSet();
sqlconn.Open();
sqld.Fill(ds, "tabstud");
dg.DataSource = ds.Tables["tabstud"];
dg.DataBind();
sqlconn.Close();
sqlconn = null;
}
public void funinsert(object serder, EventArgs e)
{
string sqlinsert;
sqlinsert ="insert user_info values('"+txtid.Text+"','"+txtname.Text+"','"+txtpsw.Text+"','"+txtrme.Text+"','"+txtsex.Text+"',"+txtage.Text+",'"+txtpurview.Text+"'"+")";
//sqlinsert = "insert user_info values('00011','huang','huang0','黄超','m',24,'游客')";
//sqlinsert = "insert user_info values('00011','huang','huang0','黄超','m',24,'游客')";
SqlConnection sqlconn = new SqlConnection(strConnstring);
sqlconn.Open();
SqlCommand sqlcomm = new SqlCommand(sqlinsert, sqlconn);
sqlcomm.ExecuteNonQuery();
sqlconn.Close();
sqlconn = null;
sqlcomm = null;
bindgrid();
}
}
第 1 行: ',' 附近有语法错误。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.Data.SqlClient.SqlException: 第 1 行: ',' 附近有语法错误。
源错误:
行 38: sqlconn.Open();
行 39: SqlCommand sqlcomm = new SqlCommand(sqlinsert, sqlconn);
行 40: sqlcomm.ExecuteNonQuery();
行 41: sqlconn.Close();
行 42: sqlconn = null;
[此贴子已经被作者于2007-5-25 11:12:54编辑过]