0

I am getting an error at my aConnection.Open() line and cannot figure out why. This exact code was working and then i tried add basically the same code to another class to Insert values into a Vehicle table and i started getting this error. So i deleted everything back to were i had it when it was working and it is still erroring when clicking Save. Any ideas? I have exhausted all resources in finding and solution. Thanks!

Data Layer

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");
        }
    }
}

Business Layer

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; }
    }
}

Presentation Layer

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();
    }
}
1
  • 2
    You have a SQL injection vulnerability. Commented Dec 2, 2012 at 3:49

2 Answers 2

2

Your code never calls Data.initializeConnection so Data.aConnection is always null.

Sign up to request clarification or add additional context in comments.

Comments

0

Null Reference Exception is an error in which you attempt to access a null object or uninitialized object. (In this case the parameter SQLConnection you pass is null).

One of the solution is to initialize the SQLconnection object in your constructor and pass the connection string as the parameter, not the SQLConnection

Or another solution is to place the connection in the Data Access Layer, in this case, make a constructor in your Data class and open the connection there.

Sorry can't help much because I'm posting this from my mobile phone ^^... Hope it helps

1 Comment

Yes, thank you. I removed the initializedConnections and put the connection in the data layer which corrected the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.