WithFlyingColors
WithFlyingColors

Reputation: 2770

Problem with sqldatasource when passing parameters

Part of the sqldatasource parameters:

            <asp:SqlDataSource ID="AllQuestionAskedDataSource" runat="server" 
                ConnectionString="<%$ ConnectionStrings:CP_AllQuestionsAnswered %>" SelectCommand="SELECT ThreadTitle
FROM Threads
WHERE UsersID=@UserID">
                <SelectParameters>
                    <asp:Parameter Name="UserID" />
                </SelectParameters>
            </asp:SqlDataSource>

The code behind:

 Guid userid = UsefulStaticMethods.GetUserNameFromUserGuid(name);
 AllQuestionAskedDataSource.SelectParameters["UserID"].DefaultValue = userid;

It tells me that it cant convert userid from Guid to string. But the parameter must be a Guid for the select statement of the sqldatasource to work

Upvotes: 1

Views: 4228

Answers (2)

Islam Yahiatene
Islam Yahiatene

Reputation: 1469

You could simply write your custom parameter:

public class UserIdParameter : Parameter
{
    protected override object Evaluate(HttpContext context, 
        System.Web.UI.Control control)
    {
        var name = GetName();
        return UsefulStaticMethods.GetUserNameFromUserGuid(name);

    }
}

<SelectParameters>
        <custom:UserIdParameter Name="UserID" />
</SelectParameters> 

Upvotes: 1

Saanch
Saanch

Reputation: 1844

<SelectParameters>
                <asp:Parameter Name="UserID" Type="Guid"  />
     </SelectParameters> 

Updated Code

<asp:Parameter Name="UserID" Type="Object" />
<asp:Parameter Name="MemberID" DbType="Guid" />

Upvotes: 3

Related Questions