2014-04-06 14 views
0

即时通讯相当新,但我熟悉空引用异常,但我无法解决这一问题。我试图从webform收集数据,并通过连接字符串传递给我的数据库,这是发生异常的时候。NullReferenceException我很熟悉它们,但在这种情况下不能解决

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; 


public partial class Register : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     try 
     { 
      if (IsPostBack) 
      { 
       SqlConnection studConnA = new SqlConnection(ConfigurationManager.ConnectionStrings["StudConnection"].ConnectionString); 
       studConnA.Open(); 
       string checkuser = "select count(*) from StudTable where Name='" + TextBoxName.Text + "'"; 
       SqlCommand studComA = new SqlCommand(checkuser, studConnA); 
       int temp = Convert.ToInt32(studComA.ExecuteScalar().ToString()); 
       if (temp == 1) 
       { 
        Response.Write("User already Exists"); 
       } 
       studConnA.Close(); 
      } 

     } 
     catch (Exception ex) 
     { 
      Response.Write("Error:" + ex.ToString()); 
     } 
    } 


    protected void Button1_Click(object sender, System.EventArgs e) 
    { 
     try 
     { 
      SqlConnection studConn = new SqlConnection(ConfigurationManager.ConnectionStrings["StudConnectionString"].ConnectionString); 
      studConn.Open(); 
      string insertQuery = "insert into StudTable (Name,Email,Age,Continent,School,Password) values (@name,@email,@age,@cont,@school,@pass)"; 
      SqlCommand studCom = new SqlCommand(insertQuery, studConn); 
      studCom.Parameters.AddWithValue("@name", TextBoxName.Text); 
      studCom.Parameters.AddWithValue("@email", TextBoxEmail.Text); 
      studCom.Parameters.AddWithValue("@age", TextBoxAge.Text); 


studCom.Parameters.AddWithValue("@cont",DropDownCont.SelectedItem.ToString()); 
      studCom.Parameters.AddWithValue("@school", TextBoxSchool.Text); 
      studCom.Parameters.AddWithValue("@pas", TextBoxPass.Text); 

      studCom.ExecuteNonQuery(); 
      Response.Redirect("Backend.aspx"); 
      Response.Write("Your Registration is Sucessful"); 

      studConn.Close(); 
     } 
     catch (Exception ex) 
     { 
      Response.Write("Error:" +ex.ToString()); 
     } 
    } 
} 

空引用发生在第19行

Line 17:     if (IsPostBack) 
Line 18:     { 
Line 19:      SqlConnection studConnA = new SqlConnection(ConfigurationManager.ConnectionStrings["StudConnection"].ConnectionString); 
Line 20:      studConnA.Open(); 
Line 21:      string checkuser = "select count(*) from StudTable where Name='" + TextBoxName.Text + "'"; 

我相信这个问题是我的连接字符串,但林不知道,

谁能帮我解决这个语法?

+4

那么它听起来像你没有叫'StudConnection'连接字符串 - 但即使你已经固定的,你应该*还*参数化您的SQL,而不是像现在一样直接将值包括在内。 –

+0

这个nullref与其他的一样。您已经取消了空引用。点之前的一件事是空的。可能是'ConfigurationManager.ConnectionStrings [“StudConnection”]'。 – usr

+0

感谢您的帮助,我以某种方式导入了错误的web.config,但现在解决了它! – Erademus

回答

1

检查您的配置文件中的以下项:

<connectionStrings> 
<add name="StudConnection" connectionString="YOUR DETAILS" /> 
</connectionStrings> 

如果它不存在,添加右键,然后重试。

你也可以用下面的代码检查问题:

if (IsPostBack) 
    { 
     // if this line fails, then you don't have the proper connection string 
     // element in the config file. 
     Debug.Assert(ConfigurationManager.ConnectionStrings["StudConnection"] != null); 

     SqlConnection studConnA = new SqlConnection(ConfigurationManager.ConnectionStrings["StudConnection"].ConnectionString); 
     studConnA.Open(); 
     string checkuser = "select count(*) from StudTable where Name='" + TextBoxName.Text + "'"; 
     SqlCommand studComA = new SqlCommand(checkuser, studConnA); 
     int temp = Convert.ToInt32(studComA.ExecuteScalar().ToString()); 
     if (temp == 1) 
     { 
      Response.Write("User already Exists"); 
     } 
     studConnA.Close(); 
    } 
0

这看起来没有配置名为StudConnection的连接字符串。