我正在尝试将数据从一种形式传递到另一种形式。已从数据库成功检索数据,但程序在设备上部署时抛出Null引用异常错误。 我的代码
form 1
:
if (comboBox1.SelectedItem.ToString() == "Select by Picture")
{
MySqlConnection con = new MySqlConnection("server=******;port= *****;database=********;User Id=root;Password=;");
MySqlDataAdapter d_a = new MySqlDataAdapter("Select Picture From sampledata", con);
DataTable d_t = new DataTable();
d_a.Fill(d_t);
int count = d_t.Rows.Count;
PictureSelection ps = new PictureSelection();
if (count > 0)
ps.Storelist.Items.Clear();
for (int i=0 ; i <count; i++)
{
ps.Storelist.Items.Add(d_t.Rows[i][0].ToString());
}
ps.ShowDialog();
}
else if (comboBox1.SelectedItem.ToString() == "Select by Artist Name")
{
MySqlConnection con = new MySqlConnection("server=*****;port= ****;database=*****;User Id=root;Password=;");
MySqlDataAdapter d_a = new MySqlDataAdapter("Select Artist Name From sampledata", con);
DataTable d_t = new DataTable();
d_a.Fill(d_t);
int count = d_t.Rows.Count;
PictureSelection ps = new PictureSelection();
if (count > 0)
ps.Storelist.Items.Clear();
for (int i = 0; i < count; i++)
{
ps.Storelist.Items.Add(d_t.Rows[i][0].ToString());
}
ps.ShowDialog();
}
}
Form2
:
private void PictureSelection_Load(object sender, EventArgs e)
{
Storelist.DataSource = mainmn.d_t;
}
任何帮助将不胜感激。
答案 0 :(得分:0)
您必须从form1:
设置数据表ps.Storelist.Datasource = d_t;
ps.ShowDialog();
因为form2没有datatable的定义,所以发生错误。