【发布时间】:2018-04-14 04:38:55
【问题描述】:
我的 SQL 语句无法将下拉列表选择中的值传递到我的网格视图。我用特定的日期测试了我的 SQL 语句 Serve Management Studio,它可以工作,但它不适用于我的下拉列表中的选择值。我如何将值传递给 Gridview?感谢您的帮助,我是 asp.net 世界的新学生。
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:";
}
}
}
}
【问题讨论】:
-
DropDownListDay.SelectedValue 应该是数值吗?还是像“星期一”或类似的东西
-
数据类型是日期...'2017-07-05'
-
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治疗 ON patientreservation.treatmentID =treatment.treatmentID) WHERE patientreservation.day = '2017-07-05' 这在我测试时工作正常,但是当我添加所选值时我开始遇到问题
-
日期周围的那些单引号不在您的代码中。
-
我已经尝试了几次单引号但没有运气......