我已经在这里工作了大约3个星期,并且已经阅读并观看了YouTube中的很多视频,我跟着它去了T但是没有一个适合我。此时我正在拔毛。
网站在网页浏览器中显示正常,但是当文本框被填满并且提交按钮被命中时数据库保持空白。
所以这就是我所拥有的:
=========================== 原创 ============ ===============
FirstName
MiddleName
LastName
Email
Phone
BTTR
BDTR
Issues
网页
First Name:<asp:TextBox ID="First_Name" runat="server" placeholder="Your First Name!" />
Middle Name:<asp:TextBox ID="Middle_Name" runat="server" placeholder="Optional" />
Last Name:<asp:TextBox ID="Last_Name" runat="server" placeholder="Last Name" />
Email Address:<asp:TextBox ID="Email" runat="server" placeholder="yourname@domain.com" />
Phone:<asp:TextBox ID="Phone" runat="server" placeholder="(XXX) XXX-XXXX" />
Good day to reach you:
<asp:DropDownList ID="BDTR" runat="server">
<asp:ListItem>Mondays</asp:ListItem>
<asp:ListItem>Tuesdays</asp:ListItem>
<asp:ListItem>Wednesdays</asp:ListItem>
<asp:ListItem>Thursdays</asp:ListItem>
<asp:ListItem>Fridays</asp:ListItem>
<asp:ListItem>Saturdays</asp:ListItem>
<asp:ListItem>Sundays</asp:ListItem>
</asp:DropDownList>
Good time to reach you:
<asp:DropDownList runat="server" name="BTTR" ID="BTTR">
<asp:ListItem Text="Select One" Value="1">Select One</asp:ListItem>
<asp:ListItem Text="6-8" Value="2">6am - 8am </asp:ListItem>
<asp:ListItem Text="8-10" Value="3">8am - 10am</asp:ListItem>
<asp:ListItem Text="10-12" Value="4">10am - Noon</asp:ListItem>
<asp:ListItem Text="12-2" Value="5">Noon - 2pm</asp:ListItem>
<asp:ListItem Text="2-4" Value="6">2pm - 4pm</asp:ListItem>
<asp:ListItem Text="4-6" Value="7">4pm - 6pm</asp:ListItem>
</asp:DropDownList>
Please describe your health issues:
<asp:TextBox runat="server" name="Issues" id="Issues" placeholder="Examples: Remember the more details you can provide the better we can help you" />
<asp:Button ID="Submit" runat="server" Text="Submit" CssClass="special" /><asp:Label ID="ConfLabel" runat="server" Font-Bold="True" ForeColor="Red"></asp:Label></li>
代码页
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class Survey_Build_Platform : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == true)
{
ConfLabel.Text = ("*** Thank you - Your request has been submitted");
}
}
protected void Submit_Click(object sender, EventArgs e)
{
SqlConnection cnn = new SqlConnection("Data Source=win-ngvi5b6ngjv/sqlexpress;Initial Catalog=MOBILEMED;Integrated Security=True");
{
SqlCommand xp = new SqlCommand("INSERT INTO 3DayTrial(RefferalName, ReferralCodeValues(@First_Name, @Middle_Name, @Last_Name, @Email, @Phone, @BTTR, @BDTR, @Issues)", cnn);
xp.Parameters.AddWithValue("@First_Name", First_Name.Text);
xp.Parameters.AddWithValue("@Middle_Name", Middle_Name.Text);
xp.Parameters.AddWithValue("@Last_Name", Last_Name.Text);
xp.Parameters.AddWithValue("@eMail", Email.Text);
xp.Parameters.AddWithValue("@CellPhone", Cell_Phone.Text);
xp.Parameters.AddWithValue("@BTTR", BTTR.Text);
xp.Parameters.AddWithValue("@BDTR", BDTR.Text);
xp.Parameters.AddWithValue("@Issues", Goals.Text);
cnn.Open();
xp.ExecuteNonQuery();
cnn.Close();
}
}
}
=========================== 已修改 ============ ===============
软件:
数据库列字段:
ID --- (INT)
FirstName --- NvarChar (50, no Nulls)
MiddleName --- NvarChar (50)
LastName --- NvarChar (50, no Nulls)
Email --- NvarChar (50, no Nulls)
Phone --- NvarChar (50, no Nulls)
GTTR --- NvarChar (50, no Nulls)
GDTR --- NvarChar (50, no Nulls)
Issues --- NvarChar (MAX)
页
First Name:<asp:TextBox ID="First_Name" runat="server" placeholder="Your First Name!" />
Middle Name:<asp:TextBox ID="Middle_Name" runat="server" placeholder="Optional" />
Last Name:<asp:TextBox ID="Last_Name" runat="server" placeholder="Last Name" />
Email Address:<asp:TextBox ID="Email" runat="server" placeholder="yourname@domain.com" />
Phone:<asp:TextBox ID="Phone" runat="server" placeholder="(XXX) XXX-XXXX" />
Good day to reach you:
<asp:DropDownList ID="GDTR" runat="server">
<asp:ListItem>Mondays</asp:ListItem>
<asp:ListItem>Tuesdays</asp:ListItem>
<asp:ListItem>Wednesdays</asp:ListItem>
<asp:ListItem>Thursdays</asp:ListItem>
<asp:ListItem>Fridays</asp:ListItem>
<asp:ListItem>Saturdays</asp:ListItem>
<asp:ListItem>Sundays</asp:ListItem>
</asp:DropDownList>
Good time to reach you:
<asp:DropDownList runat="server" name="GTTR" ID="BTTR">
<asp:ListItem Text="Select One" Value="1">Select One</asp:ListItem>
<asp:ListItem Text="6-8" Value="2">6am - 8am </asp:ListItem>
<asp:ListItem Text="8-10" Value="3">8am - 10am</asp:ListItem>
<asp:ListItem Text="10-12" Value="4">10am - Noon</asp:ListItem>
<asp:ListItem Text="12-2" Value="5">Noon - 2pm</asp:ListItem>
<asp:ListItem Text="2-4" Value="6">2pm - 4pm</asp:ListItem>
<asp:ListItem Text="4-6" Value="7">4pm - 6pm</asp:ListItem>
</asp:DropDownList>
Please describe your health issues:
<asp:TextBox runat="server" name="Issues" id="Issues" placeholder="Examples: Remember the more details you can provide the better we can help you" />
<asp:Button ID="Submit" runat="server" Text="Submit" CssClass="special" onclick="Submit_Click" /><asp:Label ID="ConfLabel" runat="server" Font-Bold="True" ForeColor="Red"></asp:Label>
代码页
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class Survey_Build_Platform : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == true)
{
ConfLabel.Text = ("*** Thank you - Your request has been submitted");
}
}
protected void Submit_Click(object sender, EventArgs e)
{
SqlConnection cnn = new SqlConnection("Data Source=win-ngvi5b6ngjv/sqlexpress;Initial Catalog=SMCMEDC;Integrated Security=True");
{
SqlCommand xp = new SqlCommand("INSERT INTO SMCMEDC (First_Name, Middle_Name, Last_Name, Email, Phone, GTTR, GDTR, Goals) VALUES (@First_Name, @Middle_Name, @Last_Name, @Email, @Phone, @GTTR, @GDTR, @Issues)", cnn);
xp.Parameters.AddWithValue("@FirstName", First_Name.Text);
xp.Parameters.AddWithValue("@MiddleName", Middle_Name.Text);
xp.Parameters.AddWithValue("@LastName", Last_Name.Text);
xp.Parameters.AddWithValue("@Email", Email.Text);
xp.Parameters.AddWithValue("@Phone", Cell_Phone.Text);
xp.Parameters.AddWithValue("@GTTR", GTTR.Text);
xp.Parameters.AddWithValue("@GDTR", GDTR.Text);
xp.Parameters.AddWithValue("@Issues", Goals.Text);
cnn.Open();
xp.ExecuteNonQuery();
int count = xp.ExecuteNonQuery();
if (count != 1)
{
ConfLabel.Text = ("*** Something went wrong");
}
cnn.Close();
}
}
}
答案 0 :(得分:4)
INSERT INTO 3DayTrial(RefferalName, ReferralCodeValues(@First_Name, @Middle_Name, @Last_Name, @Email, @Phone, @GTTR, @GDTR, @Issues)
这对我来说看起来不像是有效的SQL。插入语句通常具有
之一所以我希望你的SQL看起来像这样:
INSERT INTO 3DayTrial
VALUES (@First_Name, @Middle_Name, @Last_Name, @Email, @Phone, @GTTR, @GDTR, @Issues)
或
INSERT INTO 3DayTrial
SELECT @First_Name, @Middle_Name, @Last_Name, @Email, @Phone, @GTTR, @GDTR, @Issues
您可能还需要一个字段列表 - 但我们无法知道,因为您没有向我们展示非常重要的信息。表名和表创建语句 - 要真正理解发生了什么,我们也需要看到它。
考虑一些错误检查。这条线
xp.ExecuteNonQuery();
应该是
int count = xp.ExecuteNonQuery();
if (count != 1)
{
// handle insert error
}
如果您正在调用返回值的函数,我建议您始终检查返回值。不会有任何伤害,你会编写更好的代码。
答案 1 :(得分:1)
您没有为按钮定义点击方法。按照编码,此按钮除了导致回发之外不会执行任何操作。 Submit_Click方法将不会执行。您需要阅读并了解调试中的断点。如果您已经完成了代码,那么您会发现您的方法永远不会触发。
您需要在按钮代码中添加onclick="Submit_Click"
。
然后您很快就会发现Hogan提供的答案将解决点击事件发生时遇到的问题。