【问题标题】:Insert button is not inserting data into the Database and no errors are given at all [closed]插入按钮没有将数据插入数据库,并且根本没有给出错误[关闭]
【发布时间】:2014-01-28 08:55:22
【问题描述】:

这是包含插入方法的类,首先我填充字段,然后创建属性,然后插入方法,然后转到另一个类并制作插入按钮,请帮助,完全没有错误

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataBaseConnection;
using System.Data.SqlClient;
namespace Students
{
    public class Program
    {
        // Filling The Fields 
        private int StudentID = 0;
        private string StudentName = "";
        private  int SudentAge = 0;
        private SqlConnection Connection = new SqlConnection();

        // Properties 

        // Student ID 
        public int StudentID1
        {
            get { return StudentID; }
            set { StudentID = value; }
        }

        // Student Name 
        public string StudentName1
        {
            get { return StudentName; }
            set { StudentName = value; }
        }
        // SudentAge 


        public int SudentAge1
        {
            get { return SudentAge; }
            set { SudentAge = value; }
        }

        // Insert Method 
        public void Insert()
        {
            SqlConnection Connection = new SqlConnection(DBC.Constructor);
            string Sql = "insert into Details (StudentID,StudentName,SudentAge) Values (@StudentID1,@StudentName1,@SudentAge1)";
            SqlCommand Command = new SqlCommand(Sql, Connection);
            Command.Parameters.AddWithValue("@StudentID1", StudentID);
            Command.Parameters.AddWithValue("@StudentName1", StudentName);
            Command.Parameters.AddWithValue("@StudentAge1", SudentAge);


            try
            {
                Connection.Open();
                Command.ExecuteNonQuery();

                try
                {
                    Console.WriteLine("Execute success");
                }

                catch
                {
                    Console.WriteLine("Execute is not success");
                }

            }
            catch
            {
                Console.WriteLine("Error saving Student");
            }
            finally
            {
                try
                {

                    Connection.Close();
                }
                catch
                {
                }
            }
        }

这是我的按钮类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Students;
using System.Data.SqlClient;
using DataBaseConnection;
using System.Data;
public partial class SignUp : System.Web.UI.Page
{
    public static string Constructor = "Data Source=FOUAD-PC;Initial Catalog=Students;Integrated Security=True";
    protected void Page_Load(object sender, EventArgs e)
    {

    }


    protected void InsertButton_Click(object sender, EventArgs e)
    {

        Program X = new Program();
        X.StudentName1 = NameTxt.Text;
        X.SudentAge1 = int.Parse(AgeTxt.Text);
        X.StudentID1 = int.Parse(IDTxt.Text);

        X.Insert();

    }

}

【问题讨论】:

  • 为什么你给标签c改成c#
  • MySql 呢?看来您正在使用 Sql Server。
  • 错误已在与网页无关的Console.WriteLine 行被吃掉
  • SudentAge 是真实的列名还是您的错误(而不是StudentAge)?而且参数名也不匹配
  • try catch 里面 try catch ??

标签: c# asp.net sql


【解决方案1】:
and no errors are given at all

我的回答是,您使用Console.WriteLine 来显示错误或成功。

但是您从实际上无法访问控制台的网页内部调用此对象,并且不读取该控制台写入行。因此,当您从网页调用它时,您的错误不会显示,只有当您从控制台应用程序调用它时才会显示。

如何重写。

在你的对象中使用一个字符串来记录错误并在那里写下你的错误,例如

catch(Exception x)
{
   // change that 
   // Console.WriteLine("Execute is not success");
   // with 
   lastError = "Execute is not success - reason:" + x.ToString();
}

并且lastError 是您对象上的公共字符串,并在您拨打电话后检查它。 如果这是您的正确代码,您还有一些“正在播放”的 try/catch 根本不起作用。

【讨论】:

    【解决方案2】:

    //插入方法

    public int Insert(Program program)
        {
          int resutl;
            SqlConnection Connection = new SqlConnection(DBC.Constructor);
            string Sql = "insert into Details (program.StudentID,program.StudentName,program.SudentAge) Values (@StudentID1,@StudentName1,@SudentAge1)";
            SqlCommand Command = new SqlCommand(Sql, Connection);
            Command.Parameters.AddWithValue("@StudentID1", program.StudentID);
            Command.Parameters.AddWithValue("@StudentName1", program.StudentName);
            Command.Parameters.AddWithValue("@StudentAge1", program.SudentAge);
    
    
            try
            {
                Connection.Open();
             resutl=   Command.ExecuteNonQuery();
    
                try
                {
                    Console.WriteLine("Execute success");
                }
    
                catch
                {
                    Console.WriteLine("Execute is not success");
                }
            Return resutl;
            }
            catch
            {
                Console.WriteLine("Error saving Student");
            }
            finally
            {
                try
                {
    
                    Connection.Close();
                }
                catch
                {
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-17
      • 1970-01-01
      • 1970-01-01
      • 2019-06-18
      • 2019-03-16
      • 1970-01-01
      • 2013-06-07
      • 1970-01-01
      相关资源
      最近更新 更多