让我重新提出问题
我有一个ASP.Net页面,其中包含数据字段和2个按钮,用于检查公司是否存在(此工作正常)。
按钮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>
但是这个dosnt工作,它会执行插入,也不会将标签16文本更改为数据保存或数据未保存。
有人可以指出按钮无效的原因。
一些见解SQL和SQL连接不是我正常的编程区域,正常构建基本的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
}
}
}
}
答案 0 :(得分:1)
尝试:
cmd.CommandType = CommandType.Text;
cmd.CommandText = "INSERT INTO Data Values (@BEDRIJF,@VOORNAAM)";
cmd.Parameters.AddWithValue("@BEDRIJF", BEDRIJF);
cmd.Parameters.AddWithValue("@VOORNAAM", VOORNAAM);
不要在引号中包含您的参数。
Why I'm getting CS1012: "Too many characters in character literal" and CS0019?
答案 1 :(得分:0)
我认为你正在使用参数智能插入
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);