SQL Insert problem

delstar

Well-known member
Joined
Jun 9, 2006
Messages
59
Programming Experience
1-3
I'm having an odd problem. I built a simple interface for a database, with a dialog box to add new records. The first time I add a new record, everything works fine. The second time, though, I'm getting an error about a particular field not allowing NULLs. Take a look at the code :
VB.NET:
Public Class DlgNew
    Dim Computer As String = "huh" 'My.Application.CommandLineArgs(0)
    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Try
            FrmMain.SendSQL("BEGIN TRANSACTION;INSERT INTO Tickets(Date, Computername, Technician, Details) VALUES('" + System.DateTime.Now + "','" + Computer + "','" + My.User.Name + "','" + DetailsTextBox.Text + "');COMMIT TRANSACTION")
            FrmMain.RefreshDGV()
        Catch ex As Exception
            MsgBox(ex.ToString)
        Finally
            Me.Close()
        End Try
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub DlgNew_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LblCompName.Text = Computer
        LblTech.Text = My.User.Name
    End Sub
End Class

VB.NET:
    Private Sub BindingNavigatorAddNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorAddNewItem.Click
        Dim NewItem As New DlgNew
        NewItem.ShowDialog()
    End Sub

VB.NET:
    Public Sub SendSQL(ByVal sql As String)
        conn = New SqlConnection(My.Settings.ConnectionString)
        If conn.State <> ConnectionState.Open Then
            conn.Open()
        End If
        While conn.State <> ConnectionState.Open
            Threading.Thread.Sleep(50)
        End While
        cmd = New SqlCommand(sql, conn)
        Try
            While conn.State = ConnectionState.Connecting
            End While
            cmd.ExecuteNonQuery()
        Catch ae As SqlException
            MessageBox.Show(ae.Message.ToString)
        End Try
    End Sub

The field that's having the problem is Computername, which I hard-coded to be 'huh'. Any ideas?
 
It should also be noted that the primary key is not Computername, but a TicketID field set to identity. That part seems to be working fine.
 
OK, it gets even more confusing. After analyzing with a profiler, I've found that the second statement actually does execute, and is no different from the first one. A glance at the database shows that the row does get added.

So, where am I getting the exception from?
 
Get the IDE to tell you, On the Debug menu, choose Exceptions and then put a tick in the Thrown column

Side note, please read thePQ link in my signature

ps, never use + to concatenate strings! Use &
 
Back
Top