“插入”按钮不会将数据插入数据库,也不会发生任何错误

时间:2014-01-28 08:55:22

标签: c# asp.net sql

这是包含insert方法的类,我首先填充字段,然后创建属性然后插入方法,然后转到另一个类并使插入按钮请求帮助,并且根本没有给出错误

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

    }

}

2 个答案:

答案 0 :(得分: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是您对象上的公共字符串,并在您拨打电话后进行检查。 如果这是你的正确代码,你还有一些“无线”尝试/捕获根本不起作用。

答案 1 :(得分:1)

//插入方法

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
            {
            }
        }