なぜ私が問題を抱えているのか理解できませんでした
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);
}
}