读书人

菜鸟编写注册页面写入不了数据库求相

发布时间: 2012-09-19 13:43:54 作者: rapoo

初学者编写注册页面,写入不了数据库求大虾相助
我的注册页面:register.aspx代码如下:

C# code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="register.aspx.cs" Inherits="register" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title></title>    <script language="javascript" type="text/javascript">// <![CDATA[        function Radio1_onclick() {        }        function Text2_onclick() {        }        function Text1_onclick() {        }// ]]>    </script>    <style type="text/css">        .style1        {            width: 273px;        }    </style></head><body>    <form id="form1" runat="server">    <div>        <table style=" width :620px; height:400px;">            <tr>                <td>                  <asp:Label ID="Label1" runat="server" Text="学号:"></asp:Label>                </td>                <td class="style1">                    <input id="Text1" type="text" runat="server" onclick="return Text1_onclick()" />                    </td>                    <td>          <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="Text1" ErrorMessage="* 学号必须填写" ForeColor=Red>          </asp:RequiredFieldValidator>                </td>                         <tr>                <td>                    <asp:Label ID="Label2" runat="server" Text="姓名:"></asp:Label>                                 </td>                <td class="style1">                    <input id="Text2" type="text" runat="server" onclick="return Text2_onclick()" />                    </td>                    <td>                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="Text2" ErrorMessage="* 姓名必须填写"  ForeColor=Red></asp:RequiredFieldValidator>                </td>                        </tr>            <tr>                <td>                    <asp:Label ID="Label3" runat="server" Text="密码:"></asp:Label>                 </td>                <td class="style1">                    <input id="Password1" type="password" runat="server" />                    </td>                    <td>                    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="Password1" ErrorMessage="* 密码必须填写"  ForeColor=Red></asp:RequiredFieldValidator>                </td>              </tr>                <tr>                <td>                    <asp:Label ID="Label4" runat="server" Text="请再次输入密码确认:"></asp:Label>                </td>                <td class="style1">                    <input id="Password2" type="password" runat="server" />                    </td>                    <td>                    <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="Password2" ErrorMessage="* 请再次输入密码确认"  ForeColor=Red></asp:RequiredFieldValidator>                </td>            </tr>            <tr>            <td>                <asp:Label ID="Label5" runat="server" Text="班级:"></asp:Label>             </td>            <td class="style1">                <input id="Text3" type="text"  runat="server"/>                </td>                <td>                <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="Text3" ErrorMessage="* 班级必须填写"  ForeColor=Red></asp:RequiredFieldValidator>            </td>            </tr>            <tr>            <td>                <asp:Label ID="Label6" runat="server" Text="手机号码:"></asp:Label>            </td>            <td class="style1">                <input id="Text4" type="text" runat="server" />                </td>                <td>                <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="Text4" ErrorMessage="* 请正确填写手机号码"  ForeColor=Red></asp:RequiredFieldValidator>            </td>            </tr>            <tr>            <td>                <asp:Label ID="Label7" runat="server" Text="Email地址:"></asp:Label>            </td>            <td class="style1">                <input id="Text5" type="text" runat="server" />                </td>                <td>                <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ControlToValidate="Text5" ErrorMessage="* Email必须填写"  ForeColor=Red></asp:RequiredFieldValidator>            </td>            </tr>            <tr>            <td>                <asp:Label ID="Label8" runat="server" Text="年龄:"></asp:Label>            </td>            <td class="style1">                <input id="Text6" type="text" runat="server" />                         </td>            </tr>            <tr>            <td>                <asp:Label ID="Label9" runat="server" Text="性别:"></asp:Label>            </td>            <td class="style1">                <asp:RadioButton ID="RadioButton1" runat="server" Text="男" GroupName="Choose" />                                      <asp:RadioButton ID="RadioButton2" runat="server" Text="女"  GroupName="Choose" />                                  </td>                          </tr>                        <tr>            <td></td>            <td align="center">                <asp:ImageButton ID="ImageButton1" runat="server"                     ImageUrl="~/image/OkButton.png" onclick="ImageButton1_Click"/>            </td>            </tr>                    </table>    </div>        </form></body></html> 


其中register.aspx.cs的事件代码如下:
C# code
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Data.SqlClient;using System.Data;public partial class register : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {    }    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)    {        bool paswCheck = (Password1.Value == Password2.Value);        if (paswCheck == true)        {        }        else        {            Response.Write("<script>window.alert(\"两次密码输入不一致,请重新输入\");</script>");        }        if (Check(Text1.Value) || Check(Text2.Value) || Check(Text4.Value) || Check(Text5.Value) || Check(Text6.Value))            //判断                    {            Response.Write("<script>window.alert(\"用户信息中不能够包含特殊字符如<,>,',//,\\等,请审核\");</script>");        //输出信息                               }        else        {            SqlConnection con = new SqlConnection("server='(local)';database='Record';uid='sa';pwd='j429828893'");    //建立连接            con.Open();            string check = "select * from Student where SID='" + Text1.Value + "'";            SqlDataAdapter da = new SqlDataAdapter(check, con);                        //创建适配器            DataSet ds = new DataSet();                                            //创建数据集            da.Fill(ds, "table");                                                    //填充数据集            if (da.Fill(ds, "table") > 0)                                                //判断同名            {                Response.Write("<script>window.alert(\"用户名重复,请联系管理员\");</script>");                                //输出信息            }            else            {                                string strsql = "Insert into Student (SID,Sname,Spassword,class,Tel,Email,Age,) values  ('" + int.Parse(Text1.Value) + "','" + Text2.Value.ToString() + "','" + Password1.Value.ToString() + "','" + Text3.Value.ToString() + "','" + Text4.Value.ToString() + "','" + Text5.Value.ToString() + "','" +int.Parse(Text6.Value )+ "')";                                                                               SqlCommand cmd = new SqlCommand(strsql,con);                    //创建执行                    cmd.ExecuteNonQuery();                                    //执行SQL                    con.Close();                    Response.Write("<script>window.alert(\"恭喜你注册成功,登录账号为您的学号!!!\");</script>");                            //提示成功            }        }    }   protected bool Check(string Value)    {        if (Value.Contains("<") || Value.Contains(">") || Value.Contains("'") ||                 Value.Contains("//") || Value.Contains("\\")||Value.Contains("$"))                                    //检查字串        {            return true;                                                        //返回真        }        else        {            return false;                                                        //返回假        }        }   }


运行到cmd.ExecuteNonQuery();处就提示System.Data.SqlClient.SqlException: ')' 附近有语法错误。写入不了数据库,请教一下各位是哪里出了问题怎么写入不了数据库?




[解决办法]
目测,多了逗号,去掉

Insert into Student (SID,Sname,Spassword,class,Tel,Email,Age,)

读书人网 >C#

热点推荐