用于在asp.net中插入,更新,删除,查找操作的代码 [英] code for insert,update,delete,find operations in asp.net

查看:56
本文介绍了用于在asp.net中插入,更新,删除,查找操作的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写的上述操作的代码将在下面执行







i wrote the code for the above operations to be performed is below



public partial class insertupdatedeletepractice : System.Web.UI.Page
{
    string cs = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    SqlConnection con; SqlCommand cmd; SqlDataSource ds; SqlParameter P1;
       
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            SqlConnection con = new SqlConnection(cs);
            SqlCommand cmd = new SqlCommand("InsertRecord", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@PEmployeeId", TextBox1.Text);
            cmd.Parameters.AddWithValue("@PEmployeeName", TextBox2.Text);
            cmd.Parameters.AddWithValue("@PEmployeeDesignation", TextBox3.Text);
            cmd.Parameters.AddWithValue("@PEmployeeDOJ", TextBox4.Text);
            cmd.Parameters.AddWithValue("@PEmployeeSalary", TextBox5.Text);
            cmd.Parameters.AddWithValue("@PEmployeeDepartmentno", TextBox6.Text);
            con.Open();
            int i = cmd.ExecuteNonQuery();
            con.Close();
            Console.WriteLine("Data Inserted");
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
 
}
    protected void btnupdate_Click(object sender, EventArgs e)
    {
        try
        {
            SqlConnection con = new SqlConnection(cs);
            SqlCommand cmd = new SqlCommand("UpdateRecord", con);
            cmd.CommandType = CommandType.StoredProcedure;
            P1 = new SqlParameter("@PEmployeeId", SqlDbType.Int);
            P1.Value = Convert.ToInt32(TextBox1.Text);
            cmd.Parameters.Add(P1);
            P1 = new SqlParameter("@PEmployeeDesignation", SqlDbType.VarChar);
            P1.Value = Convert.ToInt32(TextBox2.Text);
            cmd.Parameters.Add(P1);
            P1 = new SqlParameter("@PEmployeeDOJ", SqlDbType.Date);
            P1.Value = Convert.ToInt32(TextBox3.Text);
            cmd.Parameters.Add(P1);
            P1 = new SqlParameter("@PEmployeeDesignation", SqlDbType.VarChar);
            P1.Value = Convert.ToInt32(TextBox4.Text);
            cmd.Parameters.Add(P1);
            P1 = new SqlParameter("@PEmployeeSalary", SqlDbType.Money);
            P1.Value = Convert.ToInt32(TextBox5.Text);
            cmd.Parameters.Add(P1);
            P1 = new SqlParameter("@PEmployeeDepartmentno", SqlDbType.Int);
            P1.Value = Convert.ToInt32(TextBox6.Text);
            cmd.Parameters.Add(P1);
            con.Open();
            int i = cmd.ExecuteNonQuery();
            con.Close();
            Console.WriteLine("Data Updated");
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

    }
    protected void btnfind_Click(object sender, EventArgs e)
    {
        try
        {

            SqlConnection con = new SqlConnection(cs);
            SqlCommand cmd = new SqlCommand("GetData", con);
            cmd.CommandType = CommandType.StoredProcedure;
            P1 = new SqlParameter("@PEmployeeId",SqlDbType.Int);
            P1.Value = Convert.ToInt32(TextBox1.Text);
            cmd.Parameters.Add(P1);
            P1 = new SqlParameter("@PEmployeeName", SqlDbType.VarChar);
            P1.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(P1);
            P1 = new SqlParameter("@PEmployeeDesignation", SqlDbType.VarChar);
            P1.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(P1);
            P1 = new SqlParameter("@PEmployeeDOJ", SqlDbType.Date);
            P1.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(P1);
            P1 = new SqlParameter("@PEmployeeSalary", SqlDbType.Money);
            P1.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(P1);
            P1 = new SqlParameter("@PEmployeeDepartmentno", SqlDbType.Int);
            P1.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(P1);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            TextBox2.Text = cmd.Parameters["@PEmployeeName"].ToString();
            TextBox3.Text = cmd.Parameters["@PEmployeeDesignation"].ToString();
            TextBox4.Text = cmd.Parameters["@PEmployeeDOJ"].ToString();
            TextBox5.Text = cmd.Parameters["@PEmployeeSalary"].ToString();
            TextBox6.Text = cmd.Parameters["@PEmployeeDepartmentno"].ToString();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

    }
    protected void btndelete_Click(object sender, EventArgs e)
    {
        try
        {
            cmd = new SqlCommand("DeleteRecord", con);
            cmd.CommandType = CommandType.StoredProcedure;
            P1 = new SqlParameter("@PEmployeeId", SqlDbType.Int);
            cmd.Parameters.AddWithValue("@PEmployeeId", TextBox1.Text);
            P1.Value = Convert.ToInt32(TextBox1.Text);
            cmd.Parameters.Add(P1);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            Console.WriteLine("Record Deleted");
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}



问题是插入工作非常正常,但剩下的操作根本不起作用?请告诉我并提前更正我感谢


the problem is inserting is working very fine but remaining operations are not working at all why? please tell me and correct me thanks in advance

推荐答案

不,我们无法以这种方式工作,没有任何关于确切问题的信息。

请在每次操作完成后调试您的代码,并逐行检查它是否按预期工作。



由于您已经处理了的例外情况尝试Catch Block ,这样您就可以在调试时轻松查看是否有任何异常。



如果一切正常且操作没有执行,那么查询有问题。所以,找到所有问题并纠正它们。



你会发现调试过程非常有趣。
No, we can't work this way, without any information about the exact issue.
Please debug your code while each operation is done and check line by line whether it is working as expected or not.

As you have handled Exceptions by Try Catch Block, so you can easily see if there are any Exceptions or not while debugging.

If everything is fine and operations are not performed, then there is something wrong with the query. So, find all problems and correct them.

You will come to know that the debugging process is very interesting.


这篇关于用于在asp.net中插入,更新,删除,查找操作的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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