Hi I am trying to get some data from a simple database that I have created using a separate control on the main form. This program runs however when I try to view the main form in designer view I get this exception "ExecuteReader: Connection property has not been initialized."
This is my code for the GetControl:
namespace Controls
{
public partial class GetControl : UserControl
{
private SqlCeConnection mConnection = null;
private SqlCeDataReader dataReader = null;
public SqlCeConnection Connection
{
set { mConnection = value; }
}
public GetControl()
{
InitializeComponent();
}
private void GetControl_Load(object sender, EventArgs e)
{
getData();
addData();
}
private void getData()
{
SqlCeCommand command = new SqlCeCommand("SELECT FirstName, LastName FROM Contacts", mConnection);
dataReader = command.ExecuteReader();
}
private void addData()
{
while (dataReader.Read())
{
contactList.Items.Add(dataReader.GetString(0) + " "
+ dataReader.GetString(1));
}
}
}
}
And my main form:
namespace Phonebook_Application
{
public partial class Phonebook_MainForm : Form
{
SqlCeConnection con = new SqlCeConnection("Data Source=Phonebook.sdf;Persist Security Info=false;");
public Phonebook_MainForm()
{
InitializeComponent();
con.Open();
getControl1.Connection = con;
}
}
}
This all seems to work fine if I call getData() and addData() in a button handler but not in form load. The project still runs without errors I just get that exception when trying to view designer view in the main form.