0

I have a infragistics grid with auto generated columns how can i assign a strored proc result to it from code behind

      <ig:WebScriptManager ID="WebScriptManager1" runat="server"></ig:WebScriptManager>
<ig:WebDataGrid ID="EntityGrid" runat="server"  Width="100%">
    <Behaviors>
        <ig:Sorting>
        </ig:Sorting>
    </Behaviors>
</ig:WebDataGrid>

code behind is

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        EntityName.Text = DropDownList1.SelectedItem.Text;
        string @RegardingObjectName = DropDownList1.SelectedItem.Text.Trim();
        String strConnString = ConfigurationManager.ConnectionStrings["LiveLeaseConnectionString"].ConnectionString;
        SqlConnection con = new SqlConnection(strConnString);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "p_DataList_ByRegardingObject";
        cmd.Parameters.AddWithValue("@RegardingObjectName", @RegardingObjectName);
        cmd.Connection = con;
        try
        {              
            con.Open();
            EntityGrid.DataSource = cmd.ExecuteReader();
            EntityGrid.DataBind();

i need to passin entity as parameter to stored proc. how can i do that here?

i am getting reader closed error

1
  • does the query return any rows? can you also paste the entire stack trace? Commented May 4, 2013 at 16:29

1 Answer 1

1

You will need to add a ControlParameter to SelectParameters

<asp:ControlParameter Name="entityId" 
ControlID="DropDownList1" 
PropertyName="SelectedItem" Type="String" />

Also see this question How to specify parameter value for stored procedure in SqlDataSource

Edit you can use the Selecting event as shown here

Writing a SQLdatasource in code behind in C# to use a code behind value

You can also add a select parameter as

EntityGrid.SelectParameters.Add("entityId", DropDownList1.SelectedItem.Text);

Just rename "entityId" to the name of parameter to be used with SP

Edit2 :

Instead of

EntityGrid.DataSource = cmd.ExecuteReader();
            EntityGrid.DataBind();

Try this

SqlDataReader reader = cmd.ExecuteReader();
    using (reader)
    {
      DataTable table = new DataTable();
      table.Load(reader);
      EntityGrid.DataSource = table;
    }

See this link for more :
http://mentaljetsam.wordpress.com/2008/11/20/loading-an-sqldatareader-into-a-datagridview/

Sign up to request clarification or add additional context in comments.

3 Comments

i want to do it from code behind in the.cs file as the this grid will get data only when selected index changed event is fired for a drop down
Thanks for help amitd. iam trying something like this
Invalid object name 't_Select'. i am getting this error when i try it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.