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;
}