This is a migrated thread and some comments may be shown as answers.

[Solved] StoredProcedure bing to DataGrid

1 Answer 118 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Michael Musial
Top achievements
Rank 1
Michael Musial asked on 18 Jan 2010, 03:02 AM
I got a stored produre that can get paramters from a form.  I am tring to bind my dataset to grid on the form.  What am i doing wrong in this button click procedure.  It will not reurn dataset..


 

protected void Button1_Click(object sender, EventArgs e)

 

{


RadGrid1.DataSource = RunStoredProcSearch(

"SearchNameCity", SearchGrid.Text);

 

RadGrid1.DataBind();

 

}

 

 

public DataSet RunStoredProcSearch(string spName, string search)

 

{

 

SqlConnection conn = null;

 

 

SqlDataReader rdr = null;

 

 

 

 

 

try

 

{

 

// create and open a connection object

 

conn =

new

 

 

SqlConnection("Data Source=LDSERVER1\\SQLLD;Initial Catalog=AviarySQL;Integrated Security=True");

 

conn.Open();

 

// 1. create a command object identifying

 

 

// the stored procedure

 

 

SqlCommand cmd = new SqlCommand(

 

spName, conn);

 

// 2. set the command object so it knows

 

 

// to execute a stored procedure

 

cmd.CommandType =

CommandType.StoredProcedure;

 

 

// 3. add parameter to command, which

 

 

// will be passed to the stored procedure

 

cmd.Parameters.Add(

 

new SqlParameter("@pattern", search));

 

 

 

// execute the command

 

cmd.Connection.Open();

 

SqlDataReader reader = cmd.ExecuteReader();

 

 

return reader;

 

 

 

 

 

 

 

 

}

 

finally

 

{

 

if (conn != null)

 

{

conn.Close();

}

 

if (reader != null)

 

{

rdr.Close();

}

}

 

}



1 Answer, 1 is accepted

Sort by
0
Yavor
Telerik team
answered on 21 Jan 2010, 08:01 AM
Hi Michael,

I would suggest that you use the NeedDataSource event handler of the grid control, to pass data to the grid. Calling DataBind() directly for the grid control would break many of its default functionalities.
More information on this is available in the following article:

http://www.telerik.com/help/aspnet-ajax/grdadvanceddatabinding.html

Other than this, accessing the stored procedure from within the NeedDataSource event handler is a standard approach. More information on a similar setup is available in the following article:

http://aspalliance.com/757_Code_Snip_Populating_GridView_Control_Using_ASPNET_and_C_with_MouseOver_Effect.2
I hope this information helps.

All the best,
Yavor
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Grid
Asked by
Michael Musial
Top achievements
Rank 1
Answers by
Yavor
Telerik team
Share this question
or