2011-06-03 89 views
0

我的数据记录表有以下数据:如果我请从下拉列表中的相应值应在文本框中显示值

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下拉列表即线和移位和一个文本框存储日期,如果用户从日历选择日期和两个文本框来存储工厂和操作员名称的值。

为例如,如果从压延用户选择线和移位从下拉列表和日期的相应plantname和operatorname应当在文本框中为前 可以dispalyed如果从下拉列表和SHIFT1用户选择第1行从下拉列表和日期具有25/05/2011两个文本框应该dispaly值有plant1和操作员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(); 
     } 
    } 

,但它没有显示。

+0

贵DataTable包含数据的详细信息? – 2011-06-03 05:10:09

+0

你可以请尝试使用SelectedValue而不是SelectedItem? – Deepesh 2011-06-03 05:10:19

+0

@abdul:雅与如果从下拉列表中选择刚刚行一个下拉列表和IM不检查日期还有其dispalying数据的到来,但是当我想根据路线,转向选择数据和日期的不来的Web应用程序。 – Sweety 2011-06-03 06:53:14

回答

2

的问题是在你的SQL Query where Clause,您正在设置值走错了路。

应该ddlline.SelectedItem.Value,而不是ddlline.SelectedItem因为ddlline.SelectedItem返回listitem,但你需要的SelectedValue,它是下拉不变的情况下ddlshift

+0

我想BT的NT cumng .. – Sweety 2011-06-03 06:03:35

+0

你可以尝试Text属性,当你在课堂上需要Shiftname。 ddlshift.SelectItem.Text – 2011-06-03 06:07:21

+0

我应该为第一个dropdownlist1还是dropdownlist2编写此代码。 – Sweety 2011-06-03 07:42:09

1

尝试@Muhammad阿赫塔尔建议,但我也能看到你不是使用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); 
1

必须使用下拉列表的文本改变事件来检测用户何时选择从下拉列表的值下拉列表,并执行代码以获得在该事件中

相关问题