我不知道为什么我有问题
btn.Text = comando.ExecuteScalar().ToString() ;
陈述。如果有人解释我为什么会遇到问题(顺便说一句,我是新手),我该如何纠正它。谢谢。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
SqlCommand comando = new SqlCommand();
SqlConnection conn = new SqlConnection(@"server=.\SQLEXPRESS;Initial Catalog=try;Integrated Security=True;Pooling=False");
comando.Connection = conn;
conn.Open();
int NumOfButtons = 12;
int loc = 20;
int k = 5;
for (int i = 1; i <= NumOfButtons; i++)
{
Button btn = new Button();
ListBox lst = new ListBox();
{
lst.Location = new Point(4, 4);
btn.Size = new Size(60, 20);
btn.Tag = i;
comando.CommandText = "select ProductName from Products where productID = " + btn.Tag;
btn.Text = comando.ExecuteScalar().ToString() ; // here error occurs why?
btn.Location = new Point(k, loc);
}
loc += 20;
if (i > 6)
{
if (loc == 160)
{
loc = 20;
}
k = 65;
btn.Location = new Point(k, loc);
}
panel1.Controls.Add(btn);
}
}