I have a grid and a dropdownlist from where the user select the query to fill in the grid. As it is now it works fine:
protected void Button1_Click(object sender, EventArgs e)
{
string selected = DropDownList1.SelectedItem.Value;
if (selected == "0")
{
Label1.Text = "You shall select a query!";
}
else
{
int x = Int32.Parse(selected);
string query = "";
switch (x)
{
case 1: //Top 100 TB 321
query = @"...something...";
SqlDataSource1.SelectCommand = query;
break;
case 2:
query = @"....something else....";
SqlDataSource1.SelectCommand = query;
break;
case 3:
.......
}
}
And in the markup I simply have the sqldatsource with the connection string:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:BOMConnectionString %>"
></asp:SqlDataSource>
With the above code I am unable to export the content of the grid because it generate an error "Object reference not set to an instance of an object". To avoid this issue, instead of defining the select command from code behind, I should pass the query as variable to the sqldatasource in the markup. How can I pass the variable "query" to the sqldatasource SelectCommand in the markup?
DropDownList1
always has a SelectedItem. In any case, there's no client-side SqlDataSource. It's a purely server-side thing, and with good reason; if client code could access your database, anybody loading your web app would have access to your database credentials and could log in and mess with stuff.