I have a check box list in ASP.NET with some values and I want to pass all of those values selected into a SQL parameter. The reason for it is so the user can display which columns from a table they want to see.
<asp:CheckBoxList ID="CheckBoxList1" runat="server" RepeatColumns="5" RepeatDirection="Horizontal" Width="100%">
<asp:ListItem Value="[ID]">ID</asp:ListItem>
<asp:ListItem Value="[USER]">User</asp:ListItem>
<asp:ListItem Value="[City]">City</asp:ListItem>
</asp:CheckBoxList>
Now when the user clicks on the view button, I run this code to get the list of columns the user wants to see and I place it in the label to make sure I can see visually it's working correctly. Which is does and it displays it as "[ID], [User], [City]".
string listCheckBox = "";
foreach (ListItem li in CheckBoxList1.Items)
{
if (li.Selected)
{
listCheckBox += li.Value + ",";
}
}
listCheckBox = listCheckBox.Remove(listCheckBox.Length - 1);
Label1.Text = listCheckBox;
But my issue is that when I jump to my SQL Server Management Studio and view the list that's been passed in, I can only see the first column name? which in this case is "[ID]".
DECLARE @SQL NVARCHAR(Max) = ''
SET @SQL =
'
SELECT ' + @TableList + ' FROM People
'
EXEC( @SQL)
I do believe it has something to do with the SQL Parameters in Visual Studio which might remove the rest of the list. Below is the ASP code...
if (Page.IsValid)
{
string CS = ConfigurationManager.ConnectionStrings["connectionStr"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("GetColumnListNames", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter TableList = new SqlParameter("@TableList", listCheckBox);
cmd.Parameters.Add(TableList);
con.Open();
I'm really not sure why it isn't taken in all the values. I think it has something to do with the SqlParameter
in Visual Studio, maybe it's cutting it off?
Any thoughts anyone?