C#web表单提交的数据无法插入sql数据库

C#web表单提交的数据无法插入sql数据库,c#,sql,asp.net,webforms,C#,Sql,Asp.net,Webforms,我只是一个初学者,试图遵循教程。我确实搜索了最接近的问题,试了几个。它们都不适合我。我只需要一些可能出错的线索。谢谢 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient; nam

我只是一个初学者,试图遵循教程。我确实搜索了最接近的问题,试了几个。它们都不适合我。我只需要一些可能出错的线索。谢谢

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace Company
{
    public partial class ContactUs : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void update(string FirstName, string LastName, string EmailAddress, string MobileNumber, string category, string message)
        {
            SqlConnection conn = new SqlConnection(GetConnectionString());
            string sql = "INSERT INTO ContactInformation (GUID,FirstName, LastName, EmailAddress, Number, Category, Message) VALUES "
                        + " (@GUID,@FirstName,@LastName,@EmailAddress,@Number,@category,@message)";

              Guid guid = Guid.NewGuid();
              string guidString = guid.ToString();

            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlParameter[] p = new SqlParameter[7];
                p[0] = new SqlParameter("@GUID", SqlDbType.UniqueIdentifier,50);
                p[1] = new SqlParameter("@FirstName", SqlDbType.VarChar,50);
                p[2] = new SqlParameter("@LastName", SqlDbType.VarChar,50);
                p[3] = new SqlParameter("@EmailAddress", SqlDbType.VarChar,50);
                p[4] = new SqlParameter("@Number", SqlDbType.VarChar,50);
                p[5] = new SqlParameter("@Category", SqlDbType.VarChar,50);
                p[6] = new SqlParameter("@Message", SqlDbType.VarChar,50);
                p[0].Value = guid;
                p[1].Value = FirstName;
                p[2].Value = LastName;
                p[3].Value = EmailAddress;
                p[4].Value = MobileNumber;
                p[5].Value = category;
                p[6].Value = message;

                for (int i = 0; i < p.Length; i++)
                {
                    cmd.Parameters.Add(p[i]);
                }
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                string msg = "Insert Error:";
                msg += ex.Message;
                throw new Exception(msg);
            }
            finally
            {
                conn.Close();
            }
        }

        public string GetConnectionString()
        {
            return System.Configuration.ConfigurationManager.ConnectionStrings["ContactInformation"].ConnectionString;
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (!IsPostBack) 
            {
                Validate();
                update(txtFirstName.Text,
                                  txtLastName.Text,
                                  txtEmail.Text,
                                  txtNumber.Text,
                                  DropDownList1.SelectedItem.Text,
                                  txtMessage.Text);
                Response.Write("Record was successfully added!");
                //ClearForm(Page);
            }
        }
        public static void ClearForm(Control Parent)
        {
            if (Parent is TextBox)
            { (Parent as TextBox).Text = string.Empty; }
            else
            {
                foreach (Control c in Parent.Controls)
                    ClearForm(c);
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用系统数据;
使用System.Data.SqlClient;
名称空间公司
{
公共部分类联系人:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
}
公共void更新(字符串FirstName、字符串LastName、字符串EmailAddress、字符串MobileNumber、字符串类别、字符串消息)
{
SqlConnection conn=新的SqlConnection(GetConnectionString());
string sql=“插入联系人信息(GUID、名字、姓氏、电子邮件地址、号码、类别、消息)值”
+“(@GUID、@FirstName、@LastName、@EmailAddress、@Number、@category、@message)”;
Guid=Guid.NewGuid();
字符串guidString=guid.ToString();
尝试
{
conn.Open();
SqlCommand cmd=新的SqlCommand(sql,conn);
SqlParameter[]p=新的SqlParameter[7];
p[0]=新的SqlParameter(“@GUID”,SqlDbType.UniqueIdentifier,50);
p[1]=新的SqlParameter(“@FirstName”,SqlDbType.VarChar,50);
p[2]=新的SqlParameter(“@LastName”,SqlDbType.VarChar,50);
p[3]=新的SqlParameter(“@EmailAddress”,SqlDbType.VarChar,50);
p[4]=新的SqlParameter(“@Number”,SqlDbType.VarChar,50);
p[5]=新的SqlParameter(“@Category”,SqlDbType.VarChar,50);
p[6]=新的SqlParameter(“@Message”,SqlDbType.VarChar,50);
p[0]。值=guid;
p[1].Value=FirstName;
p[2].Value=LastName;
p[3]。值=电子邮件地址;
p[4].值=MobileNumber;
p[5]。值=类别;
p[6]。值=消息;
对于(int i=0;i
连接字符串位于web.config文件中

<connectionStrings>
    <add name="ContactInformation" connectionString="datasource=database;initial catalog=ContactInformation;Integrated Security=SSPI;userid=xxx;password=xxx;"/>
</connectionStrings>


我在本地计算机上部署了该网站,并尝试在Internet Explorer上进行测试。另一方面,我打开了数据库管理工具,表仍然为空。

您正在单击按钮测试回发。这可能是真的,不是假的。将您的方法更改为此。如果您看到它写出了“postback”字符串,您需要将支票改为IsPostBack而不是!IsPostBack 另外,我不确定Validate()是什么,也没有看到它的定义。可能会导致异常。如果您在测试插入时遇到问题,请将其注释掉

protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (!IsPostBack) 
            {
                Validate();
                update(txtFirstName.Text,
                                  txtLastName.Text,
                                  txtEmail.Text,
                                  txtNumber.Text,
                                  DropDownList1.SelectedItem.Text,
                                  txtMessage.Text);
                Response.Write("Record was successfully added!");
                //ClearForm(Page);
            } 
            else Response.Write("Postback");
        }

您正在单击按钮测试回发。这可能是真的,不是假的。将您的方法更改为此。如果您看到它写出了“postback”字符串,您需要将支票改为IsPostBack而不是!IsPostBack 另外,我不确定Validate()是什么,也没有看到它的定义。可能会导致异常。如果您在测试插入时遇到问题,请将其注释掉

protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (!IsPostBack) 
            {
                Validate();
                update(txtFirstName.Text,
                                  txtLastName.Text,
                                  txtEmail.Text,
                                  txtNumber.Text,
                                  DropDownList1.SelectedItem.Text,
                                  txtMessage.Text);
                Response.Write("Record was successfully added!");
                //ClearForm(Page);
            } 
            else Response.Write("Postback");
        }

嗨@MethodMan抱歉我不熟悉发帖。我刚刚编辑了我的代码。我没有收到代码错误。我无法向数据库中插入任何数据。您是否尝试过使用调试器逐行检查代码?它是否遵循了您期望的代码路径?我认为您应该看看如何正确创建参数,当您逐步完成代码时,您应该能够计算和所有参数值。还要在这一行上放置一个断点,看看它是否命中这一行
string msg=“Insert Error:”;1
您还应该使用(){}
将Sql对象包装在一个
上。您没有处理所有创建的对象,这意味着“无法插入”是什么意思?你有错误吗?您的帖子并添加错误消息。或者它没有抛出错误,但您认为数据没有插入?你怎么会这样想?你有一个按钮点击中没有回发的检查。我很确定这是真的;点击按钮将是回发!iPostBack用于页面加载,而不是按钮单击。嗨@MethodMan抱歉,我不熟悉发布。我刚刚编辑了我的代码。我没有收到代码错误。我只是无法将任何数据插入数据库。H