学生用とスタッフ用のセッション変数を使用して週単位のクラスを表示するためのタイムテーブルを作成しました。それぞれStudentIdとStaffId。しかし、私は同じ方法でモジュールのタイムテーブルを表示したいので、まずユーザーがコースを選択してからonSelectedIndexChange(図1)を使用して利用可能なモジュールを表示できるようにカスケードドロップダウンリストを使用しました(図1)int値を文字列に変換しない
ドロップダウンリストはddlModuleと呼ばれ、ユーザーがモジュールを選択するとModuleIdが選択されます。この値ddlModuleIdを、図2に示すApp_Code/DBAccess.csで定義されているクエリに渡したいとします。
この文字列変換を試しましたが、エラーが発生します。 (図3)
string module_ID = Convert.ToString(ddlModule.text);
私はモジュールは図1
を選択したときに、それがどのように見えるべきかを示すために
string Module_ID = "6";
をハードコードしている -
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 [email protected]";
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();
}
}
図をddlModule_SelectedIndexChanged 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();
}
図3 - モジュールID = "6"
図4スローエラー:
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
ここにエラーをコピーして貼り付けてください。 –
コードを[最小限の完全で検証可能な例]に縮約することを検討してください(http://stackoverflow.com/help/mcve) – Psi
'string Module_ID =" 6 "'最後に ';'が必要です実際の問題を引き起こしています)。 – stybl