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??!