My datalogging table has following data:
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开发者_如何学JAVA 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
i have provided 2 dropdownlist namely line and shift and one textbox to store date if the user select date from the calender and two textbox to store the value of plant and opearatorname.
for eg if the user select line and shift from the dropdownlist and date from the calender the corresponding plantname and operatorname should be dispalyed in the textbox for ex if the user select line1 from dropdownlist and shift1 from dropdownlist and date has 25/05/2011 the two textbox should dispaly the value has plant1 and operator 1
i have written code has below:
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();
}
}
but it's not displayed.
The problem is in your SQL Query where Clause
, you are setting the value the wrong way.
It should be ddlline.SelectedItem.Value
instead of ddlline.SelectedItem
because ddlline.SelectedItem
returns the listitem
, but you need the SelectedValue
and it is the same case for dropdown ddlshift
Try @Muhammad Akhtar suggestion but I can also see that you're not using the SQL parameters and you code is susceptible to SQL injection attacks. This can easily be avoided and it will make your embedded SQL much prettier.
SELECT distinct plantname, operatorname
FROM datalogging1
WHERE Line = @Line AND date = @Date AND shiftname = @ShiftName
Then, before you execute this statement add the parameters with value.
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);
you must use the dropdownlist text changed event to detect when user select a value from the drop down list and implement your code to get details in that event
精彩评论