点击按钮时数据是否保存在数据库中? ASP.NET

时间:2016-05-21 09:32:24

标签: c# asp.net ms-access

我正在尝试将一些信息从一个页面保存到数据库中,我认为我使用的代码是真的,但我不知道为什么它没有保存到访问数据库?

数据库名称 - > Database1

表名 - > Table1

字段名称 - > Name,Email,Mobile,Laptop,WhatYouNeedFromUs,WhereAreYouFrom

类型 - > Text

连接很好,只是没有插入数据并保存到数据库中我不知道为什么!!

ContactUs.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="ContactUs.aspx.cs" Inherits="ContactUs" %>


<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

    <asp:Table ID="Table1" runat="server" style="width:80%; margin-left:auto; margin-right:auto; margin-top:50px; background-color:#fff" >
        <asp:TableRow ID="TableRow1" runat="server">
            <asp:TableCell ID="TableCell1" runat="server" Width="50%" Text="Name " HorizontalAlign="Center" BorderColor="black" VerticalAlign="Middle" Font-Bold="True" BorderWidth="1px"></asp:TableCell>
            <asp:TableCell ID="TableCell2" runat="server" BorderColor="black" BorderWidth="1px"><asp:TextBox ID="TextBox1" runat="server" Height="50px" 
        Width="99%" MaxLength="20" Font-Size="Large"></asp:TextBox>
</asp:TableCell>
        </asp:TableRow>

        <asp:TableRow ID="TableRow2" runat="server">
            <asp:TableCell ID="TableCell3" runat="server" Width="50%" Text="Email " HorizontalAlign="Center" BorderColor="black" VerticalAlign="Middle" Font-Bold="True" BorderWidth="1px"></asp:TableCell>
            <asp:TableCell ID="TableCell4" runat="server" BorderColor="black" BorderWidth="1px"><asp:TextBox ID="TextBox2" runat="server" Height="50px" 
        Width="99%" MaxLength="20" Font-Size="Large"></asp:TextBox>
</asp:TableCell>
        </asp:TableRow>

        <asp:TableRow ID="TableRow3" runat="server">
            <asp:TableCell ID="TableCell5" runat="server" Width="50%" Text="Mobile Phone " HorizontalAlign="Center" BorderColor="black" VerticalAlign="Middle" Font-Bold="True" BorderWidth="1px"></asp:TableCell>
            <asp:TableCell ID="TableCell6" runat="server" BorderColor="black" BorderWidth="1px"><asp:TextBox ID="TextBox3" runat="server" Height="50px" 
        Width="99%" MaxLength="20" Font-Size="Large"></asp:TextBox>
</asp:TableCell>
        </asp:TableRow>

        <asp:TableRow ID="TableRow4" runat="server">
            <asp:TableCell ID="TableCell7" runat="server"  CellPadding="5" CellSpacing="5" VerticalAlign="Top" Font-Bold="True" Font-Size="Large" HorizontalAlign="Right">Which Laptop You Want?</asp:TableCell>
            <asp:TableCell ID="TableCell8" runat="server"  CellPadding="5" CellSpacing="5" ColumnSpan="2"><asp:RadioButtonList ID="RadioButtonList1" runat="server" Font-Bold="True" Font-Size="Large">
        <asp:ListItem>Hp</asp:ListItem>


<asp:ListItem>Samsung</asp:ListItem>


<asp:ListItem>Sony</asp:ListItem>


<asp:ListItem>Lenovo</asp:ListItem>


</asp:RadioButtonList>
</asp:TableCell>
        </asp:TableRow>

        <asp:TableRow ID="TableRow5" runat="server">
            <asp:TableCell ID="TableCell9" runat="server" Width="50%" Text="What you need from us?" HorizontalAlign="Center" BorderColor="black" VerticalAlign="Middle" Font-Bold="True" BorderWidth="1px"></asp:TableCell>
            <asp:TableCell ID="TableCell10" runat="server" BorderColor="black" BorderWidth="1px"><asp:TextBox ID="TextBox4" placeholder="Enter what you want" runat="server" Height="50px" 
        Width="99%" MaxLength="200" Font-Size="Large"></asp:TextBox>
</asp:TableCell>
        </asp:TableRow>

        <asp:TableRow ID="TableRow6" runat="server">
            <asp:TableCell ID="TableCell11" runat="server" Width="50%" Text="Where Are You From? " HorizontalAlign="Center" BorderColor="black" VerticalAlign="Middle" Font-Bold="True" BorderWidth="1px"></asp:TableCell>
            <asp:TableCell ID="TableCell12" runat="server" BorderColor="black" BorderWidth="1px"><asp:ListBox ID="ListBox1" runat="server" Rows="1" Width="99%">
        <asp:ListItem>Kurdistan</asp:ListItem>

<asp:ListItem>Iraq</asp:ListItem>

<asp:ListItem>Italy</asp:ListItem>

<asp:ListItem>Netherlands</asp:ListItem>

</asp:ListBox>

</asp:TableCell>
        </asp:TableRow>


        <asp:TableRow ID="TableRow7" runat="server" >
            <asp:TableCell ID="TableCell13" runat="server" ColumnSpan="2" VerticalAlign="Middle" HorizontalAlign="Center" Font-Size="Larger"><asp:Button ID="Button1"  style="margin-top:20px;" runat="server" BackColor="Black" Font-Bold="True" 
        Font-Size="Larger" ForeColor="Red" Text="Submit" /></asp:TableCell>
        </asp:TableRow>


    </asp:Table>

    <br /><br /><br />
    <br /><br /><br />
</asp:Content>

C#代码: ContaceUs.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;

public partial class ContactUs : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        OleDbConnection connection = new OleDbConnection();
        connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Sevar\Documents\Database1.mdb;
Persist Security Info=False;";
        connection.Open();

        OleDbCommand command = new OleDbCommand();
        command.Connection = connection;

        command.CommandText = "Insert into Table1 (Name,Email,Mobile,Laptop,WhatYouNeedFromUs,WhereAreYouFrom) Values('" + TextBox1 + "','" + TextBox2 + "','" + TextBox3 + "','" + RadioButtonList1 + "','" + TextBox4 + "','" + ListBox1 + "')";
        command.ExecuteNonQuery();
        connection.Close();
    }
}

0 个答案:

没有答案