2011-12-15 5 views
1

次のコードを使用すると、「初期化文字列の書式がインデックス0から始まる仕様に適合しません」というエラーが表示されます。これらのコマンドは、2つのテーブル、つまり、マスターテーブルとしてのStudentDetails.Studentsと、子テーブルとしてのRegistrationDetails.Registrationの関連レコードを削除することになっています。2つのテーブル(SQLTransactionを使用)から関連するレコードの内容を削除する方法はありますか?

Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click 
    Try 
     Dim cd As String 

     If txtStudentID.Text = "" And txtName.Text = "" And cboDay.Text = "" And cboMonth.Text = "" And txtYear.Text = "" And lblAge.Text = "" And radioMale.Checked = False Or RadioFemale.Checked = False And txtGName.Text = "" And txtPhone.Text = "" And txtEmail.Text = "" And txtAddress.Text = "" And txtTown.Text = "" And cboRegion.Text = "" And PictureBox1.ImageLocation = "" Then 
      MessageBox.Show("There is no record selected to delete. Search for the record to delete.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) 
     Else 
      cd = MessageBox.Show("You are about to delete this record. Are you sure you want to delete?", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) 
      If cd = vbYes Then 
       Using c As New SqlConnection("connection string") 
        c.Open() 
        Using tx As SqlTransaction = c.BeginTransaction() 
         Try 
          Using cmd As SqlCommand = c.CreateCommand() 
           cmd.CommandText = "delete from RegistrationDetails.Registration where StudentId = @studentId" 
           cmd.Parameters.Add("@studentId", SqlDbType.BigInt).Value = txtStudentID.Text 
           cmd.ExecuteNonQuery() 

           cmd.CommandText = "delete from StudentDetails.Students where StudentId = @studentId" 
           cmd.Parameters.Add("@studentId", SqlDbType.BigInt).Value = txtStudentID.Text 
           cmd.ExecuteNonQuery() 

           tx.Commit() 
          End Using 
         Catch generatedExceptionName As Exception 
          tx.Rollback() 
          ' take care of exception here 
          Throw 
         End Try 
        End Using 
       End Using 
       MessageBox.Show("Record deleted", "Deleted", MessageBoxButtons.OK, MessageBoxIcon.Information) 
       showgrid() 
       txtStudentID.Clear() 
       txtName.Clear() 
       cboDay.Text = "" 
       cboMonth.Text = "" 
       txtYear.Clear() 
       lblAge.Text = "" 
       If radioMale.Checked = True Then 
        Gender = "" 
       End If 
       txtGName.Clear() 
       txtPhone.Clear() 
       txtEmail.Clear() 
       txtAddress.Clear() 
       txtTown.Clear() 
       cboRegion.Text = "" 
       PictureBox1.Image = PictureBox1.ErrorImage 
       txtStudentID.Focus() 
      End If 
     End If 
    Catch ex As Exception 
     MessageBox.Show(ex.Message) 
    End Try 
End Sub 

答えて

1

この方法を試すと、問題が解決する可能性があります。

実際には、クエリと同じパラメータを追加するたびに、単一のSqlCommandを使用しています。

ご希望の場合はこちらをご覧ください。

関連する問題