Saturday, 15 August 2015

c# - ASP.NET insert form data into SQL Server database doesn't work -



c# - ASP.NET insert form data into SQL Server database doesn't work -

i writing empty webform application using asp.net 4.5 , have problem inserting info form, made, sql server database table. think code doesn't work. need create cookie on page neither works. can please help me? work master pages , content pages. problem content page think.

my html code of page is:

<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; 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; 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; 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>

and aspx.cs code is:

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 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); seek { // open de connectie conn.open(); // voer opdracht uit en geef aan hoeveel rijen zijn toegevoegd int aantalrijen = cmd.executenonquery(); lblmelding.text = aantalrijen.tostring(); } grab (exception err) { // foutmelding lblmelding.text = err.tostring(); } { // 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>

and page cookie needs go to:

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"; } } }

and validation working!

sorry code! in content pages!

thanks!

problem : using keyword value instead of values in insert into statement.

solution: need utilize keyword values in insert into statement

please refer link insert syntax

replace this:

string sqlopdracht = "insert t_scoretoevoegen(id, voornaam, achternaam, boog, categorie, datum, score, afstand) value (@id ,@voornaam, @achternaam, @boog, @categorie, @datum, @score, @afstand)";

with this:

string sqlopdracht = "insert t_scoretoevoegen(id, voornaam, achternaam, boog, categorie, datum, score, afstand) values (@id ,@voornaam, @achternaam, @boog, @categorie, @datum, ^^^^^^^ @score, @afstand)";

c# html asp.net sql-server

No comments:

Post a Comment