C# ASP.NET将表单数据插入SQL Server数据库不';行不通
我正在使用ASP.NET 4.5编写一个空的webform应用程序,在将我制作的表单中的数据插入SQL Server数据库表时遇到问题。我认为所有的代码都是好的,但不起作用。我还需要在这个页面上制作一个cookie,但这两个都不起作用。你能帮帮我吗?我使用母版页和内容页。我想问题在于我的内容页 我的页面HTML代码为:C# ASP.NET将表单数据插入SQL Server数据库不';行不通,c#,html,asp.net,sql-server,C#,Html,Asp.net,Sql Server,我正在使用ASP.NET 4.5编写一个空的webform应用程序,在将我制作的表单中的数据插入SQL Server数据库表时遇到问题。我认为所有的代码都是好的,但不起作用。我还需要在这个页面上制作一个cookie,但这两个都不起作用。你能帮帮我吗?我使用母版页和内容页。我想问题在于我的内容页 我的页面HTML代码为: <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"&g
<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"> </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 "Achternaam" is verplicht!">*</asp:RequiredFieldValidator>
</td>
<td class="auto-style8"> </td>
<td class="auto-style2"> </td>
<td> </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 "Categorie" 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>
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"> </td>
<td class="auto-style4"> </td>
<td class="auto-style6"> </td>
<td class="auto-style8">Afstand:</td>
<td class="auto-style2">
<asp:TextBox ID="txtAfstand" runat="server" Width="60px"></asp:TextBox>
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 "Soort Boog" is verplicht!">*</asp:RequiredFieldValidator>
</td>
<td class="auto-style8"> </td>
<td class="auto-style2"> </td>
<td> </td>
</tr>
<tr>
<td class="auto-style1"> </td>
<td class="auto-style4"> </td>
<td class="auto-style6"> </td>
<td class="auto-style8"> </td>
<td class="auto-style2"> </td>
<td> </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"> </td>
<td class="auto-style8"> </td>
<td class="auto-style2"> </td>
<td> </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>
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>
所有的验证都在工作
对不起,所有的代码!所有这些都在内容页中
谢谢 问题:您在
插入到
语句中使用的是关键字值
,而不是值
解决方案:您需要在插入到
语句中使用关键字值
有关详细信息,请参阅此链接
替换此项:
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将不允许您插入此项,除非您设置标识\u insert MyDBName t\u 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
更改为value
:
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);
请描述你所说的“不工作”是什么意思。它抛出异常?数据库中插入了错误的数据?@kayson:如果我按保存按钮上的按钮(按我创建的格式),则不会将任何数据添加到我的SQL数据库中。数据应该添加到我的SQL数据库中,但我的应用程序没有这样做。谢谢你的回答。但我试过了,但还是不行。我的数据库中没有添加任何行:S@Marjolein:好的,你有什么错误吗?如果是,请提供错误详细信息。否没有错误它只是不起作用:S我在替换代码后确实运行了我的应用程序,但没有显示错误。我单击了“在我的表单中保存”,但它没有将数据插入我的数据库。@Marjolein:它解决了您的问题吗,如果没有,请取消选中它作为答案。@Marjolein:您应该能够在
lblMelding
label中看到异常详细信息。您能调试并查看吗?我尝试了,但也没有成功。:'(并且没有显示错误…好的,我必须在代码中设置还是必须在SQL DB中设置?:如果这是一个愚蠢的问题,那很抱歉,但我对ASP.NET有点陌生,我只上了几节课。我现在有:`string sqlOpdracht=“set IDENTITY”\u INSERT ASP.NET boogschieen Final t_scoreToevogen ON;INSERT in t_scoreToevogen(ID,Voornaam,Achternaam,Boog,Categorie,Datum,Score,Afstand)值(@ID,@Voornaam,@Achternaam,@Boog,@Categorie,@Datum,@Score,@Afstand);设置IDENTITY\u INSERT ASP.NET boogschieen Final t(u scoreToevogen OFF);`在我的代码中,但仍然不起作用:(好的-去掉那部分代码-不确定它是否能工作。你让SQL设置你的ID。在SQL中,当设计表时,将ID设置为Int和主键并自动递增。如果你不确定,谷歌怎么做。)。。
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(Voornaam, Achternaam, Boog, Categorie, Datum, Score, Afstand) values (@Voornaam, @Achternaam, @Boog, @Categorie, @Datum, @Score, @Afstand)";
cmd.Parameters.AddWithValue("@ID", 7000);