大家好
con.Close();
int buttoncounter= -1;
{
SqlCommand cmd = new SqlCommand("select count(*) from buttons", con);
con.Open();
buttoncounter = Convert.ToInt32(cmd.ExecuteScalar()) + 1;
con.Close();
}
我计算数据库中有多少行
for (int i = 1; i < buttoncounter; i++)
{
var buttonmenu = new Button
{
HeightRequest = 100,
WidthRequest = 100,
Margin = 5,
CornerRadius = 15,
BackgroundColor = Color.FromRgb(192, 192, 192),
};
con.Close();
buttonmenu.Clicked += butonmenu;
buttons.Children.Add(buttonmenu);
if (con.State.ToString() == "Open")
{
}
else
{
con.Open();
}
SqlCommand getir = new SqlCommand("select * from butonlar where id = '" + i.ToString() + "'", con);
SqlDataReader dr = getir.ExecuteReader();
while (dr.Read())
{
buttonmenu.Text = dr.GetValue(0).ToString();
con.Close();
break;
}
我使用这种方法创建按钮,让我们以为buttons
中有4个值。如果用户删除第2行,则它将为1,3,4,程序将创建4个按钮,而2将为2。按钮的名称为空,我应该防止创建空按钮
id不是pk
答案 0 :(得分:0)
如果要防止在Text为null时创建按钮,则需要在创建按钮之前重新排列逻辑以执行查询
var value = dr.GetValue(0).ToString();
// check is value is empty before creating button
if (!string.IsNullOrEmpty(value)) {
var buttonmenu = new Button
...
}
答案 1 :(得分:0)
我解决了这个问题
代码已更新:
SqlCommand getirme = new SqlCommand("select * from butonlar where id = '"+i.ToString()+"'", baglanti);
SqlDataReader okumasa = getirme.ExecuteReader();
while (okumasa.Read())
{
value = okumasa.GetValue(2).ToString();
break;
}
// check is value is empty before creating button
if (value == i.ToString())
{
var buttonmenu = new Button
{
if (value == i.ToString())
之前,已设置为检测哪个记录为空,但是现在,它会比较id
和i
和如果与{>匹配” ,则会创建按钮。