using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Xml;namespace RegWeb
{
    public partial class landing : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {        }
        protected int isName()
        {
            int i;
            String name = TextName.Text;
            if (name.Trim() != "")
            {
                SqlConnection con = new SqlConnection("server= .;database=;uid=sa;pwd=");
                con.Open();
                SqlCommand cmd = new SqlCommand("select count(*) from ExtUsers where UserID ='" + name + "'", con);
                return i = Convert.ToInt32(cmd.ExecuteNonQuery());
                con.close();
            }
            else
            {
                return 1;
            }
        }        protected void ButtonReg_Click(object sender, EventArgs e)
        {
            int i = isName();
            if (i <= 0)
            {
                if (add())
                {
                    SqlConnection con = new SqlConnection("server=.;database=d;uid=sa;pwd=");
                    con.Open();
                    SqlCommand cmd = new SqlCommand("insert into ExtUsers(UserID,LastLogon) values('" + TextName.Text + "','" + DateTime.Now.ToString() + "')", con);
                    cmd.ExecuteNonQuery();
                    Response.Write("<script>alert('注册成功!单击确定登陆页面');location='landing.aspx';</script>");
                    con.Close();
                }
                else
                {
                    Response.Write("<script>alert('注册失败!')<script>");
                }            }
            else
            {
                Response.Write("<script>alert('会员名已经存在!')</script>");
            }
        }
        public bool add()
        {            string pass = FormsAuthentication.HashPasswordForStoringInConfigFile(TextPass.Text, "MD5");
            string sql = "insert into ExtUsers(UserID,Pass,ExtUserGroupID, UserLName ,UserFiName,Position,Notes) values('" + TextName.Text.Trim() + "','" + TextPass.Text.Trim() + "','" + TextGroupID.Text.Trim() + "','" + TextLName.Text.Trim() + "','" + TextFiName.Text.Trim() + "','" + TextPosition.Text.Trim() + "','" + TextNotes.Text.Trim() + "')";
            SqlConnection con = new SqlConnection("Server=.;database=;uid=sa;pwd=");
            con.Open();
            SqlCommand cmd = new SqlCommand(sql, con);
            int i = Convert.ToInt32(cmd.ExecuteNonQuery());
            con.Close();
            if (i > 0)
            {
                return true;
            }
            else
            {
                return false;
            }        }
        protected void ButtonCancel_Click1(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx");
        }
    }}  如题,这其中那些地方有错误        protected int isName()
        {
            int i;
            String name = TextName.Text;
            if (name.Trim() != "")
            {
                SqlConnection con = new SqlConnection("server= .;database=;uid=sa;pwd=");
                con.Open();
                SqlCommand cmd = new SqlCommand("select count(*) from ExtUsers where UserID ='" + name + "'", con);
                return i = Convert.ToInt32(cmd.ExecuteNonQuery());
                con.close();
            }   con.close();无法检测,如果删除有没有什么影响..  
我在插入数据的时候,为什么总是提示
无法将 NULL 值插入列 'Pass',表 'admin.dbo.ExtUsers';该列不允许空值。INSERT 失败。
语句已终止。

请那位大大 帮忙解决下..在线等

解决方案 »

  1.   

    [SqlException (0x80131904): 无法将 NULL 值插入列 'Pass',表 'dataclub.dbo.ExtUsers';该列不允许空值。INSERT 失败。
    语句已终止。]
       System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +109
       System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +57
       System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +207
       System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2433
       System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async) +192
       System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) +256
       System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +117
       RegWeb.landing.ButtonReg_Click(Object sender, EventArgs e) in c:\Inetpub\wwwroot\Login\Reg.cs:50
       System.Web.UI.WebControls.Button.OnClick(EventArgs e) +78
       System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +100
       System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2863
       
    这是错误的地方,刚1楼说的我改了 没有用 谢谢了
      

  2.   

    我在把 数据库里的 限制就是 不允许空值划掉的时候,一切就正常了,(我知道这是不对.)麻烦那位大虾帮忙SEE SEE
      

  3.   

    ...自己改了下  现在还有个小小的问题,就是在跳转页面 验证会员名 是否存在的情况,这里怎么不运行,是不是那里循环出错?  麻烦那位给我看看.改正后的代码是这样      protected int isName()
            {
                int i;
                String name = TextName.Text;
                if (name.Trim() != "")
                {
                    SqlConnection con = new SqlConnection("server=hwang;database=dataclub;uid=sa;pwd=");
                    con.Open();
                    SqlCommand cmd = new SqlCommand("select count(*) from ExtUsers where UserID ='" + name + "'", con);
                    return i = Convert.ToInt32(cmd.ExecuteNonQuery());
                    con.Close();
                }
                else
                {
                    return 1;
                }
            }        protected void ButtonReg_Click(object sender, EventArgs e)
            {
                int i = isName();
                String name = TextName.Text;
                if (i <= 0)
                {
                    if (add())
                    {
                        SqlConnection con = new SqlConnection("server=hwang;database=dataclub;uid=sa;pwd=");
                        con.Open();
                        SqlCommand cmd = new SqlCommand("select count(*) from ExtUsers where UserID ='" + name + "'", con);
                        cmd.ExecuteNonQuery();
                        Response.Write("<script>alert('注册成功!单击确定登陆页面');location='main.aspx';</script>");
                        con.Close();
                    }
                    else
                    {
                        Response.Write("<script>alert('注册失败!')<script>");
                    }            }
                else
                {
                    Response.Write("<script>alert('会员名已经存在!')</script>");
                }
            }
            public bool add()
            {            string pass = FormsAuthentication.HashPasswordForStoringInConfigFile(TextPass.Text, "MD5");
                string sql = "insert into ExtUsers(UserID,Pass,ExtUserGroupID, UserLName ,UserFiName,Position,Notes,LastLogon) values('" + TextName.Text.Trim() + "','" + pass + "','" + TextGroupID.Text.Trim() + "','" + TextLName.Text.Trim() + "','" + TextFiName.Text.Trim() + "','" + TextPosition.Text.Trim() + "','" + TextNotes.Text.Trim() + "','" + DateTime.Now.ToString() + "')";
                SqlConnection con = new SqlConnection("Server=hwang;database=dataclub;uid=sa;pwd=");
                con.Open();
                SqlCommand cmd = new SqlCommand(sql, con);
                int i = Convert.ToInt32(cmd.ExecuteNonQuery());
                con.Close();
                if (i > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }        }
            protected void ButtonCancel_Click1(object sender, EventArgs e)
            {
                Response.Redirect("Default.aspx");
            }
        }}
      

  4.   

    ...你自己刚才不是也说出问题的所在了吗?    就是因为'dataclub.dbo.ExtUsers' 这个表里的'Pass'字段 你设置的是不能为空
          所以你在插入的记录的时候 这个字段是必须有值的! 如果没值就是你刚才看到的错误....
     
      

  5.   

    看着没什么问题....如果你TextName.Text 不为空的话!就应该进行用户名验证
     if (add())
                    {
                       // SqlConnection con = new SqlConnection("server=hwang;database=dataclub;uid=sa;pwd=");
                      //  con.Open();
                       // SqlCommand cmd = new SqlCommand("select count(*) from ExtUsers where UserID ='" + name + "'", con);
                      //  cmd.ExecuteNonQuery();
                      //为什么要写上面的几句?  用意何在?
                        Response.Write("<script>alert('注册成功!单击确定登陆页面');location='main.aspx';</script>");
                        con.Close();
                    }
                    else
                    {
                        Response.Write("<script>alert('注册失败!')<script>");
                    }