Question

i have a page prueba.aspx with this code

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {

            Object ob = CPUSControl1.FindControl("ObjectDataSourcecpus");
            ObjectDataSource ods = (ob != null && ob.GetType() == typeof(ObjectDataSource)) ? (ObjectDataSource)ob : null;
            if (ods != null)
            {
                ods.SelectParameters.Clear();
                ods.SelectParameters.Add("usuario", DropDownListUsuarios.SelectedValue.ToString().Trim());
                ods.InsertParameters.Add("usuario", DropDownListUsuarios.SelectedValue.ToString().Trim());
            }
        }
        catch (Exception error)
        {
            throw error;
        }


    }

CPUSControl1 is a usercontrol in this page and its found ok, the objectdatasource if found ok too. Even de selectedparameters works ok but the insertparameter i add does not work. I've tried to change the name, debugedit step by step and the insertparameters allways takes the defaultvalue(0) The objectdatasource is this

<asp:ObjectDataSource ID="ObjectDataSourcecpus" runat="server" 
    SelectMethod="getCPUs" TypeName="CPU" InsertMethod="saveCPUs">
    <SelectParameters>
        <asp:Parameter Name="usuario" DefaultValue="0" Type="String" />
    </SelectParameters>
    <InsertParameters>
        <asp:Parameter Name="usuario" DefaultValue="0" Type="String" />
        <asp:Parameter Name="Codigo" Type="String" />
        <asp:Parameter Name="NroSerie" Type="String" />
        <asp:Parameter Name="TipoCPU" Type="String" />
        <asp:Parameter Name="Mother" Type="String" />
        <asp:Parameter Name="Modelo" Type="String" />
        <asp:Parameter Name="Procesador" Type="String" />
        <asp:Parameter Name="Memoria" Type="String" />
        <asp:Parameter Name="Proveedor" Type="Int32" />
        <asp:Parameter Name="BocaRed" Type="String" />
        <asp:Parameter Name="IP" Type="String" />
        <asp:Parameter Name="UPS" Type="Int32" />
        <asp:Parameter Name="VtoGarantia" Type="DateTime" />
        <asp:Parameter Name="FechaCompra" Type="DateTime" />
        <asp:Parameter Name="NombreEquipo" Type="String" />
        <asp:Parameter Name="USB" Type="String" />
        <asp:Parameter Name="Obs" Type="String" />
        <asp:Parameter Name="Marcacion" Type="String" />
        <asp:Parameter Name="TipoCPU1" Type="Int32" />
        <asp:Parameter Name="fechaentrada" Type="DateTime" />
        <asp:Parameter Name="fechasalida" Type="DateTime" />
    </InsertParameters>
</asp:ObjectDataSource>

i've been obligated to add the ods.SelectParameters.Clear(); but not a ods.InsertedParameters.Clear(); why? i guess it's because i'm never adding the insertparameters but y cant guess why. Thanks in advance. And excuse my bad english

Was it helpful?

Solution

Try something like this:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
   ObjectDataSource ods = CPUSControl1.FindControl("ObjectDataSourcecpus") as ObjectDataSource;
   if (ods != null)
   {
      string value = DropDownListUsuarios.SelectedValue.Trim();
      ods.SelectParameters["usuario"].DefaultValue = value;
      ods.InsertParameters["usuario"].DefaultValue = value;
   }
}

Changes to your code:

  • Don't catch an Exception if you're not going to do anything with it. (If you must re-throw an exception, use throw; instead of throw error; to preserve the stack-trace.)

  • Use the as operator for conditional casting of reference types - it's more efficient than calling .GetType() and comparing the result to a specific type.

  • The SelectedValue property is already a string - there's no need to call .ToString() on it.

  • Calling .Trim() creates a new string; it's best to cache the result in a local variable and reuse it.

  • You have already added the parameter to the SelectParameters and InsertParameters collection via markup. You just need to retrieve the existing Parameter object and set its DefaultValue property.


Edit:
The InsertParameters collection doesn't store changes in ViewState, so you'll lose the value on post-back. The simplest solution is probably to use the Inserting event to set the parameter value:

Main page (code-behind):

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
   CPUSControl1.Usuario = DropDownListUsuarios.SelectedValue.Trim();
}

User control:

<asp:ObjectDataSource ID="ObjectDataSourcecpus" runat="server" 
   SelectMethod="getCPUs" TypeName="CPU" InsertMethod="saveCPUs"
   OnSelecting="DataSourceCallingMethod"
   OnInserting="DataSourceCallingMethod"
   OnUpdating="DataSourceCallingMethod"
   OnDeleting="DataSourceCallingMethod"
>
...

*User control (code-behind):

public string Usuario
{
   get { return (string)ViewState["Usuario"]; }
   set { ViewState["Usuario"] = value; }
}

protected void DataSourceCallingMethod(object sender, ObjectDataSourceMethodEventArgs e)
{
   e.InputParameters["usuario"] = Usuario;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top