传递参数时sqldatasource出现问题

时间:2011-07-11 07:48:45

标签: c# asp.net sql

sqldatasource参数的一部分:

            <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>

背后的代码:

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

它告诉我它无法将用户ID从Guid转换为字符串。但该参数必须是sqldatasource的select语句才能工作的Guid

2 个答案:

答案 0 :(得分:3)

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

更新代码

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

答案 1 :(得分:1)

您只需编写自定义参数:

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>