Formview with nested wizard

Last post 12-06-2009 7:14 AM by amorphisciol. 5 replies.

Sort Posts:

  • Formview with nested wizard

    12-04-2009, 3:52 PM
    • Member
      2 point Member
    • amorphisciol
    • Member since 07-13-2007, 3:36 PM
    • Posts 6

    Hi All

    I have a formview which has a wizard nested in the insertitemtemplate. All works well except for when I add a datetime field, I then get this error: SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM. One of the values added in a test environment is 12/12/2009, so I don't think its a culture problem. I've also tried converting the textbox value to datetime before the insert. Furthermore if I change the asp:parameters to asp:controlparameters the solution works. But I figured, as controlparameters are meant for master/detail situations, this solution just seems wrong. Would really appreciate any solutions/advice you guys may have. Thanks in advance.

    So below is a small section of the code I've been working with.   

    <asp:FormView ID="FormView1" runat="server" DataSourceID="ObjectDataSource1" DefaultMode="Insert">

     <InsertItemTemplate>

    <asp:Wizard ID="Wizard1" runat="server" OnFinishButtonClick="Wizard1_FinishButtonClick">

    <asp:WizardStep ID="WizardStep1" runat="server" Title="Add Client">

     Date of Birth<asp:TextBox ID="txtDOB" runat="server" Text='<%# Eval("dob") %>' ></asp:TextBox>

     <WizardSteps>

     </InsertItemTemplate>

    </asp:FormView>

     <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
            InsertMethod="InsertNewClient" SelectMethod="SelectAClient"
            TypeName="AddClient">
            <InsertParameters>
                <asp:Parameter Name="dob" Type="DateTime" />
            </InsertParameters>
        </asp:ObjectDataSource>

    protected void ObjectDataSource1_Inserting(object sender, ObjectDataSourceMethodEventArgs e)

    { 

    Wizard wizard = (Wizard)FormView1.FindControl("Wizard1");

    TextBox dob = (TextBox)wizard.FindControl("txtDOB"); 

    e.InputParameters["dob"] = dob.Text;

    }

    protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)

    {

       ObjectDataSource1.Insert();

    }

    SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

    public void InsertNewClient(DateTime dob)

    {

    SqlCommand cmd = new SqlCommand("Insert INTO table (id, dob) VALUES (@dob); SELECT @id = SCOPE_Identity()", conn);

    SqlParameter idParam = new SqlParameter("@id", SqlDbType.Int);

    idParam.Direction = ParameterDirection.Output;

    cmd.Parameters.Add(idParam);

    cmd.Parameters.AddWithValue("@dob", SqlDbType.DateTime).Value = dob;

    conn.Open();

    cmd.ExecuteScalar();

    conn.Close();

    //Select statement

    public DataSet SelectAClient(){

    DataSet ds = new DataSet();

    SqlDataAdapter adapter = new SqlDataAdapter("SELECT * from table ", conn);

    adapter.Fill(ds);

    return ds;

    }

     

     

     

     

  • Re: Formview with nested wizard

    12-04-2009, 9:00 PM
    • Participant
      915 point Participant
    • Mr.Bill
    • Member since 06-05-2008, 9:20 PM
    • Posts 323

    amorphisciol:
    I have a formview which has a wizard nested in the insertitemtemplate. All works well except for when I add a datetime field, I then get this error: SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM. One of the values added in a test environment is 12/12/2009, so I don't think its a culture problem. I've also tried converting the textbox value to datetime before the insert. Furthermore if I change the asp:parameters to asp:controlparameters the solution works. But I figured, as controlparameters are meant for master/detail situations, this solution just seems wrong. Would really appreciate any solutions/advice you guys may have. Thanks in advance.

    Specifically, when is the error being thrown? (i.e., when you switch to 'insert' mode or when you click an insert button)

    I've had a similar problem in the past and if I'm remembering correctly this issue is solved when you format the datetime item during the 'PreRender' event.

    I also remember that if the DateTime was 'null' then the error was thrown. So, be sure that you handle a 'null' datetime. (i.e., "if yourDateTimeVariable == null) {...})

  • Re: Formview with nested wizard

    12-05-2009, 7:29 AM
    • Member
      2 point Member
    • amorphisciol
    • Member since 07-13-2007, 3:36 PM
    • Posts 6

    Hi

    Thanks for your response. The error is being thrown on the finish button click of the wizard. Based on your advice i've tried the code below, which seems to successfully convert the value to a datetime variable but still throws the error on the finish click.  Any ideas? Thanks

    protected void Wizard1_PreRender(object sender, EventArgs e)
        {
            if (FormView1.CurrentMode == FormViewMode.Insert)
            {
                Wizard wizard = (Wizard)FormView1.FindControl("Wizard1");
                TextBox txtdob = (TextBox)wizard.FindControl("txtDOB");
                DateTime? dt = new DateTime();
                if (txtdob.Text == String.Empty)
                {
                    dt = null;
                }
                else
                {
                    dt = DateTime.Parse(txtdob.Text);
                }
            } 
        }


     

  • Re: Formview with nested wizard

    12-05-2009, 12:36 PM
    • Participant
      915 point Participant
    • Mr.Bill
    • Member since 06-05-2008, 9:20 PM
    • Posts 323

    try this:

    • comment out the contents of PreRender event (the if statement) completely
    • simply change the 'dob' text binding from "Eval" to "Bind"

    I'm going to build a simulation of the problem your having in effort to replicate and solve the problem you're having but in the meantime try the above.

  • Re: Formview with nested wizard

    12-05-2009, 12:57 PM
    Answer
    • Participant
      915 point Participant
    • Mr.Bill
    • Member since 06-05-2008, 9:20 PM
    • Posts 323

    amorphisciol:
    Furthermore if I change the asp:parameters to asp:controlparameters the solution works. But I figured, as controlparameters are meant for master/detail situations, this solution just seems wrong.

    Forget what I said before, I just read through the documentation regarding 'ControlParameters' and I can see that you should undoubtedly use the 'ControlParameter' class.

  • Re: Formview with nested wizard

    12-06-2009, 7:14 AM
    • Member
      2 point Member
    • amorphisciol
    • Member since 07-13-2007, 3:36 PM
    • Posts 6

    Thanks for your advice. Have read through the control parameters documentation and will be using them after all. 

Page 1 of 1 (6 items)