ผมลอง Debug ดูมันแจ้ง Error แบบนี้อ่ะครับ รบกวนชี้แนะหน่อย พอดีกำลังจะเริ่มหัดเขียน procedure อะครับ
Exception thrown: 'System.InvalidOperationException' in System.Data.dll ("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.")
Code (SQL)
alter procedure P_Emp
@Emp_User as Nvarchar(50), @Emp_Pass as Nvarchar (20)
, @Emp_Name as nvarchar (100), @Emp_Surname as nvarchar (100)
, @Emp_Phone as nvarchar (12), @Emp_Email as nvarchar (50)
, @Emp_Address as Nvarchar (300), @Emp_PostCode as nvarchar (10)
, @pStatus as int output , @pMsg as nvarchar (100) output
, @Emp_ID as int, @Role_ID as int, @P_ID as int
, @Dept_ID as int, @PROVINCE_ID as int, @DisTrict_ID as int, @Amphur_ID as int
, @M_DateRegis as datetime
, @Action as nvarchar (10)
as
begin
if @Action = 'Insert'
begin
begin transaction
begin try
insert into tb_Emp Values (@Emp_ID,@Emp_User, @Emp_Pass, @Emp_Name, @Emp_Surname, @Emp_Phone, @Emp_Email, @Emp_Address, @Emp_PostCode)
insert into tb_Emp Values (@Emp_ID,@Role_ID, @P_ID, @Dept_ID, @PROVINCE_ID, @DisTrict_ID, @Amphur_ID, @M_DateRegis)
set @pStatus = 1
set @pMsg = 'Insert Data Successfuly'
commit
end try
begin Catch
rollback
select @pStatus = 0
select @pMsg = ERROR_MESSAGE();
End Catch
end
end
Code (VB.NET)
Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
Try
Using objConn As New SqlConnection(strConn)
objConn.Open()
Using objtransaction As SqlTransaction = objConn.BeginTransaction(IsolationLevel.ReadCommitted)
Using objcmd As New SqlCommand("P_Emp", objConn)
With objcmd
.CommandType = CommandType.StoredProcedure
.Transaction = trans
.Parameters.AddWithValue("@Action", "Insert")
.Parameters.AddWithValue("@Emp_ID", SqlDbType.NVarChar).Value = lblAutoId.Text
.Parameters.AddWithValue("@Emp_User", SqlDbType.NVarChar).Value = txtUser.Text
.Parameters.AddWithValue("@Emp_Pass", SqlDbType.NVarChar).Value = txtPass.Text
.Parameters.AddWithValue(" @Emp_Name", SqlDbType.NVarChar).Value = txtFirstname.Text
.Parameters.AddWithValue("@Emp_Surname", SqlDbType.NVarChar).Value = txtSurname.Text
.Parameters.AddWithValue(" @Emp_Phone", SqlDbType.NVarChar).Value = txtPhone.Text
.Parameters.AddWithValue("@Emp_Email", SqlDbType.NVarChar).Value = txtEmail.Text
.Parameters.AddWithValue("@Emp_Address", SqlDbType.NVarChar).Value = txtAddress.Text
.Parameters.AddWithValue(" @Emp_PostCode", SqlDbType.NVarChar).Value = txtPostcode.Text
.Parameters.AddWithValue("@Emp_ID", SqlDbType.Int).Value = lblAutoId.Text
.Parameters.AddWithValue("@Role_ID", SqlDbType.Int).Value = ddlRole.SelectedValue.ToString
.Parameters.AddWithValue("@P_ID", SqlDbType.Int).Value = ddlPosition.SelectedValue.ToString
.Parameters.AddWithValue("@Dept_ID", SqlDbType.Int).Value = ddlDept.SelectedValue.ToString
.Parameters.AddWithValue("@PROVINCE_ID", SqlDbType.Int).Value = ddlProvince.SelectedValue.ToString
.Parameters.AddWithValue("@DisTrict_ID", SqlDbType.Int).Value = ddlDistrict.SelectedValue.ToString
.Parameters.AddWithValue("@Amphur_ID", SqlDbType.Int).Value = ddlAumphur.SelectedValue.ToString
.Parameters.AddWithValue("@M_DateRegis", SqlDbType.DateTime).Value = DateTime.Now.ToString(CultureInfo.CreateSpecificCulture("en-US"))
.CommandText = "P_Emp"
.ExecuteNonQuery()
End With
End Using
gentAutoId()
End Using
End Using
Catch ex As Exception
End Try
End Sub
Tag : .NET, Ms SQL Server 2014, Web (ASP.NET), Win (Windows App), VB.NET