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

databinding menu to Stored Procedure

1 Answer 38 Views
Menu
This is a migrated thread and some comments may be shown as answers.
Mmm
Top achievements
Rank 1
Mmm asked on 19 Sep 2011, 04:13 PM
I have following code in my Master Page C# Code. Its just throwing me error on "RadMenu1.DataSource = MenuCreation.ExecuteNonQuery();"
that Object Reference not found. This code does work elsewhere but just not working in Master Site. Can someone help. How can I use RadMenu in my master site

my stored procedure name is AllMenu which basically select all data from table name MyMenus
Also my stored procedure sql is simple. it is "SELECT id, text, (CASE WHEN parentid = 0 THEN  NULL ELSE parentid END) FROM MyMenus"

            SqlCommand MenuCreation= new SqlCommand();
            SqlConnection dbconnection = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLDB"].ConnectionString);
            MenuCreation.Connection = dbconnection ;
            MenuCreation.CommandType = CommandType.StoredProcedure;
            MenuCreation.CommandText = "AllMenu";
            

            ConnectionState previousConnectionState = dbconnection.State;

            dbconnection.Open();
            RadMenu1.DataSource = MenuCreation.ExecuteNonQuery();


            RadMenu1.DataFieldID = "ID";
            RadMenu1.DataFieldParentID = "ParentID";

            RadMenu1.DataTextField = "Text";
            RadMenu1.DataValueField = "ID";
            RadMenu1.DataNavigateUrlField = "URL";

            RadMenu1.DataBind();      

1 Answer, 1 is accepted

Sort by
0
Kevin
Top achievements
Rank 2
answered on 22 Sep 2011, 03:29 PM
Hello Mmm,

The reason your code doesn't work is because you are calling ExecuteNonQuery, which returns an int stating the number of rows affected by the query.

In order to return the result of the Stored Procedure, you need to use the SqlDataAdapter class and fill a DataTable that you will pass as the DataSource for the RadMenu.

I hope that helps.
Tags
Menu
Asked by
Mmm
Top achievements
Rank 1
Answers by
Kevin
Top achievements
Rank 2
Share this question
or