ASP.. NET将表单数据插入SQL Server数据库不工作

本文关键字:Server 数据库 工作 SQL 插入 NET 表单 数据 ASP | 更新日期: 2023-09-27 18:09:40

我正在使用ASP编写一个空的webform应用程序。. NET 4.5和我有一个问题插入数据从我的形式,我做,到我的SQL Server数据库表。我认为所有的代码都是好的,但它不能工作。我还需要在这一页上做一个饼干,但这两者都不奏效。你能帮帮我吗?我使用母版页和内容页。我想问题出在我的内容页上。

这个页面的HTML代码是:

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <p id="p">Vul hier je gegevens en je nieuwe score in!</p>
    <table style="width: 100%;" id="table">
            <tr>
                <td class="auto-style1">Voornaam:</td>
                <td class="auto-style4">
                    <asp:TextBox ID="txtVoornaam" runat="server"></asp:TextBox>
                </td>
                <td class="auto-style6">&nbsp;</td>
                <td class="auto-style8">Datum:</td>
                <td class="auto-style2">
                    <asp:TextBox ID="txtDatum" runat="server"></asp:TextBox>
                </td>
                <td>
                    <asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="txtDatum" Display="Dynamic" ErrorMessage="Onjuiste datum!" MaximumValue="01-01-2020" MinimumValue="01-01-1990" Type="Date">!</asp:RangeValidator>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="txtDatum" Display="Dynamic" ErrorMessage="Datum niet ingevoerd!">*</asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td class="auto-style1">Achternaam:</td>
                <td class="auto-style4">
                    <asp:TextBox ID="txtAchternaam" runat="server"></asp:TextBox>
                </td>
                <td class="auto-style6">
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtAchternaam" Display="Dynamic" ErrorMessage="Het veld &quot;Achternaam&quot; is verplicht!">*</asp:RequiredFieldValidator>
                </td>
                <td class="auto-style8">&nbsp;</td>
                <td class="auto-style2">&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style1">Categorie:</td>
                <td class="auto-style4">
                    <asp:DropDownList ID="DropDownList1" runat="server">
                        <asp:ListItem Value="0">Kinderen</asp:ListItem>
                        <asp:ListItem Value="1">Volwassenen</asp:ListItem>
                    </asp:DropDownList>
                </td>
                <td class="auto-style6">
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="DropDownList1" Display="Dynamic" ErrorMessage="Het veld &quot;Categorie&quot; is verplicht!">*</asp:RequiredFieldValidator>
                </td>
                <td class="auto-style8">Score:</td>
                <td class="auto-style2">
                    <asp:TextBox ID="txtScore" runat="server" Width="60px"></asp:TextBox>
                    &nbsp;punten</td>
                <td>
                    <asp:RangeValidator ID="RangeValidator2" runat="server" ControlToValidate="txtScore" Display="Dynamic" ErrorMessage="Onjuist aantal punten!" MaximumValue="300" MinimumValue="1" Type="Integer">!</asp:RangeValidator>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="txtScore" Display="Dynamic" ErrorMessage="Score niet ingevuld!">*</asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td class="auto-style1">&nbsp;</td>
                <td class="auto-style4">&nbsp;</td>
                <td class="auto-style6">&nbsp;</td>
                <td class="auto-style8">Afstand:</td>
                <td class="auto-style2">
                    <asp:TextBox ID="txtAfstand" runat="server" Width="60px"></asp:TextBox>
                    &nbsp;meter</td>
                <td>
                    <asp:RangeValidator ID="RangeValidator3" runat="server" ControlToValidate="txtAfstand" Display="Dynamic" ErrorMessage="Onjuist aantal meters!" MaximumValue="1000" MinimumValue="1" Type="Integer">!</asp:RangeValidator>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="txtAfstand" Display="Dynamic" ErrorMessage="Afstand niet ingevuld!">*</asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td class="auto-style1">Soort Boog:</td>
                <td class="auto-style4">
                    <asp:RadioButtonList ID="RadioButtonList1" runat="server">
                        <asp:ListItem Value="Recurve">Recurve</asp:ListItem>
                        <asp:ListItem Value="Compount">Compount</asp:ListItem>
                        <asp:ListItem Value="Long Bow">Long Bow</asp:ListItem>
                        <asp:ListItem Value="Flat Bow">Flat Bow</asp:ListItem>
                    </asp:RadioButtonList>
                </td>
                <td class="auto-style6">
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="RadioButtonList1" Display="Dynamic" ErrorMessage="Het veld &quot;Soort Boog&quot; is verplicht!">*</asp:RequiredFieldValidator>
                </td>
                <td class="auto-style8">&nbsp;</td>
                <td class="auto-style2">&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style1">&nbsp;</td>
                <td class="auto-style4">&nbsp;</td>
                <td class="auto-style6">&nbsp;</td>
                <td class="auto-style8">&nbsp;</td>
                <td class="auto-style2">&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style1">
                    <asp:Button ID="btnSave" runat="server" Text="Save" Width="125px" />
                </td>
                <td class="auto-style4">
                    <asp:Label ID="lblMelding" runat="server"></asp:Label>
                </td>
                <td class="auto-style6">&nbsp;</td>
                <td class="auto-style8">&nbsp;</td>
                <td class="auto-style2">&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
        </table>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" ForeColor="White" HeaderText="U heeft een foutmelding!" Height="76px" ShowMessageBox="True" style="margin-bottom: 0px" />
</asp:Content>

我的aspx.cs代码是:

    protected void btnSave_Click(object sender, EventArgs e)
    {
        // validaties!!
        if (Page.IsValid)// de validaties zijn goed afgerond
        {
            // geselecteerde waarde van de RadioButtonList opslaan in Boog
            string boog = RadioButtonList1.SelectedValue;
            // alleen de geselecteerde waarde van de DropDownList opslaan in Categorie
            char categorie = Convert.ToChar(DropDownList1.SelectedValue);
            // datum wordt de systeemdatum
            DateTime datum = DateTime.Today;
            // naar database schrijven!!
            int score = Convert.ToInt16(txtAfstand.Text);
            int afstand = Convert.ToInt16(txtAfstand.Text);
            string conString = WebConfigurationManager.ConnectionStrings["csScore"].ConnectionString;
            SqlConnection conn = new SqlConnection(conString);
            string sqlOpdracht = "INSERT INTO t_ScoreToevoegen(ID, Voornaam, Achternaam, Boog, Categorie, Datum, Score, Afstand) value (@ID ,@Voornaam, @Achternaam, @Boog, @Categorie, @Datum, @Score, @Afstand)";
            SqlCommand cmd = new SqlCommand(sqlOpdracht, conn);
            // alle waarden in parameters zetten.
            cmd.Parameters.AddWithValue("@Voornaam", txtVoornaam.Text);
            cmd.Parameters.AddWithValue("@Achternaam", txtAchternaam.Text);
            cmd.Parameters.AddWithValue("@Boog", boog);
            cmd.Parameters.AddWithValue("@Categorie", categorie);
            cmd.Parameters.AddWithValue("@Datum", datum);
            cmd.Parameters.AddWithValue("@Score", txtScore.Text);
            cmd.Parameters.AddWithValue("@Afstand", txtAfstand.Text);
            cmd.Parameters.AddWithValue("@ID", 7000);
            try
            {
                // open de connectie
                conn.Open();
                // voer opdracht uit en geef aan hoeveel rijen zijn toegevoegd
                int aantalRijen = cmd.ExecuteNonQuery();
                lblMelding.Text = aantalRijen.ToString();
            }
            catch (Exception err)
            {
                // foutmelding
                lblMelding.Text = err.ToString();
            }
            finally
            {
                // connectie sluiten
                conn.Close();
            }
        }
        // cookie!!
        // maak een nieuw HttpCookie Object
        HttpCookie koekje = new HttpCookie("userinfo");
        // Voeg de stukje tekst aan de inhoud van de cookie toe met als naam usernaam
        koekje["voornaam"] = txtVoornaam.Text;
        koekje["achternaam"] = txtAchternaam.Text;
        koekje["categorie"] = DropDownList1.SelectedValue;
        koekje["boog"] = RadioButtonList1.SelectedValue;
        koekje["datum"] = txtDatum.Text;
        koekje["score"] = txtScore.Text;
        koekje["afstand"] = txtAfstand.Text;
        // zet de houdbaarheidsdatum op 1 uur na nu
        koekje.Expires = DateTime.Now.AddHours(1);
        // schrijf de cookie weg naar schijf
        Response.SetCookie(koekje);
        //roep een volgende .aspx pagina aan.
        Response.Redirect("~/ScoreToevoegenCookie.aspx");
    }
}

Web.config:

 <configuration>
    <connectionStrings>
        <add name="csScore" connectionString="Data Source=VAIO;Initial Catalog=ASP.NET Boogschieten Final;Integrated Security=True" />
        <add name="ASP.NET Boogschieten FinalConnectionString" connectionString="Data Source=VAIO;Initial Catalog=ASP.NET Boogschieten Final;Integrated Security=True" />
        </connectionStrings>
       <system.web>
      <compilation debug="true" targetFramework="4.5" />
  <httpRuntime targetFramework="4.5" />
  </system.web>
  <appSettings>
   <add key="ValidationSettings:UnobtrusiveValidationMode" value="None"/>
   </appSettings>
   < /configuration>

以及cookie需要访问的页面:

HTML:

  <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
       <table style="width: 100%;" id="table">
          <tr>
               <td class="auto-style1">Voornaam:</td>
               <td class="auto-style4">
                   <asp:Label ID="lblVoornaamCookie" runat="server" Text="...">   </asp:Label>
               </td>
               <td class="auto-style6">&nbsp;</td>
               <td class="auto-style8">Datum:</td>
               <td class="auto-style2">
                <asp:Label ID="lblDatumCookie" runat="server" Text="..."></asp:Label>
               </td>
               <td>
                &nbsp;</td>
            </tr>
           <tr>
               <td class="auto-style1">Achternaam:</td>
               <td class="auto-style4">
                   <asp:Label ID="lblAchternaamCookie" runat="server" Text="...">     </asp:Label>
               </td>
               <td class="auto-style6">
                &nbsp;</td>
               <td class="auto-style8">&nbsp;</td>
               <td class="auto-style2">&nbsp;</td>
               <td>&nbsp;</td>
          </tr>
           <tr>
              <td class="auto-style1">Categorie:</td>
               <td class="auto-style4">
                  <asp:Label ID="lblCategorieCookie" runat="server" Text="..."></asp:Label>
              </td>
               <td class="auto-style4">
                &nbsp;</td>
               <td class="auto-style4">Score:</td>
               <td class="auto-style5">
                   <asp:Label ID="lblScoreCookie" runat="server" Text="..."></asp:Label>
                punten</td>
               <td class="auto-style4">
                &nbsp;</td>
           </tr>
           <tr>
              <td class="auto-style1">&nbsp;</td>
              <td class="auto-style4">&nbsp;</td>
               <td class="auto-style6">&nbsp;</td>
              <td class="auto-style8">Afstand:</td>
               <td class="auto-style2">
                  <asp:Label ID="lblAfstandCookie" runat="server" Text="..."></asp:Label>
                meter</td>
              <td>
                &nbsp;</td>
           </tr>
           <tr>
               <td class="auto-style1">Soort Boog:</td>
               <td class="auto-style4">
                  <asp:Label ID="lblBoogCookie" runat="server" Text="..."></asp:Label>
               </td>
               <td class="auto-style6">
                &nbsp;</td>
               <td class="auto-style8">&nbsp;</td>
              <td class="auto-style2">&nbsp;</td>
               <td>&nbsp;</td>
          </tr>
          <tr>
               <td class="auto-style1">&nbsp;</td>
              <td class="auto-style4">&nbsp;</td>
              <td class="auto-style6">&nbsp;</td>
              <td class="auto-style8">&nbsp;</td>
              <td class="auto-style2">&nbsp;</td>
              <td>&nbsp;</td>
          </tr>
          <tr>
            <td class="auto-style1">
                <asp:Button ID="btnSave" runat="server" Text="Save" Width="125px" />
            </td>
            <td class="auto-style4">
                <asp:Label ID="lblMelding" runat="server"></asp:Label>
            </td>
            <td class="auto-style6">&nbsp;</td>
            <td class="auto-style8">&nbsp;</td>
            <td class="auto-style2">&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
    </table>
</asp:Content>

aspx.cs:

 public partial class ScoreToevoegenCookie : System.Web.UI.Page
 {
    protected void Page_Load(object sender, EventArgs e)
    {
        // maak een cookie object
        HttpCookie koekje = Request.Cookies["userinfo"];
        // controleer of er een koekje met de naam userinfo bestaat.
        if (koekje != null)
        {
            // vul een label met de inhoud van usernaam en taal
            lblVoornaamCookie.Text = koekje["voornaam"];
            lblAchternaamCookie.Text = koekje["achternaam"];
            lblCategorieCookie.Text = koekje["categorie"];
            lblBoogCookie.Text = koekje["boog"];
            lblDatumCookie.Text = koekje["datum"];
            lblScoreCookie.Text = koekje["score"];
            lblAfstandCookie.Text = koekje["afstand"];
        }
        else //koekje = null
        {
            lblVoornaamCookie.Text = "Geen cookie gevonden";
            lblAchternaamCookie.Text = "Geen cookie gevonden";
            lblCategorieCookie.Text = "Geen cookie gevonden";
            lblBoogCookie.Text = "Geen cookie gevonden";
            lblDatumCookie.Text = "Geen cookie gevonden";
            lblScoreCookie.Text = "Geen cookie gevonden";
            lblAfstandCookie.Text = "Geen cookie gevonden";
        }       
    }
}

所有的验证都在工作!

抱歉所有的代码!所有这些都在内容页面中!

谢谢!

ASP.. NET将表单数据插入SQL Server数据库不工作

问题:您在INSERT INTO语句中使用关键字VALUE而不是VALUES

解决方案:需要在INSERT INTO语句

中使用关键字VALUES

请参阅INSERT INTO Syntax

替换:

string sqlOpdracht = "INSERT INTO t_ScoreToevoegen(ID, Voornaam, Achternaam,
                                     Boog, Categorie, Datum, Score, Afstand)        
   value (@ID ,@Voornaam, @Achternaam, @Boog, @Categorie, @Datum, 
                                                           @Score, @Afstand)";
与这个:

string sqlOpdracht = "INSERT INTO t_ScoreToevoegen(ID, Voornaam, Achternaam,
                                 Boog, Categorie, Datum, Score, Afstand)        
   values (@ID ,@Voornaam, @Achternaam, @Boog, @Categorie, @Datum, 
   ^^^^^^^                                      @Score, @Afstand)";

这是一个很长的镜头,虽然你的SQL表的ID是一个主键设置自动生成?

如果是,问题可能是您试图添加一个ID,而不是让SQL为您生成它。SQL不允许你插入这个,除非你输入SET IDENTITY_INSERT MyDBName t_ScoreToevoegen ON

试着改变这个:

string sqlOpdracht = "INSERT INTO t_ScoreToevoegen(ID, Voornaam, Achternaam, Boog, Categorie, Datum, Score, Afstand) value (@ID ,@Voornaam, @Achternaam, @Boog, @Categorie, @Datum, @Score, @Afstand)";

到此-通知我删除了所有ID引用-还将value更改为values每个其他答案:

string sqlOpdracht = "INSERT INTO t_ScoreToevoegen(Voornaam, Achternaam, Boog, Categorie, Datum, Score, Afstand) values (@Voornaam, @Achternaam, @Boog, @Categorie, @Datum, @Score, @Afstand)";

并删除这个:

cmd.Parameters.AddWithValue("@ID", 7000);