Calendar control with user control

Last post 07-15-2009 5:31 AM by testcc. 12 replies.

Sort Posts:

  • Calendar control with user control

    07-08-2009, 10:40 AM
    • Member
      3 point Member
    • testcc
    • Member since 07-08-2009, 10:10 AM
    • Posts 25

    Hi All,

    I am very new to VB and .net and has just stared to learn to use VIsual web developer. I have a question about calendar control. I am designing a page and would like the user to choose the date from the calendar and the chosen date will be inserted into the textbox. I am using it inside detailsview.


    The following is my code:

     

    <script language="VB" runat="server">

    Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
    DetailsView1.FindControl("Calendar1").Visible = True

    End Sub

    Sub Calendar1_Selected(ByVal sender As Object, ByVal e As EventArgs)
    Dim Calendar1 As New Calendar
    Dim TextBox2 As New TextBox

    TextBox2 = DetailsView1.FindControl("Dates")

    TextBox2.Text = Calendar1.SelectedDate.ToShortDateString()

    Calendar1 = DetailsView1.FindControl("Calendar1")



    DetailsView1.FindControl("Calendar1").Visible = False

    End Sub

    </script>

    The ASPX page

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>

    </div>
    <asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True"
    AutoGenerateRows="False" BackColor="White" BorderColor="#E7E7FF"
    BorderStyle="None" BorderWidth="1px" CellPadding="3" DataKeyNames="AuthorID"
    DataSourceID="SqlDataSource1" GridLines="Horizontal" Height="50px"
    Width="359px">
    <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
    <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
    <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
    <Fields>
    <asp:BoundField DataField="AuthorID" HeaderText="AuthorID"
    InsertVisible="False" ReadOnly="True" SortExpression="AuthorID" />
    <asp:BoundField DataField="FirstName" HeaderText="FirstName"
    SortExpression="FirstName" />
    <asp:BoundField DataField="LastName" HeaderText="LastName"
    SortExpression="LastName" />
    <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
    <asp:BoundField DataField="State" HeaderText="State" SortExpression="State" />
    <asp:BoundField DataField="Zip" HeaderText="Zip" SortExpression="Zip" />
    <asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
    <asp:TemplateField HeaderText="Dates" SortExpression="Dates">
    <EditItemTemplate>


    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    <asp:Calendar ID="Calendar1" runat="server" onselectionchanged="Calendar1_Selected" BackColor="White"
    BorderColor="#3366CC" BorderWidth="1px" CellPadding="1"
    DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt"
    ForeColor="#003399" Height="200px" Visible="False" Width="220px">
    <SelectedDayStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
    <SelectorStyle BackColor="#99CCCC" ForeColor="#336666" />
    <WeekendDayStyle BackColor="#CCCCFF" />
    <TodayDayStyle BackColor="#99CCCC" ForeColor="White" />
    <OtherMonthDayStyle ForeColor="#999999" />
    <NextPrevStyle Font-Size="8pt" ForeColor="#CCCCFF" />
    <DayHeaderStyle BackColor="#99CCCC" ForeColor="#336666" Height="1px" />
    <TitleStyle BackColor="#003399" BorderColor="#3366CC" BorderWidth="1px"
    Font-Bold="True" Font-Size="10pt" ForeColor="#CCCCFF" Height="25px" />
    </asp:Calendar>


    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />


    </EditItemTemplate>
    <InsertItemTemplate>
    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Dates") %>'></asp:TextBox>
    </InsertItemTemplate>
    <ItemTemplate>
    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Dates") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:CommandField ShowEditButton="True" />
    </Fields>
    <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
    <EditRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
    <AlternatingRowStyle BackColor="#F7F7F7" />
    </asp:DetailsView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConflictDetection="CompareAllValues"
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
    DeleteCommand="DELETE FROM [Authors] WHERE [AuthorID] = @original_AuthorID AND (([FirstName] = @original_FirstName) OR ([FirstName] IS NULL AND @original_FirstName IS NULL)) AND (([LastName] = @original_LastName) OR ([LastName] IS NULL AND @original_LastName IS NULL)) AND (([City] = @original_City) OR ([City] IS NULL AND @original_City IS NULL)) AND (([State] = @original_State) OR ([State] IS NULL AND @original_State IS NULL)) AND (([Zip] = @original_Zip) OR ([Zip] IS NULL AND @original_Zip IS NULL)) AND (([Phone] = @original_Phone) OR ([Phone] IS NULL AND @original_Phone IS NULL)) AND (([Dates] = @original_Dates) OR ([Dates] IS NULL AND @original_Dates IS NULL))"
    InsertCommand="INSERT INTO [Authors] ([FirstName], [LastName], [City], [State], [Zip], [Phone], [Dates]) VALUES (@FirstName, @LastName, @City, @State, @Zip, @Phone, @Dates)"
    OldValuesParameterFormatString="original_{0}"
    SelectCommand="SELECT * FROM [Authors]"
    UpdateCommand="UPDATE [Authors] SET [FirstName] = @FirstName, [LastName] = @LastName, [City] = @City, [State] = @State, [Zip] = @Zip, [Phone] = @Phone, [Dates] = @Dates WHERE [AuthorID] = @original_AuthorID AND (([FirstName] = @original_FirstName) OR ([FirstName] IS NULL AND @original_FirstName IS NULL)) AND (([LastName] = @original_LastName) OR ([LastName] IS NULL AND @original_LastName IS NULL)) AND (([City] = @original_City) OR ([City] IS NULL AND @original_City IS NULL)) AND (([State] = @original_State) OR ([State] IS NULL AND @original_State IS NULL)) AND (([Zip] = @original_Zip) OR ([Zip] IS NULL AND @original_Zip IS NULL)) AND (([Phone] = @original_Phone) OR ([Phone] IS NULL AND @original_Phone IS NULL)) AND (([Dates] = @original_Dates) OR ([Dates] IS NULL AND @original_Dates IS NULL))">
    <DeleteParameters>
    <asp:Parameter Name="original_AuthorID" Type="Int32" />
    <asp:Parameter Name="original_FirstName" Type="String" />
    <asp:Parameter Name="original_LastName" Type="String" />
    <asp:Parameter Name="original_City" Type="String" />
    <asp:Parameter Name="original_State" Type="String" />
    <asp:Parameter Name="original_Zip" Type="String" />
    <asp:Parameter Name="original_Phone" Type="String" />
    <asp:Parameter DbType="Date" Name="original_Dates" />
    </DeleteParameters>
    <UpdateParameters>
    <asp:Parameter Name="FirstName" Type="String" />
    <asp:Parameter Name="LastName" Type="String" />
    <asp:Parameter Name="City" Type="String" />
    <asp:Parameter Name="State" Type="String" />
    <asp:Parameter Name="Zip" Type="String" />
    <asp:Parameter Name="Phone" Type="String" />
    <asp:Parameter DbType="Date" Name="Dates" />
    <asp:Parameter Name="original_AuthorID" Type="Int32" />
    <asp:Parameter Name="original_FirstName" Type="String" />
    <asp:Parameter Name="original_LastName" Type="String" />
    <asp:Parameter Name="original_City" Type="String" />
    <asp:Parameter Name="original_State" Type="String" />
    <asp:Parameter Name="original_Zip" Type="String" />
    <asp:Parameter Name="original_Phone" Type="String" />
    <asp:Parameter DbType="Date" Name="original_Dates" />
    </UpdateParameters>
    <InsertParameters>
    <asp:Parameter Name="FirstName" Type="String" />
    <asp:Parameter Name="LastName" Type="String" />
    <asp:Parameter Name="City" Type="String" />
    <asp:Parameter Name="State" Type="String" />
    <asp:Parameter Name="Zip" Type="String" />
    <asp:Parameter Name="Phone" Type="String" />
    <asp:Parameter DbType="Date" Name="Dates" />
    </InsertParameters>
    </asp:SqlDataSource>
    </form>
    </body>
    </html>


    I keep getting the same error and could not figure out who to sort it.

    Object reference not set to an instance of an object.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    Source Error:

    Line 15:         TextBox2 = DetailsView1.FindControl("Dates")
    Line 16:
    Line 17: TextBox2.Text = Calendar1.SelectedDate.ToShortDateString()
    Line 18:
    Line 19: Calendar1 = DetailsView1.FindControl("Calendar1")


    Source File: C:\Websites\datademo\Default2.aspx    Line: 17


    Can anybody please have a look at the code and help me??!



  • Re: Calendar control with user control

    07-08-2009, 10:57 AM
    • Contributor
      6,020 point Contributor
    • integrasol
    • Member since 06-05-2009, 3:18 PM
    • Posts 1,146

    I'd say that TextBox2 control wasn't found using the FindControl method, so it's null or Nothing in VB. Try debugging and place a breakpoint on the offending line and do a QuickWatch on both the TextBox2 and Calendar1 controls to see, which is null, if not both.

    Thanks

    Carsten

    Please mark this post as Answer if it is of help to you. :-)
  • Re: Calendar control with user control

    07-09-2009, 5:02 AM
    • Member
      3 point Member
    • testcc
    • Member since 07-08-2009, 10:10 AM
    • Posts 25

    Hi Carsten,

    Thanks for your help. I did manage to modify the code a little bit more, and it does not give me any error messange now, however, when I click any date on the caldendar, it returns null and it actually doesnt insert any date ant all to the textbox. But I am able to add the new date or modify the date by typing in the textbox. Any idea how to fix the problem?


    <script language="VB" runat="server">

    Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
    DetailsView1.FindControl("Calendar1").Visible = True


    End Sub

    Sub Calendar1_Selected(ByVal sender As Object, ByVal e As EventArgs)
    Dim Calendar1 As New Calendar
    Dim tb As TextBox = CType(DetailsView1.FindControl("TextBox2"), TextBox)
    tb = CType(Page.FindControl("DetailsView1$TextBox2"), TextBox)
    Response.Write(tb.Text)
    Calendar1 = DetailsView1.FindControl("Calendar1")
    DetailsView1.FindControl("Calendar1").Visible = False
    DetailsView1.DataBind()
    End Sub

    </script>



    Could Anybody else help as well!! I have been stuck here for over two days and I really hope that I can get this problem sorted.


    Many thanks for your help

  • Re: Calendar control with user control

    07-09-2009, 8:15 AM
    Answer
    • Contributor
      6,020 point Contributor
    • integrasol
    • Member since 06-05-2009, 3:18 PM
    • Posts 1,146

    Try this code for the OnSelectionChanged event of the Calendar control.

     Sub Calendar1_Selected(ByVal sender As Object, ByVal e As EventArgs)
            Dim tb As TextBox = CType(DetailsView1.FindControl("TextBox2"), TextBox)
            Dim cal As Calendar = CType(sender, Calendar)
            tb.Text = cal.SelectedDate.ToShortDateString
            cal.Visible = False
    End Sub

    Thanks

    Carsten

    Please mark this post as Answer if it is of help to you. :-)
  • Re: Calendar control with user control

    07-09-2009, 8:49 AM
    • Member
      3 point Member
    • testcc
    • Member since 07-08-2009, 10:10 AM
    • Posts 25

    Thanks, it is getting so close now. It works no problem on the record when it already has exisitng date, however, when I tried to insert a date to the record which doesnt have a date yet,


    an error appears:

    Conversion from type 'DBNull' to type 'Date' is not valid.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.InvalidCastException: Conversion from type 'DBNull' to type 'Date' is not valid.

    Source Error:

    Line 54: 
    Line 55: <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Dates", "{0}") %>' ></asp:TextBox>
    Line 56: <asp:Calendar ID="Calendar1" runat="server"
    Line 57: onselectionchanged="Calendar1_Selected" BackColor="White"
    Line 58: BorderColor="#3366CC" BorderWidth="1px" CellPadding="1"


    Source File: C:\Websites\datademo\Default.aspx    Line: 56

    The data type for the dates is Date, and it allows NULL.


    How can I sort this error?

    Many thanks

  • Re: Calendar control with user control

    07-09-2009, 9:06 AM
    • Contributor
      6,020 point Contributor
    • integrasol
    • Member since 06-05-2009, 3:18 PM
    • Posts 1,146

    Hmm, I have to guess as I don't have an exact match of your setup, but it looks to me as if the Text property of the TextBox control is the problem. The error states that the problem is the conversion from DBNull to Date, which makes me assume that the error occurs when you retrieve a row from the database where the Date is null. Can you confirm or deny this?

    Thanks

    Carsten

    Please mark this post as Answer if it is of help to you. :-)
  • Re: Calendar control with user control

    07-09-2009, 9:14 AM
    • Member
      3 point Member
    • testcc
    • Member since 07-08-2009, 10:10 AM
    • Posts 25

    Yes, that is the setting in my database. the field for the Dates allows NULLs.  Do you think this setting caused all the problem? Should I just change it back to not allow Nulls?



  • Re: Calendar control with user control

    07-09-2009, 10:44 AM
    • Contributor
      6,020 point Contributor
    • integrasol
    • Member since 06-05-2009, 3:18 PM
    • Posts 1,146

    Ah no, don't do that. If your db design says that the filed/column should allow null values than that is fine. What I wanted to know, is if the application encounters an error when displaying a row with a null value for this field? I am trying to establish when the error occurs, at display time or when trying to update the database.

    Thanks

    Carsten

    Please mark this post as Answer if it is of help to you. :-)
  • Re: Calendar control with user control

    07-13-2009, 4:07 AM
    • Member
      3 point Member
    • testcc
    • Member since 07-08-2009, 10:10 AM
    • Posts 25

    The db works fine on its own. It seems that it is ok to edit the exisitng date filed, but the error appears when i try to insert data into the date filed. Hope I have explained the problem clearly.

  • Re: Calendar control with user control

    07-13-2009, 5:57 AM
    • Contributor
      6,020 point Contributor
    • integrasol
    • Member since 06-05-2009, 3:18 PM
    • Posts 1,146

    Cool, but just to iterate, the error occurs when you try and insert a row in the database, whether new or as an update to an existing row, if the Date is not specified, right?

    Thanks

    Carsten

    Please mark this post as Answer if it is of help to you. :-)
  • Re: Calendar control with user control

    07-13-2009, 6:33 AM
    • Member
      3 point Member
    • testcc
    • Member since 07-08-2009, 10:10 AM
    • Posts 25

    Hi, it worked. I dont really know why but the calendar control now is able to edit, update and insert data into db. thanks for all of your help!

    However, I have a new problem with the field validation now. i will not be able to use requiredfiledvalidator. If I add this function to the page, when I clcik the button, the error warning message come up and the calendar does not appear.  Any instuctions and suggestions?

    Thanks, your help is always appreciated.




  • Re: Calendar control with user control

    07-14-2009, 3:46 AM
    Answer
    • Contributor
      6,020 point Contributor
    • integrasol
    • Member since 06-05-2009, 3:18 PM
    • Posts 1,146

    Yes, the validation kicks in when leaving the TextBox control, which is the correct behavior. However, you can set the CausesValidation property of the Button control to false, so that this isn't the case.

    CausesValidation="false"

    Thanks

    Carsten

    Please mark this post as Answer if it is of help to you. :-)
  • Re: Calendar control with user control

    07-15-2009, 5:31 AM
    • Member
      3 point Member
    • testcc
    • Member since 07-08-2009, 10:10 AM
    • Posts 25

    You are genius, thanks so much for all your help!  The problem is solved.Laughing

Page 1 of 1 (13 items)