Hi,
I have a webform with HTML and coded in vb.net and is connected to SQL table.
The webform have a grid that is data bound to a sql table called Systems, It also have tickboxes.
<asp:GridView ID="GVConSystem" runat="server" AutoGenerateColumns="False" DataKeyNames="SystemID" DataSourceID="systems" Width="409px">
<Columns>
<asp:BoundField DataField="SystemID" HeaderText="SystemID" SortExpression="SystemID" Visible="False" />
<asp:BoundField DataField="SystemName" HeaderText="System Name" SortExpression="SystemName" />
<asp:TemplateField HeaderText="To add">
<ItemTemplate>
<asp:CheckBox ID="CheckBoxToadd" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Added">
<ItemTemplate>
<asp:CheckBox ID="CheckBoxAdded" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Archive">
<ItemTemplate>
<asp:CheckBox ID="CheckBoxArchive" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The webform also have textboxes such as Localcode and a hiddenfield ID.
What i am trying to do is when the Gridview is ticked for each system, it saves the data to a diferent table called NewRequestingSystemandUser
SELECT [RequestedSystemID] = autonumber
,[SystemID] = from gridview
,[RequestedFor] = Hiddenfield ID
,[RequestedForCode] Localcode Textbox
,[RequestedDate] autodate using GET DATE()
,[ToAdd] = tickbox from gridview
,[ToAddBy] = User from webform
,[ToAddDate] = amend date
,[Added] = tickbox from gridview
,[AddedDate] = Amend date
,[AddedBy] = User from webform
,[Archived] = tickbox from gridview
,[ArchivedBy] = user from webform
,[ArchivedDate] = amend date
FROM [ReferenceFileIndex].[dbo].[NewRequestingSystemandUser]
I am not able to figure out how I can save the data with checkboxes using vb.net?
Private Sub InsertData(systemID As Integer, toadd As Boolean, added As Boolean, archive As Boolean)
Dim sql As String = "usp_NewInsertEditSystemData"
Dim constr = ConfigurationManager.ConnectionStrings("Database_Connection_Lookups").ConnectionString
Try
Using conConsultantGridUpdate As New SqlConnection(constr)
conConsultantGridUpdate.Open()
Using cmdInsertConsultantGridUpdate As New SqlCommand(sql, conConsultantGridUpdate) With {
.CommandType = CommandType.StoredProcedure
}
With cmdInsertConsultantGridUpdate.Parameters
.AddWithValue("@RequestedSystemID", HFIDconsys.Value)
.AddWithValue("@SystemID", systemID)
.AddWithValue("@RequestedFor", HFIDcon.Value)
.AddWithValue("@RequestedForCode", txtcongmc.Text)
.AddWithValue("@RequestedDate", DateTime.Now)
.AddWithValue("@Toadd", toadd)
.AddWithValue("@ToAddBy", HFconuser.Value)
.AddWithValue("@ToAddDate", DateTime.Now)
.AddWithValue("@Added", added)
.AddWithValue("@AddedBy", HFconuser.Value)
.AddWithValue("@Archive", archive)
.AddWithValue("@ArchivedBy", HFconuser.Value)
End With
' Execute the stored procedure (either insert or update)
Dim rowid As Integer = cmdInsertConsultantGridUpdate.ExecuteNonQuery()
'If rowid > 0 Then
' Response.Redirect("Main.aspx")
'Else
' Response.Redirect("ErrorPage.aspx")
'End If
End Using
End Using
Catch ex As Exception
' Handle the exception here (e.g., display error message)
End Try
End Sub
Protected Sub BtnUpdateGrid_Click(sender As Object, e As EventArgs) Handles BtnUpdateGrid.Click
For Each row As GridViewRow In GVConSystem.Rows
If row.RowType = DataControlRowType.DataRow Then
Dim systemID As Integer = Convert.ToInt32(GVConSystem.DataKeys(row.RowIndex).Value)
Dim chkToadd As CheckBox = TryCast(row.Cells(2).FindControl("CheckBoxToadd"), CheckBox)
Dim chkAdded As CheckBox = TryCast(row.Cells(3).FindControl("CheckBoxAdded"), CheckBox)
Dim chkArchive As CheckBox = TryCast(row.Cells(4).FindControl("CheckBoxArchive"), CheckBox)
' Insert data into the database using a stored procedure
InsertData(systemID, chkToadd.Checked, chkAdded.Checked, chkArchive.Checked)
End If
Next
End Sub
Please help