2012-12-02 4 views
0

aConnection.Open() 줄에 오류가 발생하여 그 이유를 알 수 없습니다. 이 정확한 코드가 작동했다 그리고 나서 다른 클래스에 기본적으로 동일한 코드를 추가하여 차량 테이블에 값을 삽입하고이 오류가 발생하기 시작했습니다. 그래서 나는 모든 것을 삭제했다. 나는 그것이 작동 할 때 그것을 가지고 있었고 Save를 클릭 할 때 여전히 오류가있다. 어떤 아이디어? 나는 발견하고 해결책에있는 모든 자원을 다 써 버렸습니다. 감사!System.NullReferenceException : 개체 참조가 개체의 인스턴스로 설정되지 않았습니다.

데이터 레이어

public class Data 
{ 
    public static Business anApplicant; 

    static SqlConnection aConnection = null; 

    public static void initializeConnection(SqlConnection aDbConnection) 
    { 
     aConnection = aDbConnection; 
     aConnection.Open(); 
    } 

    // Method for Inserting Applicant information into the database 
    public static void InsertApplicant() 
    { 
     try 
     { 
      SqlCommand myCommand = new SqlCommand("INSERT INTO Applicant (FirstName, LastName, Address, City, State, Zip, Phone, Gender, BirthDate)" + 
       "VALUES ('" + anApplicant.FName + "', '" + anApplicant.LName + "', '" + anApplicant.Address + "', '" + anApplicant.City + "', '" + anApplicant.State + 
       "', '" + anApplicant.Zip + "', '" + anApplicant.Phone + "', '" + anApplicant.Gender + "', '" + anApplicant.BirthDate + "')", aConnection); 

      if (aConnection.State == ConnectionState.Closed) 
       aConnection.Open(); 

      myCommand.ExecuteNonQuery(); 

      aConnection.Close(); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.ToString(), "Error"); 
     } 
    } 
} 

비즈니스 계층

public partial class PolicyHomeForm : Form 
{ 
    public PolicyHomeForm() 
    { 
     InitializeComponent(); 
    } 

    private void PolicyHomeForm_Load(object sender, EventArgs e) 
    { 

    } 

    public void saveButton_Click(object sender, EventArgs e) 
    { 
     Data.anApplicant = new Business(txtFirstName.Text, txtLastName.Text, txtAddress.Text, txtCity.Text, comboState.Text, txtZip.Text, txtPhone.Text, 
      comboGender.Text, txtBirthDate.Text); 

     //Data.aVehicle = new Vehicle(comboMake.Text, txtModel.Text, txtYear.Text, txtDesc.Text); 

     Data.InsertApplicant(); 

     //Data.InsertVehicle(); 
    } 
} 
+2

SQL 인젝션 취약점이 있습니다. – SLaks

답변

0

null 참조 예외는 null 객체 또는 초기화되지 않은 개체에 액세스하려고 시도하는 오류가

public class Business 
{ 
    SqlConnection aConnection = new SqlConnection("Data Source=zac2424-HP; Initial Catalog=Final; Trusted_Connection=True;"); 

    public void initializeConnection() 
    { 
     Data.initializeConnection(aConnection); 
    } 

    private string policyNumber; 
    private string fName; 
    private string lName; 
    private string address; 
    private string city; 
    private string state; 
    private string zip; 
    private string phone; 
    private string gender; 
    private string birthDate; 

    public Business(string fName, string lName, string address, 
     string city, string state, string zip, string phone, string gender, string birthDate) 
    { 
     FName = fName; 
     LName = lName; 
     Address = address; 
     City = city; 
     State = state; 
     Zip = zip; 
     Phone = phone; 
     Gender = gender; 
     BirthDate = birthDate; 
    } 

    public Business() 
    { 
    } 

    // Applicant Get and Set Method 
    public string PolicyNumber 
    { 
     get { return policyNumber; } 
     set { policyNumber = value; } 
    } 

    public string FName 
    { 
     get { return fName; } 
     set { fName = value; } 
    } 

    public string LName 
    { 
     get { return lName; } 
     set { lName = value; } 
    } 

    public string Address 
    { 
     get { return address; } 
     set { address = value; } 
    } 

    public string City 
    { 
     get { return city; } 
     set { city = value; } 
    } 

    public string State 
    { 
     get { return state; } 
     set { state = value; } 
    } 

    public string Zip 
    { 
     get { return zip; } 
     set { zip = value; } 
    } 

    public string Phone 
    { 
     get { return phone; } 
     set { phone = value; } 
    } 

    public string Gender 
    { 
     get { return gender; } 
     set { gender = value; } 
    } 

    public string BirthDate 
    { 
     get { return birthDate; } 
     set { birthDate = value; } 
    } 

    string premium = ""; 
    public string Premium 
    { 
     get { return premium; } 
     set { premium = value; } 
    } 
} 

프리젠 테이션 레이어. 이 경우 SQLConnection 전달 매개 변수는 null입니다. 솔루션의

하나는이 경우

또는 다른 솔루션은 데이터 액세스 계층에서 연결을 배치하는 것입니다 아닌 경우 SQLConnection에게, 당신의 생성자에서 SqlConnection 개체를 초기화하고 매개 변수로 연결 문자열을 전달하는 것입니다 , 귀하의 데이터 클래스에서 생성자를 만들고 거기에 연결을 엽니 다. 내 휴대 전화에서이 게시하도록하겠습니다 때문에 많은 도움이되지 수

죄송합니다 ^^ ...이 코드는 결코 Data.initializeConnection 그렇게 Data.aConnection는 항상 null의 호출하지

+0

예, 감사합니다. initializedConnections를 제거하고 문제를 해결 한 데이터 계층에 연결을 넣습니다. – user1869749

2

도움이되기를 바랍니다.

관련 문제