views:

406

answers:

1

Hello,

I have an ObjectDataSource with an ID of ObjectDataSource1 on a webpage. I also have a gridview in which I am binding the ObjectDataSource.ID to the GridView.DataSourceID. The problem I get is when text is changed in a textbox, the code calls BrokerageTransactions.GetAllWithDt which returns a DataTable. I want to set this datatable as the DataSource for the GridView, but it is telling me that I can't set the DataSouce and DataSourceId together. How can I fix this? Code is below. Also. Why can't you set a DataSourceID and a DataSource when using an ObjectDataSource?

Thanks, X

protected void BrokerageChange(Object sender, EventArgs e)
{
    BrokerageTransactions brokerageaccountdetails = 
                          new BrokerageTransactions();

    DataSet ds = BrokerageAccount.GetBrkID2(new 
                 Guid(Membership.GetUser().ProviderUserKey.ToString()), 
                 ddlBrokerageDetails.SelectedItem.Text.ToString());

    foreach (DataRow dr in ds.Tables[0].Rows)
    {
        brokerageaccountdetails.BrokerageId = 
                                new Guid(dr["BrkrgId"].ToString());
    }

    ddlBrokerageDetails.SelectedItem.Value = 
                        brokerageaccountdetails.BrokerageId.ToString();

    if (txtTransactionsTo.Text != "" 
        && txtTransactionsFrom.Text != "")
        ObjectDataSource1.FilterExpression = 
        "convert(CreateDt,System.DateTime)>Convert('" + 
         Convert.ToDateTime(txtTransactionsFrom.Text) + "',System.DateTime) 
         and Convert(CreateDt,System.DateTime)<convert('"
         + Convert.ToDateTime(txtTransactionsTo.Text.ToString()) + 
         "',System.DateTime)";
    else if (txtTransactionsFrom.Text != "")
            ObjectDataSource1.FilterExpression =
            "convert(CreateDt,System.DateTime)>convert('" +
             Convert.ToDateTime(txtTransactionsFrom.Text) + 
            "',System.DateTime)";
    else if (txtTransactionsTo.Text != "")
            ObjectDataSource1.FilterExpression = 
            "convert(CreateDt,System.DateTime)
            <convert('" 
            + Convert.ToDateTime(txtTransactionsTo.Text.ToString()) +  
            "',System.DateTime)";
    else
        ObjectDataSource1.FilterExpression = " ";

    grvBrokerage.DataSourceID = ObjectDataSource1.ID;
    grvBrokerage.DataBind();

    DateTime dtTransFrom = Convert.ToDateTime("1/1/1900");
    DateTime dtTransTo = System.DateTime.Today;

    //TransactionsTo Box is Empty
    if ((txtTransactionsFrom.Text.Length > 2) 
    && (txtTransactionsTo.Text.Length < 2)) 
    {
        dtTransFrom = Convert.ToDateTime(txtTransactionsFrom.Text);
        dtTransTo = System.DateTime.Today;
    }

    //TransactionsFrom Box is Empty
    if ((txtTransactionsFrom.Text.Length < 2) 
    && (txtTransactionsTo.Text.Length > 2))
    {
       dtTransFrom = Convert.ToDateTime("1/1/1900");
       dtTransTo = Convert.ToDateTime(txtTransactionsTo.Text);
    }

    //TransactionsFrom Box and TransactionsTo Box is Not Empty
    if ((txtTransactionsFrom.Text.Length > 2) 
    && (txtTransactionsTo.Text.Length > 2))
    {
        dtTransFrom = Convert.ToDateTime(txtTransactionsFrom.Text);
        dtTransTo = Convert.ToDateTime(txtTransactionsTo.Text);
    }
    // Fails Here
    grvBrokerage.DataSource = 
    BrokerageTransactions.GetAllWithDt(brokerageaccountdetails.BrokerageId, 
                                       dtTransFrom, 
                                       dtTransTo);
    grvBrokerage.DataBind();  }
A: 

You got 2 options:

  • Do not use the ObjectDataSource at all. You just use the DataSource property and Set It grammatically every time.
  • You use the DataSourceID property of the GridView and Add 2 plain asp:Parameters with default value set. On the ObjectDataSource_Selecting Event you set these parameters through e.InputParameters[]

Hope this helps.

norbertB
Could you be more specific? What would these two parameters be?Thanks
Xaisoft
Those parameters for you would be the From and To dates for the BrokerageTransactions In your example code this would be dtTransFrom and dtTransTo.
norbertB