Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using Sqldatasource

I want to run query according to condition

suppose
C#
I session("userid")="1" 
{
run this to query to bind grid

}
else
{
 this Query to bind grid
}
Posted
Updated 1-Dec-10 7:57am
v2

So, what's the problem in it?

There can be multiple ways... lets take one of them:
C#
if(Session["userid"] == "1")
{
   myDataGrid.DataSource = PopulateDataSetUsingQuery1();
}
else
{
   myDataGrid.DataSource = PopulateDataSetUsingQuery2();
}

//where as the two methods PopulateDataSetUsingQuery1 & PopulateDataSetUsingQuery2 will fetch the records based on Query1 & Query2
 
Share this answer
 
Comments
qasimidl 2-Dec-10 4:54am    
great working fine
You may also try something like that in Page_Load event:

If condition 
{
    SqlDataSource1.SelectCommand = "select * from TableA";
}
else
{
    SqlDataSource1.SelectCommand = "select * from TableB";
}




Or maybe you can get the result using parameters in your datasource:

<asp:sqldatasource
    id="SqlDataSource1"
    runat="server"
    connectionstring="<%$ ConnectionStrings:MyNorthwind%>"
    selectcommand="SELECT * FROM Table WHERE userid = @userid">
    <selectparameters>
        <asp:parameter name="userid" type="int32" />
    </selectparameters>
</asp:sqldatasource>


private void Page_Load(object sender, System.EventArgs e)
{
    If condition
    {
        SqlDataSource1.SelectParameters["userid"].DefaultValue = 123;
    }
    else
    {
        SqlDataSource1.SelectParameters["userid"].DefaultValue = 456;
    }
}



I hope it is useful.





-----------------------------------------------------------------
My site: Apartamentos a venda em Guarulhos
 
Share this answer
 
Comments
qasimidl 2-Dec-10 4:54am    
Thanks alot working fine

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900