1

I have a single dropdownlist with EnableAutoPostBack and two SqlDataSources.

What I am trying to do is if user chooses radiobuttonRed then the DDLType will use SqlDataSourceRed and display data into a gridview depending on DDLTYpe item selected.

If the user chooses radiobuttonBlue then the DDLType will use SqlDataSourceBlue and display the data into a gridview depending on DDLTYpe item selected.

How can I achieve this?

1 Answer 1

2

You would need two SqlConnection objects with connection strings to each database:

SqlConnection connRed = new SqlConnection();
SqlConnection connBlue = new SqlConnection();
DataTable dt = null;
SqlDataAdapter da = null;

if(radioButtonRed.Checked)
{
    dt = new DataTable();
    da = new SqlDataAdapter("select command", connRed);   
}
else
{    
    dt = new DataTable();
    da = new SqlDataAdapter("select command", connBlue);
}

da.Fill(dt);
dgv.DataSource = dt;
dgv.DataBind();
Sign up to request clarification or add additional context in comments.

7 Comments

The problem here is that you are using two Gridviews. I want to use one gridview, is it possible?
I've updated my answer to use one DataGridView and Fill the DataTable appropriately.
DataTable dt = null; SqlDataAdapter da = null; should i do this is giving an error since they are not used
you might want to finish it off with: dgv.databind
@nocturns2 Thanks for pointing that out. I had forgotten to call DataBind() since I forgot this was ASP.NET and not Windows Forms. In Windows Forms, it's not explicitly needed to call DataBind().
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.