I want to make a windows forms application with two forms: form1 and form2, form2 displays a 3D chart with a local SQL server connection to make a database as it's DataSource.
I gave two combobox in form1 which will display the columns in the data so that the user can select the columns they want and then click a button on form1 which will open the form2 with the 3D chart made from taking the columns as it's x and y axis.
For this I made a DataTable and added the data, then provided the DataTable columns names to both the combobox then I made an instance of form2 inside form1 and passed the combobox items as strings to the string variables created in form2.
But it's showing the exception 'Object reference not set to an instance of an object.' How do I fix this error?
//Form1
public void GetColumns()
{
DataTable dt=new DataTable();
data d = new Dataaccess.data();
dt = (DataTable)d.GetData();
foreach (DataColumn dc in dt.Columns)
{
Xaxis.Items.Add(dc.ColumnName);
Yaxis.Items.Add(dc.ColumnName);
}
}
Form2 frm = new Form2();
public void Xaxis_SelectedIndexChanged(object sender, EventArgs e)
{
frm.xaxis = Xaxis.SelectedItem.ToString();
}
public void Yaxis_SelectedIndexChanged(object sender, EventArgs e)
{
frm.yaxis = Yaxis.SelectedItem.ToString();
}
public void Loadchart_Click(object sender, EventArgs e)
{
this.Hide();
frm.Show();
}
//Form2
public partial class Form2 : Form
{
public string xaxis;
public string yaxis;
public Application()
{
InitializeComponent();
FillData();
}
public void FillData()
{
object dt;
data d = new Dataaccess.data();
dt = d.GetData();
chart1.DataSource = dt;
chart1.Series[0].LegendText = xaxis;
chart1.Series[0].IsVisibleInLegend = true;
chart1.Series[0].XValueMember = xaxis;
chart1.Series[0].YValueMembers = yaxis;
}