将日历选定日期保存到SQL数据库ASP.net

时间:2017-05-24 13:50:01

标签: c# sql asp.net visual-studio calendar

我正在为当地学校开发一个小项目,我几乎可以称之为编程中的新手。 我试图在日历中检索用户选择的日期以保存到单独的数据库。我有一个实体框架模型(.edmx)来链接我需要的Visual Studio中的表。 这是我到目前为止所得到的:

ASP.net:

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div class="main_banner">
    </div>
    <div class="main_container">
        <div class="main_heading">
            <h1 class="main_title">Projector Inspection</h1>
            <p><a href="AddProjector.aspx">Add</a> new Projector</p>
        </div>
        <div class="info_container">
            <div>
                <asp:Label ID="RoomNolbl" runat="server" Text="Room No:"></asp:Label>
             </div>
            <div>
                <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="Roomsdbo" DataTextField="Room_No" DataValueField="Room_No" AutoPostBack="True">
                    <asp:ListItem>---Select Room---</asp:ListItem>
                </asp:DropDownList> 
                <asp:SqlDataSource ID="Roomsdbo" runat="server" ConnectionString="<%$ ConnectionStrings:iPadLoanConnectionString2 %>" SelectCommand="SELECT [Room_No] FROM [tblRooms]"></asp:SqlDataSource>
             </div>

             <div>
                <asp:Label ID="Datelbl" runat="server" Text="Date Checked:"></asp:Label>
             </div>
            <div>
                <asp:Calendar ID="Calendar1" runat="server" BackColor="White" BorderColor="White" BorderWidth="1px" Font-Names="Verdana" Font-Size="9pt" ForeColor="Black" Height="190px" NextPrevFormat="FullMonth" Width="350px" OnSelectionChanged="Calendar1_SelectionChanged1" DayNameFormat="Shortest">
                    <DayHeaderStyle Font-Bold="True" Font-Size="8pt" />
                    <NextPrevStyle Font-Bold="True" Font-Size="8pt" ForeColor="#333333" VerticalAlign="Bottom" />
                    <OtherMonthDayStyle ForeColor="#999999" />
                    <SelectedDayStyle BackColor="#333399" ForeColor="White" />
                    <TitleStyle BackColor="White" BorderColor="#ae1022" BorderWidth="4px" Font-Bold="True" Font-Size="12pt" ForeColor="#ae1022" />
                    <TodayDayStyle BackColor="#CCCCCC" />
                </asp:Calendar>
                <asp:TextBox ID="Calendartxt" runat="server" ReadOnly="True"></asp:TextBox>
             </div>

            <div class ="info_container_task">
                <h3>Task carried</h3>
            <div>
                <asp:Label ID="CheckBoxFilterlbl" runat="server" Text="Filter Cleaning:"></asp:Label>
                <asp:CheckBox ID="CheckBoxFilter" runat="server" OnCheckedChanged="CheckBoxFilter_CheckedChanged" AutoPostBack="true" />
            </div>
            <div>
                <asp:Label ID="CheckBoxBulblbl" runat="server" Text="Bulb Replacement:"></asp:Label>
                <asp:CheckBox ID="CheckBoxBulb" runat="server"  OnCheckedChanged="CheckBoxBulb_CheckedChanged" AutoPostBack="true"/>
            </div>
            <div>
                <asp:Button ID="ButtonSubmit" runat="server" Text="Submit" OnClick="ButtonSubmit_Click" />
            </div>
          </div>
             <div>
                <asp:Label ID="HourCountBulblbl" runat="server" Text="Bulb Hours:"></asp:Label>
             </div>
            <div>
                <asp:TextBox ID="HourBulbtxt" runat="server" ></asp:TextBox>
             </div>
            <div>
                <asp:Label ID="FilterHourlbl" runat="server" Text="Filter Hours:"></asp:Label>
            </div>
            <div>
                <asp:TextBox ID="FilterHourtxt" runat="server" ></asp:TextBox>
            </div>
             <div>
                <asp:Label ID="Commentslbl" runat="server" Text="Additional Comments:" ></asp:Label>
             </div>
            <div>
                <textarea id="Commentstxt" cols="20" rows="2" ></textarea>&nbsp;</div>
        </div>

    </div>
<div class="sub_container">
    <h2>Additional Information</h2>
<div class="Tbl_Projectorinfo">
    <div class="projectorGrid">
            <asp:GridView ID="projectorinfo" runat="server" AutoGenerateColumns="False" CellPadding="4" DataSourceID="linkprojectorsdbo" ForeColor="#333333" GridLines="None" DataKeyNames="Model_No">
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                <Columns>
                    <asp:BoundField DataField="Current_Hours" HeaderText="Current_Hours" SortExpression="Current_Hours" />
                    <asp:BoundField DataField="Last_Replacement_Date" HeaderText="Last_Replacement_Date" SortExpression="Last_Replacement_Date" />
                    <asp:BoundField DataField="Last_Cleaned" HeaderText="Last_Cleaned" SortExpression="Last_Cleaned" />
                </Columns>
                <EditRowStyle BackColor="#999999" />
                <FooterStyle BackColor="#ae1022" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#ae1022" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                <SortedAscendingCellStyle BackColor="#E9E7E2" />
                <SortedAscendingHeaderStyle BackColor="#506C8C" />
                <SortedDescendingCellStyle BackColor="#FFFDF8" />
                <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
            </asp:GridView>
        </div>
            <asp:SqlDataSource ID="linkprojectorsdbo" runat="server" ConnectionString="<%$ ConnectionStrings:iPadLoanConnectionString %>" SelectCommand="SELECT [Model_No], [Current_Hours], [Last_Replacement_Date], [Last_Cleaned] FROM [tblProjectors] WHERE ([Room_No] = @Room_No)">
                <SelectParameters>
                    <asp:ControlParameter ControlID="DropDownList1" Name="Room_No" PropertyName="SelectedValue" Type="String" />
                </SelectParameters>
            </asp:SqlDataSource>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="tblModels" CellPadding="4" ForeColor="#333333" GridLines="None">
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                <Columns>
                    <asp:BoundField DataField="Manufacturer" HeaderText="Manufacturer" SortExpression="Manufacturer" />
                    <asp:BoundField DataField="Model_Name" HeaderText="Model" SortExpression="Model_Name" />
                    <asp:BoundField DataField="Min_Hours" HeaderText="MinHours" SortExpression="Min_Hours" />
                    <asp:BoundField DataField="Max_Hours" HeaderText="MaxHours" SortExpression="Max_Hours" />
                </Columns>
                <EditRowStyle BackColor="#999999" />
                <FooterStyle BackColor="#ae1022" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#ae1022" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                <SortedAscendingCellStyle BackColor="#E9E7E2" />
                <SortedAscendingHeaderStyle BackColor="#506C8C" />
                <SortedDescendingCellStyle BackColor="#FFFDF8" />
                <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
            </asp:GridView>
            <asp:SqlDataSource ID="tblModels" runat="server" ConnectionString="<%$ ConnectionStrings:iPadLoanConnectionString %>" SelectCommand="SELECT [Manufacturer], [Model_Name], [Min_Hours], [Max_Hours] FROM [ProjectorsRoom2017] WHERE ([Room_No] = @Room_No)">
                <SelectParameters>
                    <asp:ControlParameter ControlID="DropDownList1" Name="Room_No" PropertyName="SelectedValue" Type="String" />
                </SelectParameters>
            </asp:SqlDataSource>
        </div>
    </div>

.cs文件:

 protected void ButtonSubmit_Click(object sender, EventArgs e)
    {
        tblProjector entry = new tblProjector();
        entry.Last_Cleaned = Convert.ToDateTime(Calendartxt.Text);
        iPadLoanEntities db = new iPadLoanEntities();
        db.tblProjectors.Add(entry);
        db.SaveChanges();
    }




    protected void Calendar1_SelectionChanged1(object sender, EventArgs e)
    {
        Calendartxt.Text = Calendar1.SelectedDate.ToShortDateString();
    }
}

我经常收到错误消息,指出一个或多个实体的验证失败。错误也指向代码行:

db.SaveChanges();

我知道使用该代码我试图获取链接到日历的TextBox的信息以保存到数据库,但说实话,如果可能的话,直接从日历中保存它将是很好的。< / p>

如果有人能帮我说明如何实现这一目标,我将不胜感激。感谢

0 个答案:

没有答案