本文介绍了错误:-System.NullReferenceExc ...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 使用System; 使用System.Collections; 使用System.Configuration; 使用System.Data; 使用System.Linq; 使用System.Web; 使用System.Web.Security; 使用System.Web.UI ; 使用System.Web.UI.HtmlControls; 使用System.Web.UI.WebControls; 使用System.Web.UI。 WebControls.WebParts; 使用System.Xml.Linq; 使用System.Data.SqlClient; public partial class registration:System.Web.UI.Page { protected void Page_Load(object sender,EventArgs e) { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings [RegConnectionString]。ConnectionString); con.Open(); string cmdStr =从注册中选择count(*),其中Username =''+ TextBoxUN.Text +''; SqlCommand UserExit = new SqlCommand(cmdStr,con); int temp = Convert.ToInt32(UserExit.ExecuteScalar()。ToString()); con.Close(); if(temp == 1) { Response.Write(用户名已经存在...... !!! 请选择其他用户名); } } protected void Submit_Click1(object sender,EventArgs e) { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings [RegConnectionString]。ConnectionString); con.Open(); string inCmd =插入注册(用户名,密码,EmailAddress,国家)值(@ UserName,@ Password,@ EmailAddress,@ Country); SqlCommand insertUser = new SqlCommand( inCmd,con); insertUser.Parameters.AddWithValue(@ UserName,TextBoxUN.Text); insertUser.Parameters.AddWithValue(@ Password,TextBoxPass。文字); insertUser.Parameters.AddWithValue(@ EmailAddress,TextBoxEA.Text); insertUser.Parameters.AddWithValue(@ FullName,TextBoxFN.Text); insertUser。 Parameters.AddWithValue(@ Country,DropDownListCountry.SelectedItem.ToString()); try { insertUser.ExecuteNonQuery(); con.Close(); Response.Redirect(login.aspx); } catch(例外) { Response.Write( 事情很糟糕); } 终于 { //你想要什么特别的东西添加 } } } 解决方案 检查我web.config你有RegConnectionString连接字符串还是ty正确的。 并检查你的连接字符串是否适合连接。 SqlConnection(ConfigurationManager.ConnectionStrings [RegConnectionString]。ConnectionString); 这行此行我有关于nullrefrenceexception的错误.... ..... using System;using System.Collections;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;using System.Data.SqlClient;public partial class registration : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RegConnectionString"].ConnectionString); con.Open(); string cmdStr="Select count(*) from Registration where Username =''"+TextBoxUN.Text+"''"; SqlCommand UserExit=new SqlCommand(cmdStr,con); int temp = Convert.ToInt32(UserExit.ExecuteScalar().ToString()); con.Close(); if(temp==1) { Response.Write("Username already exist...!!!please choose another Username"); } } protected void Submit_Click1(object sender, EventArgs e) { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RegConnectionString"].ConnectionString); con.Open(); string inCmd="Insert into Registration (UserName, Password, EmailAddress, Country) Values (@UserName,@Password,@EmailAddress,@Country)"; SqlCommand insertUser = new SqlCommand(inCmd,con); insertUser.Parameters.AddWithValue("@UserName",TextBoxUN.Text); insertUser.Parameters.AddWithValue("@Password",TextBoxPass.Text); insertUser.Parameters.AddWithValue("@EmailAddress",TextBoxEA.Text); insertUser.Parameters.AddWithValue("@FullName",TextBoxFN.Text); insertUser.Parameters.AddWithValue("@Country",DropDownListCountry.SelectedItem.ToString()); try { insertUser.ExecuteNonQuery(); con.Close(); Response.Redirect("login.aspx"); } catch(Exception er) { Response.Write("something is bad happened"); } finally { //any special thing you want add } }} 解决方案 check i web.config do you have RegConnectionString connection string or it is typed correctly.and also check is your connection string right for connection.SqlConnection(ConfigurationManager.ConnectionStrings["RegConnectionString"].ConnectionString);here in this line i have error about nullrefrenceexception......... 这篇关于错误:-System.NullReferenceExc ...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-29 17:11