user2983177
user2983177

Reputation: 201

dropdown list to execute query and display it in gridview

I have the following situation, I have a dropdown list populated by a database table that has the following columns "id", "name" and "query", the query column records are sql queries "select yada yada yada", the column displayed is "name".

sample from db

this dropdown list is in an aspcontent with a form tag within it, code below in image:

dropdown aspx code

then I have the gridview I want to populate which is in another asp content section

<asp:Content ID="rightcontentdown" ContentPlaceHolderID="rightcontentdown" Runat="Server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"></asp:GridView></asp:Content>

as it stands it says the gridview has to be in a form tag which is false, 'cause in my other page I have buttons (that execute queries) within a form tag and they populate a gridview in another aspcontent section outside the form tag (exactly the same setup I have)

now onto my issue/question, as I have mentioned up there, I have the actual query string inside db table column records, I want to be able to select the name in the dropdown list, execute the corresponding query and then display the query results in the gridview (I know I have to use databind)

is this even possible? the reason why I'm trying to do it like this is 'cause I or anyone within the IT department want to be able to, in the future, manage what the user sees without resorting to Visual Studio every time someone needs a new query or wants to edit an existing one, this another one of my questions which is related to this issue one of my threads here

Upvotes: 0

Views: 1016

Answers (1)

ovaltein
ovaltein

Reputation: 1215

How about something like this in your SelectIndexChanged Event. Assuming your dropdownlist is populated with "nome" in the text field and that those are unique names, this will grab the query stored in your database which you can then pass to another method to perform the lookup and populate your gridview. Let me know if I'm not fully understanding what you're trying to do.

SqlConnection Connection = null;
SqlCommand Command = null;

string ConnectionString = ConfigurationManager.ConnectionStrings["DB_testeConnectionString"].ConnectionString;
string CommandText = "SELECT rotina "
                   + "FROM rotinas_comercial "
                   + "WHERE nome = @someValue";
Connection = new SqlConnection(ConnectionString);

Connection.Open();
Command = new SqlCommand(CommandText, Connection);
Command.Parameters.Add(new SqlParameter("@someValue", DropDownList1.SelectedItem.Text));
var results = Command.ExecuteScalar();

Command.Dispose();
Connection.Close();

Upvotes: 1

Related Questions