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 :
The field that's having the problem is Computername, which I hard-coded to be 'huh'. Any ideas?
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?