Click here to Skip to main content
15,067,574 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
create proc getQuantityUpTo100
as
begin
  DECLARE @intFlag INT
  SET @intFlag = 0
  WHILE (@intFlag <=100)
  BEGIN
    PRINT @intFlag
    SET @intFlag = @intFlag + 1
  END
end

c# code
C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var ddl = (DropDownList)e.Row.FindControl("myDropDownList");
        SqlConnection con = new SqlConnection(StrConnection);
        con.Open();
        DataTable dt = new DataTable();
        SqlCommand cmd = new SqlCommand("getQuantityUpTo100", con);
        cmd.CommandType = CommandType.StoredProcedure;

        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);

        ddl.DataSource = dt;
        ddl.DataBind();
    }
}

asp.net code
HTML
<asp:TemplateField HeaderText="Quantity">
  <ItemTemplate>
    <asp:DropDownList ID="myDropDownList"  Style="width: 350px;" runat="server" OnSelectedIndexChanged="myDropDownList_SelectedIndexChanged"
                                                    AutoPostBack="true" EnableViewState="true">

    </asp:DropDownList>
Posted
Updated 11-Dec-14 2:59am
v2

1 solution

Values passed to PRINT in your stored procedure will not be part of any result-set. Only values returned as part of a SELECT statement will appear in your DataTable.

You could fix the stored procedure:
SQL
CREATE PROC getQuantityUpTo100
As
BEGIN
    SELECT TOP 101
        ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) - 1 As N
    FROM
        sys.all_columns
    ;
END


However, since you're just returning a list of numbers from 0 to 100, you don't need a stored procedure. Make sure you have a reference to the System.Core assembly, and a using System.Linq; statement at the top of your code file, and then use:
C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var ddl = (DropDownList)e.Row.FindControl("myDropDownList");
        ddl.DataSource = Enumerable.Range(0, 101);
        ddl.DataBind();
    }
}


Since the list is static, and trivial to recreate on post-back, you could reduce the size of your ViewState by binding the list in the control's Init event, rather than the GridView's RowDataBound event:
ASP.NET
<asp:DropDownList ID="myDropDownList"  runat="server"
    AutoPostBack="true"
    Style="width: 350px;"
    OnInit="BindMyDropDownList"
    OnSelectedIndexChanged="myDropDownList_SelectedIndexChanged"
/>

C#
protected void BindMyDropDownList(object sender, EventArgs e)
{
    var ddl = (DropDownList)sender;
    ddl.DataSource = Enumerable.Range(0, 101);
    ddl.DataBind();
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900