Assiging parameters to sqldatasource

23,009

Solution 1

You have two problems:

  1. first you don't need to add the parameter again - it's already defined in your markup
  2. you don't have to use a @ in your parameter name - just the name will do.

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 .DefaultValue on the existing parameter, and use the idd parameter name, as defined in your markup (<asp:Parameter Name="idd" Type="String" />)

Solution 2

It seems like you are adding 2 parameters here. One declarative and one in your code behind.

Try to add only the parameter in your code behind. also change the name to idd instead of @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();
 }
Share:
23,009
Dvirski
Author by

Dvirski

Updated on August 23, 2020

Comments

  • Dvirski
    Dvirski almost 4 years

    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.

    (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();
     }