从数据库添加到下拉列表时出错

本文关键字:出错 下拉列表 数据库 添加 | 更新日期: 2023-09-27 18:02:06

我有一个面板,只有在点击GridView的编辑按钮时才可见。

在该面板是一个形式与DropDownListTextBox,你可以写一个数字,并将其添加到ListBox

当我单击按钮Finalize时,所有想要的数字都添加到ListBox中,正在向数据库添加数据。在Gridview中,我从数据库中连接了名字,因为我分别有Lastname和Firstname。

为了更容易,我选择在DropDownList中添加数据库,当我单击具有特定ID的编辑按钮时。

当编辑按钮被点击时,这个错误显示:

由于对象的当前状态,操作无效。

在我添加DropDownList的行中。我验证了我的数据库,我的表的名称,所有和是正确的。我甚至只尝试了firstname,而不是concatinate,它也会犯同样的错误。我不知道怎么了。我希望你能帮助我。这是出现错误的代码。

protected void btnEditSO_Click(object sender, EventArgs e)
{
    panelSO.Visible = true;
    btnFinalizeSO.Text = " Update ";
    Button but = (Button)sender;
    GridViewRow grid = (GridViewRow)but.NamingContainer;
    string select_sql_SOddl = "SELECT ID, (LASTNAMER | | ' ' | | FIRSTNAMER ) AS REFERENTNAME FROM REFERENT_SHIPPING WHERE ID=" + grid.Cells[14].Text;
    using (OracleConnection con = new OracleConnection(ConfigurationManager.ConnectionStrings["DBCS"].ToString()))
    {
        con.Open();
        OracleCommand cmd1 = new OracleCommand(select_sql_SOddl, con);
        OracleDataReader dr1 = cmd1.ExecuteReader();
        while (dr1.Read())
        {
            ddlReferentShip.Items.Add(dr[0].ToString());
         // ddlReferentShip.Items.Add(dr["REFERENTNAME"].ToString());
         //   ddlReferentShip.DataSource = dr1;
        //    ddlReferentShip.DataTextField = dr1["REFERENTNAME"].ToString();
        //    ddlReferentShip.DataValueField = dr1["ID"].ToString();               
        //    ddlReferentShip.DataBind();
        }
    }
}

从数据库添加到下拉列表时出错

您正在检查dr1.Read()

和读取dr[0].Tostring()

也尝试在添加数据之前清除列表。如果你需要显示名称

索引应该是1

应该是

 while (dr1.Read())
        {
            ddlReferentShip.Items.Add(dr1[1].ToString());
        }

我猜ID是数字在查询你传递作为.Text

试试这个

string select_sql_SOddl = "SELECT ID, (LASTNAMER | | ' ' | | FIRSTNAMER ) AS REFERENTNAME 
FROM REFERENT_SHIPPING WHERE ID=" + Convert.ToInt32(grid.Cells[14].Text);

也尽量使用参数化查询来避免SQL注入

dr1作为数据读取器时,为什么要从dr设置ddl项?

也许你可以试试这个:

protected void btnEditSO_Click(object sender, EventArgs e)
{
    panelSO.Visible = true;
    btnFinalizeSO.Text = " Update ";
    Button but = (Button)sender;
    GridViewRow grid = (GridViewRow)but.NamingContainer;
    string select_sql_SOddl = "SELECT ID, (LASTNAMER | | ' ' | | FIRSTNAMER ) AS REFERENTNAME FROM REFERENT_SHIPPING WHERE ID=" + grid.Cells[14].Text;

    using (OracleConnection con = new OracleConnection(ConfigurationManager.ConnectionStrings["DBCS"].ToString()))
    {
        con.Open();
        OracleCommand cmd = new OracleCommand(select_sql_SO, con);
        OracleCommand cmd1 = new OracleCommand(select_sql_SOddl, con);
        OracleDataReader dr = cmd.ExecuteReader();
        OracleDataReader dr1 = cmd1.ExecuteReader();
        int i=0;
        while (dr1.Read())
        {
            ddlReferentShip.Items.Add(dr1[1].ToString());
            i++;
         // ddlReferentShip.Items.Add(dr["REFERENTNAME"].ToString());
         //   ddlReferentShip.DataSource = dr1;
        //    ddlReferentShip.DataTextField = dr1["REFERENTNAME"].ToString();
        //    ddlReferentShip.DataValueField = dr1["ID"].ToString();               
        //    ddlReferentShip.DataBind();
        }
    }
}