2012-10-13 152 views
1

我嘗試LINQ VB.NET但它的工作不插入到數據庫VB.NET - 我不插入到數據庫

此行「.CommandText = sqlAdd」

消息錯誤對象引用未設置爲對象的實例。

Imports System 
Imports System.Configuration 
Imports System.Globalization 
Imports System.Data 
Imports System.Data.SqlClient 
Imports System.Text 

Public Class _1_1Invoice 
    Dim db As New assetDataContext() 
    Dim Conn As SqlConnection 
    Dim cmd As SqlCommand 
    Dim tr As SqlTransaction 
    Dim sb As New StringBuilder() 

Private Sub btSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSave.Click 

     If MessageBox.Show("Do you want insert invoice?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then 
      tr = Conn.BeginTransaction() 

      sb.Remove(0, sb.Length) 
      sb.Append("INSERT INTO tbl_AsInvoice (Poid)") 
      sb.Append(" VALUES (@Poid)") 
      Dim sqlAdd As String = sb.ToString() 

      With cmd 
       .CommandText = sqlAdd 
       .CommandType = CommandType.Text 
       .Connection = Conn 
       .Transaction = tr 
       .Parameters.Clear() 
       .Parameters.Add("@Poid", SqlDbType.VarChar).Value = txtPoid.Text.Trim() 
       .ExecuteNonQuery() 
      End With 
      tr.Commit() 
      MessageBox.Show("Succesfull", "Done", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) 
     End If 

感謝您的時間:)

+2

linq在哪裏? –

回答

1

cmd是什麼。你需要並初始化它:

cmd = New SqlCommand() 
+0

謝謝這是工作:D – nettoon493

0

你沒有初始化cmd因此它是Nothing和它的屬性不能被訪問。您必須創建一個新的SqlCommand並將其分配給cmd

cmd = New SqlCommand() 
With cmd 
    ... 
End With 
+0

謝謝這是工作:D – nettoon493