how to call sp_tables from C#

I need to populate a Dropdown with the table names in the databse using sp_tables stored proc. Can someone please help me on how to do it.
string con = "------------------------------------"
        cn = new SqlConnection(con);
        cmd = new SqlCommand("sp_tables", cn);
 
        cn.Open();
        cmd.CommandType = CommandType.StoredProcedure;
        p1 = new SqlParameter();
        p1.Direction = ParameterDirection.Output;
        p1.SqlDbType = SqlDbType.VarChar;
        cmd.Parameters.Add(p1);
        cmd.ExecuteNonQuery();

Open in new window

aspgeekAsked:
Who is Participating?
 
RiteshShahCommented:
here you go.




//create connection string and assign it to SqlConnection object
        string strConn = "Data Source=.;Initial Catalog=AdventureWorks;Persist Security Info=True;User ID=sa;Password=sa";
        System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(strConn);
        conn.Open();
 
        //setup SqlCommand and assign SP name along with input parameter variable name and value
        System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand();
        command.CommandType = System.Data.CommandType.StoredProcedure;
        command.Connection = conn;
        command.CommandText = "SP_Tables";
        
        //create one data adapter which will execute the command and fill the data into data set
        System.Data.SqlClient.SqlDataAdapter recordAdp = new System.Data.SqlClient.SqlDataAdapter();
        recordAdp.SelectCommand = command;
        System.Data.DataSet recordSet = new System.Data.DataSet();
        recordAdp.Fill(recordSet);
        command.Dispose();
        conn.Close();
 
        DropDownList1.DataSource = recordSet.Tables[0];
        DropDownList1.DataTextField = recordSet.Tables[0].Columns["Table_Name"].ToString();
        DropDownList1.DataValueField= recordSet.Tables[0].Columns["Table_Name"].ToString();
        DropDownList1.DataBind();

Open in new window

0
 
aspgeekAuthor Commented:
excellent !! Thanks for the immediate response.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.