0

I am working on displaying a students timetable based on possible Module choices. (See Screenshot) Currently I am displaying all available modules in a gridview which the student can select by using check boxes. I have already created the insert query to insert their selection to the database. (2)

However, each time a check box is selected I want to retrieve the 'ModuleId' to add to a SELECT query which displays a timetable of their selected modules. (1) So if a user selects 3 check boxes the 'ModuleId' from each row selected will be passed into the SELECT query.

GridView Screenshot

Below is my method which enables the timetable for specific a ModuleId to be displayed:

 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 " + timeslotInt.ToString() + " BETWEEN ClassScheduleStartTimeId and ClassScheduleEndTimeId";
            SqlDataReader dr = newCmd.ExecuteReader();
            while (dr.Read())
            {
                String current = "<div class='slot' " + (!dr.IsDBNull(6) ? "style=\"background-color: " + dr.GetString(6) + ";\"" : "") + ">";
                current += "<div class='line1'>" + dr.GetString(0) + "&nbsp;" + dr.GetString(1) + "</div>";// +"<br />";
                current += "<div class='line2'>" + dr.GetTimeSpan(2).ToString().TrimEnd('0').TrimEnd('0').TrimEnd(':') + "&nbsp;-&nbsp;" + dr.GetTimeSpan(3).ToString().TrimEnd('0').TrimEnd('0').TrimEnd(':') + "</div>";// +"<br />";
                current += "<div class='line3'>" + dr.GetString(4) + "&nbsp;" + dr.GetString(5) + "</div>";
                current += "</div>";
                modulesList.Add(current);
            }
            conn.Close();
            return modulesList.ToArray();
        }

How would I pass all selected values (ModuleId) from the checkboxes to be used in my above select query as previously for displaying just one module I used ' Module.ModuleId = " + moduleID ' ?

1 Answer 1

1

To complete what you are trying to do efficiently and without big foreach loops you need to change the whole way your user inputs data in to the gridview.

I believe that you should be utilising the GridViews OnRowUpdating Event, When a row is updated, using this event you already have context of which row has changed. e.RowIndex - Gets the index of the row being updated

Utilising this event will require a bit of a redesign of your gridview fields, You will need to add a EditItemTemplate tag in each TemplateField Add a CommandFieldand using OnRowUpdating re-write your update event. Its to much information to post in a answer, This information should help you get started on using the GridView events to their full potential.

On a side note you should use paramerterized queries in your sql.

EDIT

Based on your clarification that you need to retrieve the moduleid for each checkbox, I am going to make a few assumptions so it might not work without some tweaks.

To start with try this:

<asp:TemplateField>
  <ItemStyle HorizontalAlign="Center" Width="40px"></ItemStyle>
<ItemTemplate>
      <asp:CheckBox ID="chkRow" runat="server" ToolTip='<%# Eval("ModuleId") %>' OnCheckedChanged="module_Changed" />
 </ItemTemplate>

Using '<%# Eval("ModuleId") %>' Will populate the tooltip for the checkbox with the ModuleID value during the GridView.DataBind() function. Iam making the assumption that the column name is exactly "ModuleId"

Then in the code behind you can read the tooltip value of the checkbox like this:

protected void module_Changed(object sender, EventArgs e)
{
    // Retrieve the check box ModuleId value to add to SELECT query
    string moduleid = ((CheckBox)sender).ToolTip;
}

Now that when the gridview loads all the checkboxes have a tooltip that is populated with the ModuleID you can easily know which box is for which module by checking the tooltip.

Sign up to request clarification or add additional context in comments.

8 Comments

Many thanks for responding in detail. The insert 'moduleupload' part works fine and I have no problems with that. i just posted it up to give a better insight into how it was coded. (I will parameterise the queries). @Nick I just need to get the ModuleId value of for each tickbox that has been selected. The selection here will not be used for the insert but only to be used to display the correct Moduleid in the time table in real time. So as the user selects a ModuleId which is equal to 1 then ModuleId = 1 is used in the select query for the timetable.. etc Does that make sense.
@DavidCraven So i think i get you now, Check my edit. I think that the quickest way to relate your checkboxes to modules IDs is to tell the gridview to populate the chk tooltips with the IDs when the data populates the grid.
That is a smart idea! Many thanks I will give this a go soon!! Thanks for all your help :)
This worked. The only change I had to make was "AutoPostback=true" in order for it to work. Thanks. I also updated the above code to include a select query which I use to display the timetable for a ModuleId
Glad I could help mate, have fun.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.