【问题标题】:C# web form submitted data could not insert sql databaseC# web表单提交数据无法插入sql数据库
【发布时间】:2016-08-30 17:34:08
【问题描述】:

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

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

连接字符串在 web.config 文件中。

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

我在本地机器上部署了网站,并尝试在Internet Explorer上测试它。另一方面,我打开数据库管理工具,表仍然是空的。

【问题讨论】:

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

标签: c# sql asp.net webforms


【解决方案1】:

您正在测试按钮单击中的回发。它可能是 TRUE 而不是 FALSE。将您的方法更改为此。如果您看到它写出“回发”字符串,则需要将支票更改为 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");
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-15
    • 2019-01-19
    • 2016-04-03
    相关资源
    最近更新 更多