使用按钮单击事件将文本框数据从内容表单插入Access数据库

时间:2016-04-17 21:54:00

标签: c# asp.net vb.net

我需要一些帮助让我的代码工作,因为我不知道如何解决这个问题。

下面是我在InsertDVD.aspx.vb代码中与上图相对应的代码:

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data.OleDb
Imports System.Configuration
Imports System.Collections
Imports System.Data
Imports System.Data.SqlClient

Public Class WebForm1
    Inherits System.Web.UI.Page
    Dim strTitle As String
    Dim strDirector As String
    Dim strRating As String
    Dim intDvdLength As Integer
    Dim intYearReleased As Integer
    Dim releaseDate As String

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)       Handles Me.Load
    calRDate.SelectedDate = DateAndTime.Now
    lblError.Text = ""
End Sub

Protected Sub btnDVDInformation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDVDInformation.Click
    Dim Title As String = txtTitle.Text
    Dim Director As String = txtDirector.Text
    Dim Rating As String = txtRating.Text
    Dim DvdLength = Int32.Parse(txtLength.Text)
    Dim YearReleased = Int32.Parse(txtReleased.Text)
    Dim ReleasedDate = calRDate.SelectedDate.ToString()
    lblDisplayUserInput.Text = "You entered " + txtTitle.Text + " for the DVD and the director is " + txtDirector.Text + ", Rating is " + strRating + ", DVD Length is " + intDvdLength.ToString() + ", Year Released is " + intYearReleased.ToString() + ", Release Date is " + releaseDate

    Dim conn As OleDbConnection
    conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\DVDdatabase.accdb")

    conn.Open()
    Dim strSql = "INSERT INTO [tblDVDs] ([ID], [Director], [Rating], [DVDLength], [YearReleased], [ReleasedDate]) VALUES (@ID, @Director, @Rating, @DVDLength, @YearReleased, @ReleasedDate)"
    Using objcmd = New OleDbCommand(strSql, conn)
        objcmd.Parameters.AddWithValue("@Title", Title)
        objcmd.Parameters.AddWithValue("@Director", Director)
        objcmd.Parameters.AddWithValue("@Rating", Rating)
        objcmd.Parameters.AddWithValue("@DVDLength", DvdLength)
        objcmd.Parameters.AddWithValue("@YearReleased", YearReleased)
        objcmd.Parameters.AddWithValue("@ReleasedDAte", ReleasedDate)
        objcmd.Parameters.AddWithValue("@ID", ID)
        objcmd.ExecuteNonQuery()
    End Using
    conn.Close()

    ' Show a message showing that the data was successfully inserted to the database. 
    Response.Write("Submitted Successfully")

End Sub

Protected Sub calRDate_SelectionChanged(sender As Object, e As EventArgs) Handles calRDate.SelectionChanged
    Dim curDate As Date
    curDate = DateAndTime.Now()

    If curDate <= calRDate.SelectedDate Then
        lblError.Text = "Please check the release date, it should be less than or equal to today"
        btnDVDInformation.Enabled = False
    Else
        lblError.Text = ""
        btnDVDInformation.Enabled = True
    End If

End Sub

End Class

下面是我的标记代码,它与上面的代码对应:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="InsertDVD.aspx.vb" Inherits="Gabe_Lopez__DVDs.WebForm1" MasterPageFile="~/GabeLopezDVDs.Master"%>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

<div>
Welcome to Gabe Lopez&#39; DVD Collection
<br />
<br />
</div>

<asp:label id="lblTitle" runat="server" text="Title: " />
<asp:textbox id="txtTitle" runat="server" forecolor="Black" />

<asp:requiredfieldvalidator id="rv1" runat="server" cssclass="errtext" controltovalidate="txtTitle" errormessage="Please enter DVD Title" ForeColor="Red" />
<br />
<br />

<asp:label id="lblDirector" runat="server" text="Director: " />
<asp:textbox id="txtDirector" runat="server" />

<asp:requiredfieldvalidator id="rv2" runat="server" cssclass="errtext" controltovalidate="txtDirector" errormessage="Please enter DVD Director" ForeColor="Red" />
<br />
<br />
<asp:label id="lblRating" runat="server" text="Rating: " />
<asp:textbox id="txtRating" runat="server" />

<asp:requiredfieldvalidator id="rfvRating" runat="server" cssclass="errtext"   controltovalidate="txtRating" errormessage="Please enter DVD Rating" ForeColor="Red" />
<asp:rangevalidator id="rvValidator" runat="server" cssclass="errtext" controltovalidate="txtRating" minimumvalue="1" maximumvalue="5" type="Integer" errormessage="Invalid Rating, It should be between 1 and 5" ForeColor="Red" />
<br />
<br />
<asp:label id="lblDVDLength" runat="server" text="DVD Length: " />
<asp:textbox id="txtLength" runat="server" />

<asp:requiredfieldvalidator id="rfvValidator" runat="server" cssclass="errtext" controltovalidate="txtLength" errormessage="Please enter DVD Length" ForeColor="Red" />
<asp:comparevalidator id="cvValidator" runat="server" cssclass="errtext" valuetocompare="0" controltovalidate="txtLength" operator="GreaterThanEqual" errormessage="Please check DVD length, it should be equal or greater than zero" ForeColor="Red" />
<br />
<br />
<asp:label id="lblYearReleased" runat="server" text="Year Released: " />
<asp:textbox id="txtReleased" runat="server" />

<asp:requiredfieldvalidator id="rfReleased" runat="server" cssclass="errtext" controltovalidate="txtReleased" errormessage="Please enter Year Released" ForeColor="Red" />
<asp:RangeValidator id="rvReleased" runat="server" CssClass="errtext" ControlToValidate="txtReleased" MinimumValue="1900" MaximumValue="2016" Type="Integer" ErrorMessage="Invalid year released, It should be between 1900 and 2016" ForeColor="Red" />
<asp:RegularExpressionValidator runat="server" id="rexReleased" CssClass="errtext"  controltovalidate="txtReleased" validationexpression="^[0-9]{4}$" errormessage="Please enter a 4 digit number!" ForeColor="Red" />
<br />
<br />
<asp:Label id="lblpDate" runat="server" text="Released Date"></asp:Label>
<asp:Calendar id="calRDate" runat="server"></asp:Calendar>    

<br />        
<asp:button OnClick="btnDVDInformation_Click" id="btnDVDInformation" runat="server" text="Insert DVD Information" />  
<br />

<br /><asp:Label id="lblError" runat="server" CssClass="errtext" ForeColor="Red"/>
<br />
<asp:Label id="lblUserInput" runat="server" text="Here is the text you entered in the text boxes above: " />

<asp:Label id="lblDisplayUserInput" runat="server" />
<br />
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString4 %>" 
    ProviderName="<%$ ConnectionStrings:ConnectionString4.ProviderName %>" 
    SelectCommand="SELECT * FROM [tblDVDs]"></asp:SqlDataSource>
<br />

</asp:Content>

以下是用户输入DVD数据的y内容页面的图片: Insert DVD info page 下面是我的错误消息的图片: error message

0 个答案:

没有答案