【问题标题】:If I select value from dropdownlist the corresponding value should be displayed in the textbox如果我从下拉列表中选择值,则相应的值应显示在文本框中
【发布时间】:2011-06-03 05:02:49
【问题描述】:

我的数据记录表有以下数据:

Name       Shiftname       operatorname         date        Plantname       Line    Machine
Ashwini   Shift1(7-3)     Operator 1    2011-05-24      Plant 1    Line1       mc1
Deepika   Shift2(3-11)  Operator 2  2011-05-24      Plant 2    Line3       mc5
Pradeepa      Shift2(11-7)  Operator 3  2011-05-25      Plant 3    Line5       mc10
Deepika   Shift1(7-3)   Operator 1  2011-05-25      Plant 1    Line1       mc1

如果用户从日历中选择日期,我提供了 2 个下拉列表,即 line 和 shift 以及一个用于存储日期的文本框,以及两个用于存储植物和操作员名称的值的文本框。

例如,如果用户选择行并从下拉列表中移动并从日历中选择日期,则应在文本框中显示相应的工厂名称和操作员名称,例如 如果用户从下拉列表中选择 line1 并从下拉列表中选择 shift1 并且日期为 25/05/2011,则两个文本框应显示值具有植物 1 和操作员 1

我写的代码如下:

protected void ddlline_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("connection string");
        con.Open();
        DataTable dt = new DataTable();
        SqlCommand sqlCmd = new SqlCommand("SELECT distinct plantname,operatorname FROM datalogging1 WHERE Line='" + ddlline.SelectedItem + "'and date='"+txtdate.Text+"'and shiftname='"+ddlshift.SelectedItem+"'", con);
        SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
        sqlDa.Fill(dt);

        if (dt.Rows.Count > 0)
        {
            //Where ColumnName is the Field from the DB that you want to display 
            txtplant.Text = dt.Rows[0]["Plantname"].ToString();
            txtop.Text = dt.Rows[0]["operatorname"].ToString();
        }
    }

但它不显示。

【问题讨论】:

  • 您的 DataTable 是否包含数据?
  • 你能试试用 SelectedValue 代替 SelectedItem 吗?
  • @abdul:ya 它使用一个下拉列表显示数据,如果您从下拉列表中仅选择行,我不会在那里检查日期,但当我想根据行选择数据时,移位和日期不会出现用于网络应用程序。
  • 数据库中是否存在与提供的过滤器匹配的某些数据?

标签: c# .net asp.net sql drop-down-menu


【解决方案1】:

问题在于您的SQL Query where Clause,您设置的值错误。

应该是ddlline.SelectedItem.Value 而不是ddlline.SelectedItem,因为ddlline.SelectedItem 返回listitem,但您需要SelectedValue,下拉ddlshift 也是如此

【讨论】:

  • 你可以试试 Text 属性,因为你需要在课堂上使用 Shiftname。 ddlshift.SelectItem.Text
  • 我应该为第一个 dropdownlist1 还是 dropdownlist2 编写此代码。
  • 你可以为 ddlshift.SelectItem.Text 和 ddlline.SelectedItem.Text 写这个
【解决方案2】:

尝试@Muhammad Akhtar 的建议,但我也可以看到您没有使用 SQL 参数,并且您的代码容易受到 SQL 注入攻击。这很容易避免,它会让你的嵌入式 SQL 更漂亮。

SELECT distinct plantname, operatorname 
FROM datalogging1 
WHERE Line = @Line AND date = @Date AND shiftname = @ShiftName

然后,在执行此语句之前,添加带有值的参数。

sqlCmd.Parameters.AddWithValue("@Line", ddlline.SelectedItem.Value);
// passing the date as text is also a bad idea because it will make your
// date format dependant on culture and language specific settings in both
// the database and application code if you parse the date first and
// pass the value as a DateTime value you eliminate the date format hassle
// that might otherwise occur in the database
sqlCmd.Parameters.AddWithValue("@Date ", txtdate.Text);
sqlCmd.Parameters.AddWithValue("@ShiftName", ddlshift.SelectedItem.Value);

【讨论】:

    【解决方案3】:

    您必须使用下拉列表文本更改事件来检测用户何时从下拉列表中选择一个值并实现您的代码以获取该事件的详细信息

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-12
      • 2015-05-26
      • 1970-01-01
      • 2021-07-01
      相关资源
      最近更新 更多