I have a CheckBoxList control and update button:
<asp:CheckBoxList ID="moduleselect" runat="server" DataSourceID="semester2" DataTextField="module_name" DataValueField="module_id"></asp:CheckBoxList>
<br />
<asp:Button ID="uploadbutton" runat="server" Text="Choose Modules" OnClick="uploadbutton_Click" CssClass="submitbtn" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" CssClass="submitbtn" />
This contains a list of modules for students to choose and when the upload button is clicked then it should update a database table.
I have the following C#
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
String name = Request.QueryString["studentno"];
username.Text = name;
}
protected void uploadbutton_Click(object sender, EventArgs e)
{
String user = username.Text;
string ConnectionString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection myConnection = new SqlConnection(ConnectionString);
myConnection.Open();
int count = moduleselect.Items.Count;
for (int i = 0; i < count; i++)
{
if (moduleselect.Items[i].Selected)
{
string value = moduleselect.Items[i].Value;
String query = "INSERT INTO students_vs_modules (student_no, module_id) VALUES (@student_no, @module_id)";
SqlCommand myCommand = new SqlCommand(query, myConnection);
myCommand.Parameters.AddWithValue("@student_no", user);
myCommand.Parameters.AddWithValue("@module_id", value);
myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
}
}
However, when I select a number of modules when the upload button is clicked it only updates the initial option selected and not all of them. I'm pretty new to web development so any help would be greatly appreciated.