Hello,
I'm needing to pass a variable length number of values to a select statement so I can populate a result list with items related to all the checkboxlist items that were selected by the user. for example, the user checks products x, y and z, then hits submit, and then they see a list of all the tests they need to run for each product.
I found a UDF that parses a comma delimited string and puts the values into a table. I learned how to do this here:
http://codebetter.com/blogs/darrell.norton/archive/2003/07/01/361.aspx
I have a checkboxlist that I'm generating the string from, so the string could look like this: "1,3,4,5,7" etc.
I added the function mentioned in the URL above to my database, and if I understand right, I should be able to pass the table it creates into the select statement like so:
WHERE (OrderStatus IN ((select value from dbo.fn_Split(@.StatusList,','))) OR @.StatusList IS NULL)
but now I don't know how to assign the string value to the parameter, say to '@.solution_id'.
my current select statement which was generated by Visual Studio 2005 looks like this:
SELECT [test], [owner], [date] FROM [test_table] WHERE ([solution_ID] = @.solution_ID)
...but this only pulls results for the first item checked in the checkboxlist.
Does anyone know how this is done? I'm sure it's simple, but I'm new to ASP .NET so any help would be greatly appreciated.
hi
First make sure you have createddbo.fn_Split .
SELECT [test], [owner], [date]FROM [test_table]WHERE ([solution_ID]IN ((select valuefrom dbo.fn_Split(@.solution_ID,',')))OR @.solution_IDISNULL)
I am not sure "OR @.solution_IDISNULL" should be added,you have to decide it according to your logic.
You are required to pass @.solution_ID to the statement(1,3,4,6 etc) then you can get corresponding test.
Hope this helps.
|||Thanks for your response. If I'm following you, I do understand that I need to pass @.solution_ID to the select statement like you showed. I have a string of values that I created from iterating through CheckBoxList to find selected boxes. My question is, how do I assign the value of this string to @.solution_ID?
Regards,
Daniel
|||
Assume you have checkboxlist Check1, using following code to get @.solution_ID :
for (int i = 0; i < Check1.Items.Count; i++)
{
if (Check1.Items[i].Selected)
{
// List the selected items
solution_ID = solution_ID + Check1.Items[i].Text;
solution_ID = solution_ID +",";
}
}
Then connect with DB:
SqlCommand sqlcmd = new SqlCommand("SELECT [test], [owner], [date]FROM [test_table]WHERE
([solution_ID]IN ((select valuefrom dbo.fn_Split(@.solution_ID,',')))OR @.solution_IDISNULL)", sqlconn);
sqlcmd.Parameters.AddWithValue("@.solution_ID",solution_ID);
sqlconn.Open();
SqlDataReader sdr = sqlcmd.ExecuteReader();
.............
You 'd bette put bold sql script into a stored procedure.
hope this helps.
No comments:
Post a Comment