Extracting a parameter in code behind.

  • Hi, I've tried to search under this kind of topic for quite a while now, and was unable to find anything that fits what I want.

    So here is what I wanna do:

    1) Count the rows of a table, leaving me with a single value in the data source (check)

    <asp:SqlDataSource ID="dsMapCount" runat="server"

    ConnectionString="<%$ ConnectionStrings:exeConnectionString %>"

    SelectCommand="SELECT COUNT(*) AS MapCount FROM Maps HAVING (COUNT(*) = @count)">

    <SelectParameters>

    <asp:Parameter Name="count" />

    </SelectParameters>

    </asp:SqlDataSource>

    2) Using this value as a max end of a random function:

    public static int randNumber(int i)

    {

    Random random = new Random();

    int result = random.Next(1, i+1);

    return result;

    }

    3) Using this result to get a field value (this case Name) by using a Where clause with the result of the function.

    <asp:SqlDataSource ID="dsMap" runat="server"

    ConnectionString="<%$ ConnectionStrings:exeConnectionString %>"

    SelectCommand="SELECT Name FROM Maps WHERE (ID = @randID) HAVING (Name = @name)">

    <SelectParameters>

    <asp:Parameter Name = "randID" />

    <asp:Parameter Name = "Name" />

    </SelectParameters>

    </asp:SqlDataSource>

    4) Display this in a label (should be easy as long as i can extract the name parameter of the second DS selection).

    Would love a suggestion as to how I could do this.

    Hope that is enough information to see what i am trying to do.

  • eankorins (11/30/2011)


    Hi, I've tried to search under this kind of topic for quite a while now, and was unable to find anything that fits what I want.

    So here is what I wanna do:

    1) Count the rows of a table, leaving me with a single value in the data source (check)

    <asp:SqlDataSource ID="dsMapCount" runat="server"

    ConnectionString="<%$ ConnectionStrings:exeConnectionString %>"

    SelectCommand="SELECT COUNT(*) AS MapCount FROM Maps HAVING (COUNT(*) = @count)">

    <SelectParameters>

    <asp:Parameter Name="count" />

    </SelectParameters>

    </asp:SqlDataSource>

    2) Using this value as a max end of a random function:

    public static int randNumber(int i)

    {

    Random random = new Random();

    int result = random.Next(1, i+1);

    return result;

    }

    3) Using this result to get a field value (this case Name) by using a Where clause with the result of the function.

    <asp:SqlDataSource ID="dsMap" runat="server"

    ConnectionString="<%$ ConnectionStrings:exeConnectionString %>"

    SelectCommand="SELECT Name FROM Maps WHERE (ID = @randID) HAVING (Name = @name)">

    <SelectParameters>

    <asp:Parameter Name = "randID" />

    <asp:Parameter Name = "Name" />

    </SelectParameters>

    </asp:SqlDataSource>

    4) Display this in a label (should be easy as long as i can extract the name parameter of the second DS selection).

    Would love a suggestion as to how I could do this.

    Hope that is enough information to see what i am trying to do.

    Well...this is not an asp.net site but I can probably help.

    You really are asking 3 or 4 questions?? I think you don't really have a question #1 since you said (check)? So I will assume that is the case and start with Question #2.

    2) Using this value as a max end of a random function:

    What is the question here? You have a random number function but i don't understand what you are trying to do.

    3) Using this result to get a field value (this case Name) by using a Where clause with the result of the function.

    Again...huh??? Are you wanting to use the result of your random function as the value for the @randID for this select statement?

    4) I think you want to get the value of the parameter so you can use it in the display? This is completely trivial to the issue at hand here.

    So I am going to assume that my assessment of your process is correct. There is more than 1 way to handle this type of thing. You could use session parameters or keep the current type of parameter. I will show you how to do this with session parameters because i think it is probably easier in this case.

    To further make assumptions, I will assume you want a random number as the @randID parameter each time the page loads.

    In your page load event just add a session object.

    Session["randID"] = randNum(myInt);

    Now you put your random number as the parameter value (assuming you change your parameter source to the session and set the correct session field.

    That should answer both 2 and 3. Then you can just pull that same value to answer #4. If you want to know another way to find SelectParameter values you can access the SelectParameters collection to retrieve values like this:

    dsMap.SelectParameters.GetValues(HttpContext.Current, null)["randID"]

    Hope that helps.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply