为什么数据不在数据库表中 [英] Why the data is not going in database table

查看:67
本文介绍了为什么数据不在数据库表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些异常在我的CS代码中给出了

异常

These exceptions are giving in my cs code

Exception

System.InvalidOperationException: ExecuteNonQuery requires the command to have a transaction when the connection assigned to the command is in a pending local transaction. The Transaction property of the command has not been initialized. at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at registration.Button1_Click(Object sender, EventArgs e) in e:\TuitionIndia\registration.aspx.cs:line 67 





我的CS代码在下面给出





My cs code is given below

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;

public partial class registration : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
    {
        if (rbPFaculty.Checked == true)
        {
            ddClass.Enabled = true;
        }
    }
    protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
    {
        if (rbStudent.Checked == true)
        {
            ddClass.Enabled = false;
        }
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string sc1=null;

        if (rbStudent.Checked == true)
        {
            sc1 = "Student";
        }
        if (rbPFaculty.Checked == true)
        {
            sc1 = "Private Faculty";
        }
        if (rbSFaculty.Checked == true)
        {
            sc1 = "School Faculty";
        }

        SqlConnection CON = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["tuitionConnectionString"].ConnectionString);
        SqlTransaction tr = null;
        CON.Open();
        tr = CON.BeginTransaction();
        try
        {
            SqlCommand cmd = new SqlCommand("insert into registration (email,fname,lname,dob,cont,rtype,tclass,board,city,pcode,state) values (@email,@fname,@lname,@dob,@cont,@rtype,@tclass,@board,@city,@pcode,@state)");
            cmd.Parameters.AddWithValue("@email", txtEmail.Text);
            cmd.Parameters.AddWithValue("@fname", txtFname.Text);
            cmd.Parameters.AddWithValue("@lname", txtLname.Text);
            cmd.Parameters.AddWithValue("@dob", txtDob.Text);
            cmd.Parameters.AddWithValue("@cont", txtCno.Text);
            cmd.Parameters.AddWithValue("@rtype",sc1);
            cmd.Parameters.AddWithValue("@tclass", ddClass.SelectedValue);
            cmd.Parameters.AddWithValue("@board", txtFname.Text);
            cmd.Parameters.AddWithValue("@city", txtFname.Text);
            cmd.Parameters.AddWithValue("@pcode", txtFname.Text);
            cmd.Parameters.AddWithValue("@state", txtFname.Text);
            cmd.Connection = CON;
            cmd.ExecuteNonQuery();
            tr.Commit();

        }
        catch (Exception ex)
        {
            tr.Rollback();
        }
        finally
        {
            CON.Close();
        }
    }
}

推荐答案

来自异常消息:
From the exception message :
"The Transaction property of the command has not been initialized"



因此,在执行命令之前:



So, before execution the command :

cmd.Transaction = tr;


希望对您有所帮助(但没​​有尝试过).

干杯


Hope that helps (, did not try it).

Cheers


将您的代码更改为下面的代码(或在catch块中放置一个断点),以查看您得到的异常是什么:
Change your code to the below (or put a break point in the catch block) to see what the exception is you are getting :
...
      catch (Exception ex)
      {
          Response.Write(""+ex);
          tr.Rollback();
      }
...


这篇关于为什么数据不在数据库表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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