我无法绑定DropDownlist,我的代码是Sqldatareader。 我没有得到任何输出。我想从数据库中显示值并绑定它。
try
{
SqlDataReader ds = obj.GetDataReader("Select Name, Emp_ID from
EMP_Info where Emp_ID in ( select Emp_ID from EMP_Info where Name ='" +
Session["AG_name"].ToString() + "' except select Emp_ID from RosterWeekly
where WeekStartDate between '" + txtDate.Text + "' and '" +
Convert.ToDateTime(txtDate.Text).AddDays(7).ToShortDateString() + "') Order
By Name");
txempid.DataSource = ds;
txempid.DataValueField = "Emp_ID";
txempid.DataTextField = "Name";
txempid.DataBind();
txempid.Items.Insert(0, new ListItem("--Select Employee Name--",
"0"));
txempid.Items.Insert(1, new ListItem(ds[0].ToString(), "1"));
}
catch (Exception ex)
{
}
// dropdownlist code
<asp:DropDownList Height="35" Width="200" ID="txempid" runat="server"
AutoPostBack="true" OnSelectedIndexChanged ="txempid_SelectedIndexChanged" >
</asp:DropDownList>
答案 0 :(得分:0)
问题似乎与字符串中的datetime参数有关。我不喜欢直接在查询中传递的txtDate.Text。
我建议你在查询中加入几个参数并将它们作为强类型参数传递(我认为obj
是SqlCommand
个对象):
var obj = new SqlCommand("Select Name, Emp_ID from
EMP_Info where Emp_ID in ( select Emp_ID from EMP_Info where Name ='" +
Session["AG_name"].ToString() + "' except select Emp_ID from RosterWeekly
where WeekStartDate between @startDate and @endDate) Order
By Name", conn);
var par1 = new SqlParameter();
par1.DbType = DbType.DateTime;
par1.Value = DateTime.Parse(txtDate.Text);
par1.ParameterName = "@startDate";
var par2 = new SqlParameter();
par2.DbType = DbType.DateTime;
par2.Value = DateTime.Parse(txtDate.Text).AddDays(7);
par2.ParameterName = "@endDate";
obj.Parameters.Add(par1);
obj.Parameters.Add(par2);
SqlDataReader ds = obj.ExecuteReader();
我没有时间在运行时尝试这个,所以也许某些东西需要调整......