[英]MSSQL asp.net insert to table doesnt work
讓我重新思考這個問題
我有一個帶數據字段和2個按鈕的ASP.Net頁,一個用於檢查公司是否存在(此方法有效)。
按鈕2需要插入值
[ID]
,[BEDRIJF]
,[FUNCTIE]
,[TITEL]
,[VOORNAAM]
,[VOORLETTERS]
,[TUSSENVOEGSEL]
,[ACHTERNAAM]
,[SEXE]
,[ADRES]
,[PCPLAATS]
,[LAND]
,[EMAIL]
,[BRANCHE]
,[TELEFOON]
,[MOBIEL]
,[DATUM_BRIEF]
,[VEBAN]
,[PAYOFF]
從文本框單選按鈕和頁面上的下拉列表中。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<asp:Image ID="Image1" runat="server" ImageUrl="~/images/jubels_logo_vlakje_magenta.png" style="margin-left: 0px" />
<form id="form1" runat="server">
<div style="margin-left: 80px">
<asp:Label ID="Label1" runat="server" Text="Ingevoerd Door :"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" ReadOnly="True"></asp:TextBox>
<br />
<asp:Label ID="Label2" runat="server" Text="Bedrijfsnaam :"></asp:Label>
<asp:TextBox ID="TextBox17" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label3" runat="server" Text="Branche"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="BRANCHE" DataValueField="BRANCHE">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:FUP-DataConnectionString %>" SelectCommand="SELECT [BRANCHE] FROM [Branches]"></asp:SqlDataSource>
<br />
<asp:Label ID="Label4" runat="server" Text="Geslacht"></asp:Label>
<asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow">
<asp:ListItem Value="M">Man</asp:ListItem>
<asp:ListItem Value="V">Vrouw</asp:ListItem>
</asp:RadioButtonList>
<br />
<asp:Label ID="Label5" runat="server" Text="Voorletters "></asp:Label>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label6" runat="server" Text="Voornaam "></asp:Label>
<asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label7" runat="server" Text="Tussenvoegsel"></asp:Label>
<asp:TextBox ID="TextBox7" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label8" runat="server" Text="Achternaam "></asp:Label>
<asp:TextBox ID="TextBox8" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label9" runat="server" Text="Functie"></asp:Label>
<asp:TextBox ID="TextBox9" runat="server"></asp:TextBox>
<br />
<br />
<asp:Label ID="Label10" runat="server" Text="Straat - huisnummer"></asp:Label>
<asp:TextBox ID="TextBox11" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox10" runat="server" Width="16px"></asp:TextBox>
<br />
<asp:Label ID="Label11" runat="server" Text="Postcode - plaats"></asp:Label>
<asp:TextBox ID="TextBox12" runat="server" Width="41px"></asp:TextBox>
<asp:TextBox ID="TextBox13" runat="server"></asp:TextBox>
<br />
<br />
<asp:Label ID="Label12" runat="server" Text="Telefoon"></asp:Label>
<asp:TextBox ID="TextBox14" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label13" runat="server" Text="Mobiel"></asp:Label>
<asp:TextBox ID="TextBox15" runat="server">06</asp:TextBox>
<br />
<asp:Label ID="Label14" runat="server" Text="Email"></asp:Label>
<asp:TextBox ID="TextBox16" runat="server"></asp:TextBox>
<br />
<br />
<asp:Label ID="Label15" runat="server" Text="Brieftext"></asp:Label> <asp:RadioButtonList ID="RadioButtonList3" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow" style="margin-bottom: 0px">
<asp:ListItem>Normaal</asp:ListItem>
<asp:ListItem>EX-Klanten/Propects</asp:ListItem>
<asp:ListItem>Vormgever</asp:ListItem>
<asp:ListItem>Fotograaf</asp:ListItem>
</asp:RadioButtonList>
<br />
<asp:Label ID="Label17" runat="server" Text="Ondertekening"></asp:Label>
<asp:RadioButtonList ID="RadioButtonList4" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow">
<asp:ListItem>Fred</asp:ListItem>
<asp:ListItem>Jeroen</asp:ListItem>
<asp:ListItem>Fons</asp:ListItem>
</asp:RadioButtonList>
<br />
<asp:Button ID="Button1" runat="server" Text="Check Database op bestaand record" OnClick="Button1_Click" /><asp:Button ID="Button2" runat="server" Text="Voeg toe aan Database" OnClick="Button2_Click" />
<br />
<br />
<asp:Label ID="Label16" runat="server" Text=""></asp:Label>
<br />
</div>
</form>
</body>
</html>
但這確實有效,它不會插入,也不會將標簽16文本更改為“已保存數據”或“未保存數據”。
有人可以指出為什么按鈕不起作用。
在正常的編程領域中,通常會有一些洞察力強的SQL和SQL連接,通常按照同事的要求構建具有簡單布局和simpel功能的基本Windows應用程序。
aspx.cs文件中的按鈕單擊功能
protected void Button2_Click(object sender, EventArgs e)
{
string connString = ConfigurationManager.ConnectionStrings["FUP-DataConnectionString"].ConnectionString;
SqlConnection conn = null;
try
{
SqlCommand cmd = new SqlCommand("sp_insert", con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@BEDRIJF", TextBox17.Text);
cmd.Parameters.AddWithValue("@VOORNAAM", TextBox6.Text);
cmd.Parameters.AddWithValue("@ACHTERNAAM", TextBox8.Text);
con.Open();
int i = cmd.ExecuteNonQuery();
if (i != 1)
{
Label16.Text = ("Data Saved");
Response.AppendHeader("Refresh", "5;url=Default2.aspx");
}
else
{
Label16.Text = ("Data Not Saved");
Response.AppendHeader("Refresh", "5;url=Default2.aspx");
}
}
catch (Exception ex)
{
//log error
//display friendly error to user
}
finally
{
if (conn != null)
{
//cleanup connection i.e close
}
}
}
}
嘗試:
cmd.CommandType = CommandType.Text;
cmd.CommandText = "INSERT INTO Data Values (@BEDRIJF,@VOORNAAM)";
cmd.Parameters.AddWithValue("@BEDRIJF", BEDRIJF);
cmd.Parameters.AddWithValue("@VOORNAAM", VOORNAAM);
不要在逗號中包含參數。
正如我所看到的,您正在使用參數明智插入
string BEDRIJF = TextBox17.Text;
string VOORNAAM = TextBox6.Text; // Scrub user data
string VOORLETTERS = TextBox5.Text;
string Tussenvoegsel = TextBox7.Text;
string connString = ConfigurationManager.ConnectionStrings["FUP-DataConnectionString"].ConnectionString;
SqlConnection conn = null;
try
{
conn = new SqlConnection(connString);
conn.Open();
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandType = CommandType.Text;
cmd.CommandText = "INSERT INTO Data Values ('@BEDRIJF','@VOORNAAM')";
cmd.Parameters.AddWithValue("@BEDRIJF", BEDRIJF);
cmd.Parameters.AddWithValue("@VOORNAAM", VOORNAAM);
int rowsAffected = cmd.ExecuteNonQuery();
if (rowsAffected == 1)
{
//Success notification
}
else
{
//Error notification
}
}
}
catch (Exception ex)
{
//log error
//display friendly error to user
}
finally
{
if (conn != null)
{
//cleanup connection i.e close
}
看到這部分你錯了
cmd.CommandText = "INSERT INTO Data Values ('@BEDRIJF','@VOORNAAM')";
cmd.Parameters.AddWithValue("@BEDRIJF", BEDRIJF);
cmd.Parameters.AddWithValue("@VOORNAAM", VOORNAAM);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.