【问题标题】:Assiging parameters to sqldatasource将参数分配给 sqldatasource
【发布时间】:2013-06-04 17:19:57
【问题描述】:

我正在尝试从 SQL Server 获取数据并在 formview 中使用它,但 formview 控件不会从数据源中获取任何数据。

(数据源在页面加载时获取参数)

输出只是:“这里没有什么可看的”和一个空表。

这里是datasource和formview标签:

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

这是我的代码:

 protected void Page_Load(object sender, EventArgs e)
 {
        SqlDataSource1.SelectParameters.Add("@idd", "077763554");
        FormView1.DataBind();
 }

【问题讨论】:

    标签: c# asp.net sqldatasource formview


    【解决方案1】:

    你有两个问题:

    1. 首先您不需要再次添加参数 - 它已在您的标记中定义
    2. 您不必在参数名称中使用 @ - 只需使用名称即可。

    所以改用这段代码:

    protected void Page_Load(object sender, EventArgs e)
    {
        SqlDataSource1.SelectParameters["idd"].DefaultValue = "077763554";
        FormView1.DataBind();
    }
    

    这应该可以解决问题 - 在现有参数上设置 .DefaultValue,并使用标记中定义的 idd 参数名称 (&lt;asp:Parameter Name="idd" Type="String" /&gt;)

    【讨论】:

      【解决方案2】:

      您似乎在这里添加了 2 个参数。一个声明性的,一个在您的代码中。

      尽量只在后面的代码中添加参数。还将名称更改为 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();
       }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-24
        • 2021-10-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多