Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从上到下移动项目时保存在SQL Server中的列表框项目,反之亦然_C#_Windows Forms Designer - Fatal编程技术网

C# 从上到下移动项目时保存在SQL Server中的列表框项目,反之亦然

C# 从上到下移动项目时保存在SQL Server中的列表框项目,反之亦然,c#,windows-forms-designer,C#,Windows Forms Designer,在Windows窗体中 我有一个列表框和两个上下按钮。。单击其中一个按钮时,列表项将根据单击的按钮向上或向下移动 我的问题是,当我单击向下按钮时,listItem将向下移动,同时它将在运行时更新为listitems。。我尝试了很多,我的代码如下所示 private void btndown_Click(object sender, EventArgs e) { //string myNextItem1 = frm2lstbx.Items[frm2lstbx.SelectedIndex +

在Windows窗体中

我有一个列表框和两个上下按钮。。单击其中一个按钮时,列表项将根据单击的按钮向上或向下移动

我的问题是,当我单击向下按钮时,listItem将向下移动,同时它将在运行时更新为listitems。。我尝试了很多,我的代码如下所示

private void btndown_Click(object sender, EventArgs e)
{
    //string myNextItem1 = frm2lstbx.Items[frm2lstbx.SelectedIndex + 1].ToString();

    string globalNextItem1 = "";

    {
        int a = frm2lstbx.Items.Count - 1;

        if (frm2lstbx.SelectedItem == null)
        {
            MessageBox.Show("Please select an Item");
        }
        else if (frm2lstbx.SelectedIndex == a)
        {
            MessageBox.Show("No Items to move Lower side");
        }
        else
        {
            int i = 0;
            i = frm2lstbx.SelectedIndex;
            string currItem = "";
            string nextItem = "";
            globalNextItem1= frm2lstbx.Items[i + 1].ToString();
            currItem = frm2lstbx.SelectedItem.ToString();

            frm2lstbx.Items.Insert(i, globalNextItem1.ToString());
            MyDAL.UpdateData1(currItem, globalNextItem1);
            MyDAL.UpdateData1(globalNextItem1, currItem);
            frm2lstbx.Items.RemoveAt(i + 2);
         }
     }
  }
在MYDAL类中,我有以下SQL Server中Updatedata的方法

public static void UpdateData1(string UpdatedItem, string OldItem)
{
        string strSql = @" Update tbl_srs set [Description]='" + UpdatedItem + "' where [Description]='" + OldItem + "'";

        Utils.ExecuteSql(strSql);
}
注意:Utils是另一个类库,它包含执行sql命令等的方法

我的问题是:SQL Server正在正确更新所选项目下面的行。。但所选项目将覆盖同一项目。。。这就是为什么整个列表框更新为所选项目的相同值

例如:我已将I=0索引的选定项

当我单击“向下”按钮时,控件将转到I=1,它现在将选中项,因此在SQL Server中,我使用I=0项更新了I=1索引项。它已正确更新


i=0后,索引项可能无法正确更新。它会覆盖i=0索引项中的相同值。

您的sql受到sql注入的影响,请考虑修复它。参数化查询可以帮助您。请告诉我清晰的描述…@peer