我的SQL语句在将下拉列表select中的值传递到我的gridview时遇到了问题。我用一个特定的日期测试了我的SQL语句Serve Management Studio,它可以工作,但它不能处理我的下拉列表中的select值。我如何将值传递给Gridview?谢谢你的帮助,我是asp.net世界的新手。
web应用程序的图像:
我的数据库镜像:
public void RefreshDay()
{
SqlConnection conn = new SqlConnection(@"data source =.\sqlexpress; integrated security = true; database = DBdentist");
SqlDataAdapter da = null;
DataSet ds = null;
DataTable dt = null;
string sqlsel = "SELECT Distinct patient.patientID, day from patient, patientreservation where patient.patientID = patientreservation.patientID";
try
{
da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand(sqlsel, conn);
ds = new DataSet();
da.Fill(ds, "myDay");
dt = ds.Tables["myDay"];
DropDownListDay.DataSource = dt;
DropDownListDay.DataTextField = "day";
DropDownListDay.DataValueField = "patientID";
DropDownListDay.DataBind();
DropDownListDay.Items.Insert(0, "Select Day");
}
catch (Exception ex)
{
LabelDay.Text = ex.Message;
}
finally
{
conn.Close();
}
}
protected void DropDownListDay_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownListDay.SelectedIndex != 0)
{
SqlConnection conn = new SqlConnection(@"data source =.\sqlexpress; integrated security = true; database = DbDentist");
SqlDataAdapter da = null;
DataSet ds = null;
DataTable dt = null;
string sqlsel = "SELECT patientreservation.patientID, patient.firstname, patient.lastname, patientreservation.day, patientreservation.hour, treatment.treatment FROM((patientreservation INNER JOIN patient ON patientreservation.patientID = patient.patientID) INNER JOIN treatment ON patientreservation.treatmentID = treatment.treatmentID) WHERE patientreservation.day = " + DropDownListDay.SelectedValue + "";
try
{
da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand(sqlsel, conn);
ds = new DataSet();
da.Fill(ds, "myDay");
dt = ds.Tables["myDay"];
GridViewDay.DataSource = dt;
GridViewDay.DataBind();
}
catch (Exception ex)
{
LabelDay.Text = ex.Message;
}
finally
{
conn.Close();
}
}
else
{
LabelDay.Text = "You choose None:";
}
}
}
}
发布于 2017-11-02 15:23:58
您可以简单地使用以下命令:
WHERE patientreservation.day = '" + DropDownListDay.Text.ToString() + "'";
发布于 2017-11-11 05:54:34
谢谢你的帮助。我什么都试过了
DropDownListDay.DataTextField = "day"; DropDownListDay.DataValueField = "day"
帮上忙了。我的网格视图也有一些问题。我把它删除了,然后做了一个新的,不知何故,这有帮助。
发布于 2017-11-02 11:59:08
dropdownlist未触发事件。试着在你的asp下拉列表标签中把这个AutoPostBack=写成“真”。
https://stackoverflow.com/questions/47062025
复制相似问题