我已成功创建了一个时间表,该时间表显示学生和教职员的每周课程,每个课程使用会话变量; StudentId和StaffId分别为。但是我想以相同的方式显示模块的时间表,所以我使用了级联下拉列表,允许用户先选择一个课程,然后使用onSelectedIndexChange显示可用的模块(图1)
下拉列表名为ddlModule,因此当用户选择模块时,他们选择的是ModuleId。我想将此值ddlModuleId传递给查询,该查询在图2所示的App_Code / DBAccess.cs中定义。
我已尝试过此字符串转换,但会引发错误。 (图3)
string module_ID = Convert.ToString(ddlModule.text);
我有硬编码
string Module_ID = "6";
显示选择模块时的外观
图1. - ddlModule_SelectedIndexChanged
protected void ddlModule_SelectedIndexChanged(object sender, EventArgs e)
{
ddlModule.Items.Clear();
ddlModule.AppendDataBoundItems = true;
String strConnString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
String strQuery = "select ModuleId, ModuleCode from Module where CourseId=@CourseId";
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.Parameters.AddWithValue("@CourseId", ddlCourse.SelectedItem.Value);
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
cmd.Connection = con;
try
{
con.Open();
ddlModule.DataSource = cmd.ExecuteReader();
ddlModule.DataTextField = "ModuleCode";
ddlModule.DataValueField = "ModuleId";
ddlModule.DataBind();
if (ddlModule.Items.Count > 1)
{
ddlModule.Enabled = true;
}
else
{
ddlModule.Enabled = false;
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}
}
图2. - DBAccess.cs
public String[] getModulesAtCurrentSlot(int timeslotInt, String moduleID, String Day)
{
List<String> modulesList = new List<string>();
if (conn.State.ToString() == "Closed")
{
conn.Open();
}
SqlCommand newCmd = conn.CreateCommand();
newCmd.Connection = conn;
newCmd.CommandType = CommandType.Text;
newCmd.CommandText = "SELECT DISTINCT Module.ModuleCode,ClassType.ClassTypeName,Convert(time,Class.StartTime), Convert(time,Class.EndTime),Building.BuildingName,RoomCode.RoomCode,Class.Color" +
" FROM Class INNER JOIN Module ON Class.ModuleId = Module.ModuleId INNER JOIN RoomCode ON Class.RoomCodeId = RoomCode.RoomcodeId INNER JOIN Building ON RoomCode.BuildingId = Building.BuildingId INNER JOIN Days ON Class.DayId = Days.DayID INNER JOIN ClassType ON Class.ClassTypeId = ClassType.ClassTypeId WHERE " +
" Module.ModuleId = " + moduleID + " AND Convert(Date,StartTime) = '" + Day + "' AND ClassScheduleStartTimeId = " + timeslotInt.ToString();
SqlDataReader dr = newCmd.ExecuteReader();
while (dr.Read())
{
//Module.ModuleCode,ClassType.ClassTypeName,Convert(time,Class.StartTime), Convert(time,Class.EndTime),Building.BuildingName,RoomCode.RoomCode,Class.Color
String current = "<div class='slot'>";
current += "<div class='line1'>" + dr.GetString(0) + " " + dr.GetString(1) + "</div>";// +"<br />";
current += "<div class='line2'>" + dr.GetTimeSpan(2).ToString().TrimEnd('0').TrimEnd('0').TrimEnd(':') + " - " + dr.GetTimeSpan(3).ToString().TrimEnd('0').TrimEnd('0').TrimEnd(':') + "</div>";// +"<br />";
current += "<div class='line3'>" + dr.GetString(4) + " " + dr.GetString(5) + "</div>";
current += "</div>";
modulesList.Add(current);
}
conn.Close();
return modulesList.ToArray();
}
图4 - 使用ModuleId =&#34; 6&#34;
错误消息是:
Incorrect syntax near '='.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near '='.
Source Error:
Line 117: " FROM Class INNER JOIN Module ON Class.ModuleId = Module.ModuleId INNER JOIN RoomCode ON Class.RoomCodeId = RoomCode.RoomcodeId INNER JOIN Building ON RoomCode.BuildingId = Building.BuildingId INNER JOIN Days ON Class.DayId = Days.DayID INNER JOIN ClassType ON Class.ClassTypeId = ClassType.ClassTypeId WHERE " +
Line 118: " Module.ModuleId = " + moduleID + " AND Convert(Date,StartTime) = '" + Day + "' AND ClassScheduleStartTimeId = " + timeslotInt.ToString();
Line 119: SqlDataReader dr = newCmd.ExecuteReader();
Line 120: while (dr.Read())
Line 121: {
Source File: c:\Users\40104198\Desktop\CIT\Qsis\DBAccess.cs Line: 119
Stack Trace:
[SqlException (0x80131904): Incorrect syntax near '='.]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +2436598
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +5731392
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +628
System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +3731
System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() +58
System.Data.SqlClient.SqlDataReader.get_MetaData() +89
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +379
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds, Boolean describeParameterEncryptionRequest) +2026
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) +375
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +53
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +240
System.Data.SqlClient.SqlCommand.ExecuteReader() +99
Qsis.DBAccess.getModulesAtCurrentSlot(Int32 timeslotInt, String moduleID, String Day) in c:\Users\40104198\Desktop\CIT\Qsis\DBAccess.cs:119
Qsis.Students.ModuleTimetable.Page_Load(Object sender, EventArgs e) in c:\Users\40104198\Desktop\CIT\Qsis\Students\ModuleTimetable.aspx.cs:140
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
System.Web.UI.Control.OnLoad(EventArgs e) +95
System.Web.UI.Control.LoadRecursive() +59
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2952
答案 0 :(得分:-1)
我能够通过url上的查询字符串传递ModuleId,因此我将拥有一个用户选择模块的页面,然后传递通过查询字符串选择的ModuleId。我会在一页上喜欢这一切,但这是妥协。
String module_ID = "2";
if (Request.QueryString["module"] != null)
{
module_ID = Request.QueryString["module"];
}else{
Response.Write("Missing ?module=XX from url :(");
Response.End();// EndRequest;
}