locked
SqlDataSource paramerter binding RRS feed

  • Question

  • User1280718391 posted

    Hi i am using GridView and SqlDatasource to bind the grid based on the parameter. So iused the below code to bind the Grid in aspx page

      <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:PostingNoticeConnectionString %>"
                SelectCommand="SELECT ID,EID,Name,DefViewPartition FROM Emp WHERE [FID] = @EID AND Deleted = 0 ORDER BY Name">
                <SelectParameters>
                    <asp:Parameter Direction="Input" Name="@EID" Type="Int32" DefaultValue="1000" />
                </SelectParameters>
            </asp:SqlDataSource>
     
    But i ma getting the "Must Declare Scalr variable error"
    Since the input value comes dynamically I also tried in code behind

    //SqlDataSource1.SelectParameters.Add("@EID",DbType.Int32,EID);

    SqlDataSource1.SelectParameters.Add("@EID", DbType.Int32, EID);

    SqlDataSource1.SelectParameters[0].DefaultValue = EID;

     

    But seems no result..

    so please help me to find the result

    -Thanks

    Thursday, June 2, 2011 9:48 AM

Answers

  • User-1499637000 posted

    Hi,

    • Check wether you have mentioned the DatakeyNames property to the grid.
    • I have noticed in your SelectCommand WHERE [FID]=@EID, change this to WHERE EID = @EID (Mean to say once again verify the column names)
    • Change the Name in select parameter from Name=@EID to Name=EID.

    Below is the modified SqlDataSource:

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:PostingNoticeConnectionString %>" 
                SelectCommand="SELECT ID,EID,Name,DefViewPartition FROM Emp WHERE EID = @EID AND Deleted = 0 ORDER BY Name"> 
                <SelectParameters> 
                    <asp:Parameter Direction="Input" Name="EID" Type="Int32" DefaultValue="1000" /> 
                </SelectParameters> 
            </asp:SqlDataSource>

    Hope the above helps to solve your problem.

    Thanks...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 2, 2011 10:11 AM

All replies

  • User-1499637000 posted

    Hi,

    • Check wether you have mentioned the DatakeyNames property to the grid.
    • I have noticed in your SelectCommand WHERE [FID]=@EID, change this to WHERE EID = @EID (Mean to say once again verify the column names)
    • Change the Name in select parameter from Name=@EID to Name=EID.

    Below is the modified SqlDataSource:

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:PostingNoticeConnectionString %>" 
                SelectCommand="SELECT ID,EID,Name,DefViewPartition FROM Emp WHERE EID = @EID AND Deleted = 0 ORDER BY Name"> 
                <SelectParameters> 
                    <asp:Parameter Direction="Input" Name="EID" Type="Int32" DefaultValue="1000" /> 
                </SelectParameters> 
            </asp:SqlDataSource>

    Hope the above helps to solve your problem.

    Thanks...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 2, 2011 10:11 AM
  • User3866881 posted

    Hello:)

    Two solutions——

    1)Remove“@”in the <SelectParameters> tags.

    2)Remove the whole "<asp:Parameter Direction="Input" Name="@EID" Type="Int32" DefaultValue="1000" />", and just do manually coding in the Page_Load if(!IsPostBack){……}block.

    Thx

    Friday, June 3, 2011 11:26 PM