user1080139
user1080139

Reputation: 165

Populate GridView with resultset of stored procedure

I've been struggling with displaying the resultset of a stored procedure in a grid view on the click of a button.

My issue is that am working on the existing code of a portal based on 3-tier architecture which doesn't allow me to use stuff like SqlDataAdapter and such. Because such functionality have already been incorporated by some methods which are available in the form of dlls.

I've tried returning a DataTable or a DataSet as the data source of the gridview but in vain.

Please help me by suggesting if there is any alternative object that can be assigned to the datasource of the gridview.. I am new to all this.. Any help is most appreciated !!

Here is my code:

DataAccess Layer:

public IDataReader BindGridView(string rgn)
{
    string spdName = "spd_get_default_region";
    DbCommand cmd = DB.GetStoredProcCommand(spdName);
    DB.AddInParameter(cmd, "Rgn", DbType.String, rgn);
    IDataReader idr = cmd.ExecuteReader();
    return  idr;
}

Here, GetStoredProcCommand is a built-in method. gvAll is the ID of the gridview.

Grid.aspx.cs:

protected void Go_Rgn_Click(object sender, EventArgs e)
{ 
    DAL da=new DAL();
    plhTable.Visible = true;
    plhChoose.Visible = false;
    plhForm.Visible = false;
    string Rgn = afRgn.Text;
    gvAll.DataSource = da.BindGridView(Rgn);
    gvAll.DataBind();
}

Upvotes: 1

Views: 1933

Answers (1)

Turnkey
Turnkey

Reputation: 9406

A simple alternative is to create a dataTable object.

DataTable defaultRegion = new DataTable();
defaultRegion.Load(cmd.ExecuteReader());
gvAll.DataSource = table;
gvAll.DataBind();

Upvotes: 1

Related Questions