Hi kelsen1989,
Replace your code with below.
Protected Sub btnSaveBottom_Click(sender As Object, e As EventArgs) Handles btnSaveBottom.Click
openconnection()
Dim i As Integer
For i = 0 To CheckBoxList1.Items.Count - 1
If CheckBoxList1.Items(i).Selected Then
Dim cmdupdateString As String = "UPDATE [SK_SkillsTracking] SET [LabInitial] = @LabInitial WHERE [UniqueId] = @Id AND [CWID] = @CWId"
Dim cmdupdate As SqlCommand = New SqlCommand(cmdupdateString, conn)
cmdupdate.Parameters.AddWithValue("@LabInitial", InitialString)
cmdupdate.Parameters.AddWithValue("@Id", CheckBoxList1.Items(i).Value)
cmdupdate.Parameters.AddWithValue("@CWId", Request.QueryString("id"))
cmdupdate.ExecuteNonQuery()
cmdupdate.Dispose()
End If
Next
conn.Close()
End Sub
Check the sample example.
HTML
<asp:CheckBoxList ID="CheckBoxList1" runat="server" DataSourceID="SqlDataSource1"
DataTextField="Description" DataValueField="HobbyId">
</asp:CheckBoxList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConString %>"
SelectCommand="SELECT * FROM [Hobbies]"></asp:SqlDataSource>
<asp:Button ID="btnSaveBottom" runat="server" Text="Save" />
Namespaces
C#
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data.SqlClient
Code
C#
protected void btnSaveBottom_Click(object sender, EventArgs e)
{
var conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
conn.Open();
for (int i = 0; i < CheckBoxList1.Items.Count - 1; i++)
{
if (CheckBoxList1.Items[i].Selected)
{
string cmdupdateString = "UPDATE [Hobbies] SET [IsSelected] = @IsSelected WHERE [HobbyId] = @Id";
SqlCommand cmdupdate = new SqlCommand(cmdupdateString, conn);
cmdupdate.Parameters.AddWithValue("@IsSelected", CheckBoxList1.Items[i].Selected);
cmdupdate.Parameters.AddWithValue("@Id", CheckBoxList1.Items[i].Value);
cmdupdate.ExecuteNonQuery();
cmdupdate.Dispose();
}
}
conn.Close();
}
VB.Net
Protected Sub btnSaveBottom_Click(sender As Object, e As EventArgs) Handles btnSaveBottom.Click
Dim conn = New SqlConnection()
conn.ConnectionString = ConfigurationManager.ConnectionStrings("ConString").ConnectionString
conn.Open()
For i = 0 To CheckBoxList1.Items.Count - 1 Step 1
If CheckBoxList1.Items(i).Selected Then
Dim cmdupdateString As String = "UPDATE [Hobbies] SET [IsSelected] = @IsSelected WHERE [HobbyId] = @Id"
Dim cmdupdate As SqlCommand = New SqlCommand(cmdupdateString, conn)
cmdupdate.Parameters.AddWithValue("@IsSelected", CheckBoxList1.Items(i).Selected)
cmdupdate.Parameters.AddWithValue("@Id", CheckBoxList1.Items(i).Value)
cmdupdate.ExecuteNonQuery()
cmdupdate.Dispose()
End If
Next
conn.Close()
End Sub