2014-04-06 5 views
0

메신저 ASP.net에 상당히 익숙하지만 Null 참조 예외에 익숙하지만이 문제는 해결할 수 없다. 메신저에서 데이터를 수집하고 연결 문자열을 통해 내 데이터베이스로 전달하려고하면 예외가 발생합니다.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()); 
     } 
    } 
} 

널 참조가 나는 문제가 내 연결 문자열하지만 메신저하지 않도록,

이 사람이 나에게이 문제를 해결하는 데 도움이 수의 구문이라고 생각

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 + "'"; 

라인 (19)에서 발생?

+4

글쎄 그것은 소리 - 당신이 수정 한 경우에도 비록 당신이해야 그 * 또한 * 현재 SQL처럼 값을 직접 포함하지 말고 SQL을 매개 변수화하십시오. –

+0

이 nullref는 다른 것과 같습니다. null 참조를 역 참조했습니다. 점들 앞에있는 것들 중 하나는 null입니다. 아마,'ConfigurationManager.ConnectionStrings [ "StudConnection"]'. – usr

+0

도움을 주셔서 감사합니다. 어떻게 든 잘못된 web.config를 가져 왔지만 지금은 해결되었습니다. – Erademus

답변

1

은 다음 키에 대한 구성 파일을 확인 : 존재하지 않는 경우 오른쪽 키를 추가하고 다시 시도

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

.

또한 다음 코드로 문제를 확인할 수 있습니다

: 당신이 StudConnection``라는 연결 문자열이없는 것처럼

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이라는 연결 문자열이 구성되어 있지 않은 것으로 보입니다.