提交新用户时注册表单出错。 [英] Error on registration form when submitting a new user.

查看:71
本文介绍了提交新用户时注册表单出错。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个注册新用户的表单。当用户输入他们的电子邮件地址和密码然后单击提交我收到错误:System.NullReferenceException {对象引用未设置为对象的实例。}。这是什么错误,我该如何解决?错误发生在第29行。该行是:

  int  temp = Convert.ToInt32(userExist。 ExecuteScalar()。ToString()); 





以下是表单的所有代码:



 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;
使用 System.Data.SqlClient;
使用 System.Configuration;
使用 System.Web.Security;
使用 System.Security.Cryptography;

public partial class SubmitPage:System.Web.UI.Page
{
protected void Page_Load( object sender,EventArgs e)
{
if (IsPostBack)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings [ PasswordConnectionString]。ConnectionString);
con.Open();

string cmdStr = 从TableSecurity中选择INST_ID,EmailAddress,其中EmailAddress =' + TextBoxEA.Text + ';
string cmdStr2 = 选择INST_ID,EmailAddress来自TableCEO,其中EmailAddress =' + TextBoxEA.Text + ';
string cmdStr3 = 选择INST_ID,EmailAddress来自TableialO,其中EmailAddress =' + TextBoxEA.Text + ';

SqlCommand userExist = new SqlCommand(cmdStr,con);
SqlCommand cmd = new SqlCommand( select INST_ID,来自TableSecurity的电子邮件地址,con);
SqlCommand cmd2 = new SqlCommand( select来自TableCEO的INST_ID,EmailAddress,con);
SqlCommand cmd3 = new SqlCommand( select来自TableIALO的INST_ID,EmailAddress,con);
int temp = Convert.ToInt32(userExist.ExecuteScalar()。ToString());
if (temp == 1

Response.Write ( 用户名已存在!!!< br />请选择其他用户名。 );
}



}

受保护 void Submit_Click( object sender,EventArgs e)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings [ PasswordConnectionString ] .ConnectionString);
con.Open();

string chkUser = 从TableCEO中选择INST_ID,EmailAddress,其中EmailAddress' + TextBoxEA.Text + ;
chkUser = 从TableialO中选择INST_ID,EmailAddress,其中EmailAddress' + TextBoxPW.Text + ;
SqlCommand chkUsercmd = new SqlCommand(chkUser,con);


string insCmd = 插入TableSecurity(INST_ID,EmailAddress,Password)值(@INST_ID,@ EmailAddress,@ Password);
SqlCommand insertUser = new SqlCommand(insCmd,con);
insertUser.Parameters.AddWithValue( @ EmailAddress,TextBoxEA.Text);
insertUser.Parameters.AddWithValue( @ Password,TextBoxPW.Text);



try
{
insertUser.ExecuteNonQuery();
con.Close();
Response.Redirect( Login.aspx);
}
catch (例外)
{
Response.Write( 发生错误: + er.Message);
}
最后
{
}
}

}

解决方案

你是个好程序员......



但是......是我注意到的一件事....你在将参数传递给SqlCommand对象时错过了一行添加到你的代码中。



< br $> b $ b

  //  您的代码....  

字符串 insCmd = < span class =code-string>插入TableSecurity(INST_ID,EmailAddress,Password)值(@INST_ID,@ EmailAddress,@ Password);
SqlCommand insertUser = new SqlCommand(insCmd,con);



// 以下是解决方案 -
// 将@INST_ID添加到SQLCommand

insertUser .Parameters.AddWithValue( @ INST_ID' TextBoxName.Text');

// ---------------- ----

// 您的代码......
insertUser.Parameters.AddWithValue( @ EmailAddress,TextBoxEA.Text);
insertUser.Parameters.AddWithValue( @ Password,TextBoxPW.Text);


Yup ....



从所需的表中获取INST_ID的值...而且只是将它分配给局部变量...然后将其值而不是Textbox传递给SqlCommand对象





  int  tempID = ' 用于检索唯一INST_ID的代码' 

insertUser.Parameters.AddWithValue( @ INST_ID, tempID);


I have a form that registraters a new user. When the user puts in their email address and password then click submit I get an error for: System.NullReferenceException {"Object reference not set to an instance of an object."}. What is this error and how can I fix it? The error happens on line 29. that line is:

int temp = Convert.ToInt32(userExist.ExecuteScalar().ToString());



Here is all of the code for the form:

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.Configuration;
using System.Web.Security;
using System.Security.Cryptography;

public partial class SubmitPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["PasswordConnectionString"].ConnectionString);
            con.Open();

            string cmdStr = "Select INST_ID, EmailAddress from TableSecurity where EmailAddress='" + TextBoxEA.Text + "'";
            string cmdStr2 = "Select INST_ID, EmailAddress from TableCEO where EmailAddress ='" + TextBoxEA.Text + "'";
            string cmdStr3 = "Select INST_ID, EmailAddress from TableIALO where EmailAddress ='" + TextBoxEA.Text + "'";
            
            SqlCommand userExist = new SqlCommand(cmdStr, con);
            SqlCommand cmd = new SqlCommand("select INST_ID, EmailAddress from TableSecurity", con);
            SqlCommand cmd2 = new SqlCommand("select INST_ID, EmailAddress from TableCEO", con);
            SqlCommand cmd3 = new SqlCommand("select INST_ID, EmailAddress from TableIALO", con);
            int temp = Convert.ToInt32(userExist.ExecuteScalar().ToString());
            if (temp == 1)

                Response.Write("User Name Already Exist!!!<br /> Please Choose Another User Name.");
        }



    }

    protected void Submit_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["PasswordConnectionString"].ConnectionString);
        con.Open();

        string chkUser = "select INST_ID, EmailAddress from TableCEO where EmailAddress'" + TextBoxEA.Text + "";
        chkUser = "select INST_ID, EmailAddress from TableIALO where EmailAddress'" + TextBoxPW.Text + "";
        SqlCommand chkUsercmd = new SqlCommand(chkUser, con);


        string insCmd = "Insert into TableSecurity (INST_ID, EmailAddress, Password) values (@INST_ID, @EmailAddress, @Password)";
        SqlCommand insertUser = new SqlCommand(insCmd, con);
        insertUser.Parameters.AddWithValue("@EmailAddress", TextBoxEA.Text);
        insertUser.Parameters.AddWithValue("@Password", TextBoxPW.Text);
        


        try
        {
            insertUser.ExecuteNonQuery();
            con.Close();
            Response.Redirect("Login.aspx");
        }
        catch (Exception er)
        {
            Response.Write("An error occurred: " + er.Message);
        }
        finally
        {
        }
    }
    
}

解决方案

You are Good Programmer....

But..there is One thing I noticed.... that....you just missed a single line to add into your code while passing Parameters to the SqlCommand Object.



//Your Code....

string insCmd = "Insert into TableSecurity (INST_ID, EmailAddress, Password) values (@INST_ID, @EmailAddress, @Password)";
SqlCommand insertUser = new SqlCommand(insCmd, con);



//Here is the Solution--
//Add @INST_ID to SQLCommand

        insertUser.Parameters.AddWithValue("@INST_ID", 'TextBoxName.Text');

//--------------------

      //Your Code...
        insertUser.Parameters.AddWithValue("@EmailAddress", TextBoxEA.Text);
        insertUser.Parameters.AddWithValue("@Password", TextBoxPW.Text);


Yup....

Fetch the value of INST_ID from required table...and just assign it to the local variable ...then pass its value instead of Textbox into SqlCommand object


int tempID='code for retrieving unique INST_ID'

insertUser.Parameters.AddWithValue("@INST_ID", tempID);


这篇关于提交新用户时注册表单出错。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆