C# 为 sqldatasource 分配参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17000390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Assiging parameters to sqldatasource
提问by Dvirski
I'm trying to get data from SQL Server and use it in a formview, but the formview control won't get any data from the datasource.
我正在尝试从 SQL Server 获取数据并在 formview 中使用它,但 formview 控件不会从数据源获取任何数据。
(The datasource gets parameter on page load)
(数据源在页面加载时获取参数)
Output is just: "There is nothing to see here" and an empty table.
输出只是:“这里没有什么可看的”和一张空桌子。
Here is the datasource and formview tags:
这是数据源和表单视图标签:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:igroup20_test2ConnectionString %>"
SelectCommand="SELECT * FROM [member] where ([id] = @idd)">
<SelectParameters>
<asp:Parameter Name="idd" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="id">
<EmptyDataTemplate>
There is nothing to see here.
</EmptyDataTemplate>
<ItemTemplate>
<table>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("id") %>'></asp:Label>
</td>
<td>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("f_name") %>'></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
</asp:FormView>
Here is my code behind:
这是我的代码:
protected void Page_Load(object sender, EventArgs e)
{
SqlDataSource1.SelectParameters.Add("@idd", "077763554");
FormView1.DataBind();
}
回答by Ronald
It seems like you are adding 2 parameters here. One declarative and one in your code behind.
似乎您在这里添加了 2 个参数。一个声明性的,一个在你的代码后面。
Try to add only the parameter in your code behind. also change the name to iddinstead of @idd.
尝试仅在后面的代码中添加参数。还将名称更改为idd而不是@idd。
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:igroup20_test2ConnectionString %>"
SelectCommand="SELECT * FROM [member] where ([id] = @idd)">
</asp:SqlDataSource>
protected void Page_Load(object sender, EventArgs e)
{
SqlDataSource1.SelectParameters.Add("idd", "077763554");
FormView1.DataBind();
}
回答by marc_s
You have two problems:
你有两个问题:
- first you don't need to addthe parameter again - it's already defined in your markup
- you don't have to use a
@in your parameter name - just the name will do.
- 首先,您不需要再次添加参数 - 它已在您的标记中定义
- 您不必
@在参数名称中使用 a - 只需使用名称即可。
So use this code instead:
因此,请改用此代码:
protected void Page_Load(object sender, EventArgs e)
{
SqlDataSource1.SelectParameters["idd"].DefaultValue = "077763554";
FormView1.DataBind();
}
That should do the trick - set the .DefaultValueon the existing parameter, and use the iddparameter name, as defined in your markup (<asp:Parameter Name="idd" Type="String" />)
这应该可以解决问题 -.DefaultValue在现有参数上设置,并使用idd标记中定义的参数名称 ( <asp:Parameter Name="idd" Type="String" />)

