Hi Imar,
Thanks for taking a look at this. I'm still not getting anywhere with it. What's weird is that in the ItemDataBound I need that second SQL query to find out if the checkbox should be checked. It finds the checkboxes fine and marks them correctly. So, in that ItemDataBound it is finding checkbox and marking them correctly.
I tried putting in, "Dim chkAccessLevelFeature As CheckBox = Item.Cells(i).Controls(0)" which is the code I started with. It errors out with:
Specified argument was out of the range of valid values. Parameter name: index
I did some research and found out that that code is now "illegal" in .NET 2.0
The code runs fine in 1.1.
Thanks for pointing out about the SQL injection. We change that once we go to production.
Here are the tables with data and all of the code.
--************************************************** ********************
CREATE TABLE [AccessLevel] (
[AccessLevelID] [int] IDENTITY (1, 1) NOT FOR REPLICATION NOT NULL ,
[AccessLevelName] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[AccessLevelOrder] [tinyint] NOT NULL ,
[Status] [tinyint] NOT NULL CONSTRAINT [DF_AccessLevel_Status] DEFAULT (1),
CONSTRAINT [PK_AccessLevel] PRIMARY KEY CLUSTERED
(
[AccessLevelID]
) WITH FILLFACTOR = 90 ON [PRIMARY]
) ON [PRIMARY]
GO
INSERT INTO AccessLevel (AccessLevelName, AccessLevelOrder, Status) VALUES ('Basic', 1, 1)
INSERT INTO AccessLevel (AccessLevelName, AccessLevelOrder, Status) VALUES ('Silver', 2, 1)
INSERT INTO AccessLevel (AccessLevelName, AccessLevelOrder, Status) VALUES ('Gold', 3, 1)
INSERT INTO AccessLevel (AccessLevelName, AccessLevelOrder, Status) VALUES ('Platinum', 4, 1)
CREATE TABLE [AccessFeature] (
[AccessFeatureID] [int] IDENTITY (1, 1) NOT FOR REPLICATION NOT NULL ,
[AccessFeatureName] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[AccessFeatureDesc] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[Status] [bit] NOT NULL ,
CONSTRAINT [PK_AccessFeature] PRIMARY KEY CLUSTERED
(
[AccessFeatureID]
) WITH FILLFACTOR = 90 ON [PRIMARY]
) ON [PRIMARY]
GO
INSERT INTO AccessFeature (AccessFeatureName, AccessFeatureDesc, Status) VALUES ('Inventory', 'None', 1)
INSERT INTO AccessFeature (AccessFeatureName, AccessFeatureDesc, Status) VALUES ('Inventory.Add', 'None', 1)
INSERT INTO AccessFeature (AccessFeatureName, AccessFeatureDesc, Status) VALUES ('Inventory.Edit', 'None', 1)
INSERT INTO AccessFeature (AccessFeatureName, AccessFeatureDesc, Status) VALUES ('Inventory.Delete', 'None', 1)
CREATE TABLE [AccessLevelFeature] (
[AccessLevelID] [int] NOT NULL ,
[AccessFeatureID] [int] NOT NULL
) ON [PRIMARY]
GO
INSERT INTO AccessLevelFeature (AccessLevelID, AccessFeatureID) VALUES (1, 1)
INSERT INTO AccessLevelFeature (AccessLevelID, AccessFeatureID) VALUES (2, 3)
/*
SELECT * FROM AccessLevel
SELECT * FROM AccessFeature
SELECT * FROM AccessLevelFeature
DROP TABLE accesslevel
DROP TABLE accesslevelfeature
DROP TABLE accessfeature
*/
--************************************************** ********************
'********************************
VB Code********************************
Private Sub getAccessLevelFeature()
Try
_oConn.Open()
Dim SQL As String = "SELECT AccessFeatureID, AccessFeatureName AS [Feature]"
Dim SQL2 As String = "SELECT AccessLevelID, AccessLevelName FROM AccessLevel ORDER BY AccessLevelName;"
Dim CMD2 As SqlCommand = New SqlCommand(SQL2, _oConn)
Dim oDA2 As New SqlDataAdapter(CMD2)
Dim oDS2 As New DataSet
oDA2.Fill(oDS2)
For Each Row As DataRow In oDS2.Tables(0).Rows
SQL += ", " & Row.Item("AccessLevelID") & " AS [" & Row.Item("AccessLevelName") & "] "
Next
SQL += "FROM AccessFeature ORDER BY AccessFeatureName;"
Dim CMD As SqlCommand = New SqlCommand(SQL, _oConn)
Dim oDA As New SqlDataAdapter(CMD)
Dim oDS As New DataSet
oDA.Fill(oDS)
grdAccessLevelFeature.DataSource = oDS
grdAccessLevelFeature.DataBind()
Catch ex As Exception
Response.Write(ex.Message)
Finally
_oConn.Close()
End Try
End Sub
Protected Sub grdAccessLevelFeature_ItemDataBound(ByVal s As Object, ByVal e As DataGridItemEventArgs) Handles grdAccessLevelFeature.ItemDataBound
Select Case e.Item.ItemType
Case ListItemType.Item, ListItemType.AlternatingItem
Dim i As Integer
For i = 2 To e.Item.Cells.Count - 1
Dim chkAccessLevelFeature As New CheckBox
Dim SQL As String = "SELECT * FROM AccessLevelFeature WHERE AccessLevelID = " & e.Item.Cells(i).Text & " AND AccessFeatureID = " & e.Item.Cells(0).Text & ";"
Dim CMD As SqlCommand = New SqlCommand(SQL, _oConn)
Dim oDA As New SqlDataAdapter(CMD)
Dim oDS As New DataSet
oDA.Fill(oDS)
chkAccessLevelFeature.ID = "chkAccessLevelFeature"
chkAccessLevelFeature.Checked = oDS.Tables(0).Rows.Count > 0
e.Item.Cells(i).Controls.Add(chkAccessLevelFeature )
Next
End Select
e.Item.Cells(0).Visible = False
End Sub
Protected Sub btnSave_Click(ByVal s As Object, ByVal e As EventArgs) Handles btnSave.Click
Dim Pass As Boolean
Try
_oConn.Open()
Dim SQL As String = "DELETE FROM AccessLevelFeature;"
Dim CMD As New SqlCommand(SQL, _oConn)
CMD.ExecuteNonQuery()
For Each Item As DataGridItem In grdAccessLevelFeature.Items
Dim i As Integer
For i = 2 To Item.Cells.Count - 1
'Dim chkAccessLevelFeature As CheckBox = CType(Item.FindControl("chkAccessLevelFeature"), CheckBox)
Dim chkAccessLevelFeature As CheckBox = Item.Cells(i).Controls(0)
If chkAccessLevelFeature.Checked Then
Dim SQL2 As String = "INSERT INTO AccessLevelFeature (AccessLevelID, AccessFeatureID) VALUES (" & Item.Cells(i).Text & ", " & Item.Cells(0).Text & ");"
Dim CMD2 As New SqlCommand(SQL2, _oConn)
CMD2.ExecuteNonQuery()
End If
Next
Next
Pass = True
Catch ex As Exception
Response.Write(ex.Message)
Finally
_oConn.Close()
If Pass Then
getAccessLevelFeature()
End If
End Try
End Sub
'************************************************* **************************
aspx front page
<table border="0" width="100%" cellpadding="0" cellspacing="10">
<tr>
<td width="50%" valign="top">
<asp:DataGrid ID="grdAccessLevelFeature" Width="100%" CssClass="" runat="server" />
<br />
<asp:Button ID="btnSave" Text="Save" runat="server" /><br />
</td>
<td width="50%" valign="top">
<br />
</td>
</tr>
</table>
Very much appreciated.
Thanks,
Richard