插入按钮不插入数据到数据库,并在所有没有给出错误 [英] Insert button is not inserting data into the Database and no errors are given at all

查看:99
本文介绍了插入按钮不插入数据到数据库,并在所有没有给出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是首先包含insert方法我填写的字段,然后做的属性然后在插入方法和去了另一个类,并取得了插入按钮,请帮助,并在所有没有给出错误类

 使用系统;
使用System.Collections.Generic;
使用System.Data.SqlClient的;
使用System.Linq的;
使用System.Text;
使用System.Threading.Tasks;
使用的DatabaseConnection;
使用System.Data.SqlClient的;
学生空间
{
    公共类节目
    {
        //填充字段
        私人INT StudentID = 0;
        私人字符串StudentName =;
        私人INT SudentAge = 0;
        私人的SqlConnection连接=新的SqlConnection();        //属性        // 学生卡
        公众诠释StudentID1
        {
            {返回StudentID; }
            集合{StudentID =价值; }
        }        // 学生姓名
        公共字符串StudentName1
        {
            {返回StudentName; }
            集合{StudentName =价值; }
        }
        // SudentAge
        公众诠释SudentAge1
        {
            {返回SudentAge; }
            集合{SudentAge =价值; }
        }        //插入方法
        公共无效插入()
        {
            SqlConnection的连接=新的SqlConnection(DBC.Constructor);
            字符串的Sql =插入详细信息(StudentID,StudentName,SudentAge)VALUES(@ StudentID1,@ StudentName1,@ SudentAge1);
            的SqlCommand命令=新的SqlCommand(SQL,连接);
            Command.Parameters.AddWithValue(@ StudentID1,StudentID);
            Command.Parameters.AddWithValue(@ StudentName1,StudentName);
            Command.Parameters.AddWithValue(@ StudentAge1,SudentAge);
            尝试
            {
                Connection.Open();
                Command.ExecuteNonQuery();                尝试
                {
                    Console.WriteLine(执​​行成功);
                }                抓住
                {
                    Console.WriteLine(执​​行并不成功);
                }            }
            抓住
            {
                Console.WriteLine(错误保存学生);
            }
            最后
            {
                尝试
                {                    connection.close()时;
                }
                抓住
                {
                }
            }
        }

这是我的按钮类

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Web.UI程序;
使用System.Web.UI.WebControls;
使用学生;
使用System.Data.SqlClient的;
使用的DatabaseConnection;
使用System.Data这;
公共部分类注册:System.Web.UI.Page
{
    公共静态字符串构造=数据源=福阿德-PC;初始目录=学生;集成安全性=真;
    保护无效的Page_Load(对象发件人,EventArgs的发送)
    {    }
    保护无效InsertButton_Click(对象发件人,EventArgs的发送)
    {        节目X =新计划();
        X.StudentName1 = NameTxt.Text;
        X.SudentAge1 = int.Parse(AgeTxt.Text);
        X.StudentID1 = int.Parse(IDTxt.Text);        X.Insert();    }}


解决方案

//插入法

 公众诠释插入(计划程序)
    {
      INT resutl;
        SqlConnection的连接=新的SqlConnection(DBC.Constructor);
        字符串的Sql =插入详细信息(program.StudentID,program.StudentName,program.SudentAge)VALUES(@ StudentID1,@ StudentName1,@ SudentAge1);
        的SqlCommand命令=新的SqlCommand(SQL,连接);
        Command.Parameters.AddWithValue(@ StudentID1,program.StudentID);
        Command.Parameters.AddWithValue(@ StudentName1,program.StudentName);
        Command.Parameters.AddWithValue(@ StudentAge1,program.SudentAge);
        尝试
        {
            Connection.Open();
         resutl = Command.ExecuteNonQuery();            尝试
            {
                Console.WriteLine(执​​行成功);
            }            抓住
            {
                Console.WriteLine(执​​行并不成功);
            }
        返回resutl;
        }
        抓住
        {
            Console.WriteLine(错误保存学生);
        }
        最后
        {
            尝试
            {                connection.close()时;
            }
            抓住
            {
            }
        }

This is the class that contains the insert method first I filled the fields and then made properties then the insert method and went to another class and made the insert button please help and no errors are given at all

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

this is my button class

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

    }

}

解决方案

// Insert Method

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

这篇关于插入按钮不插入数据到数据库,并在所有没有给出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆