vendredi 8 septembre 2017

VB.net to SQLserver INSERT checkbox results

I'm writing a VB.net program that records data to SQLServer2012. At this stage I'm trying to create a new row in the database. The primary key automatically creates a line N+1, and to create the line you enter a new serial number. This all worked the other day, except you could not then load the row into the form to edit the rest of the data because it was tripping up on the nulls.

Anyway, I decided as I created the new row I'll load a blank space " " into all text boxes, today's date into all date boxes and a 'false' return on all check boxes. I've also added code to check for nulls. Below is the code to insert a result into the checkboxes, but I get the error "Incorrect syntax near '='".

Private Sub btnAddNewSerial_Click(sender As System.Object, e As System.EventArgs) Handles btnAdd.Click, btnAddNewSerial.Click
    Try

        con.ConnectionString = (frmAdmin.lblDBConnection.Text & frmAdmin.txtDBPW.Text & frmAdmin.lblDBConnection2.Text)
        con.Open()
        cmd.Connection = con
        cmd.CommandText = "INSERT INTO SLE1000DB (SK2SoakMainPCB) " & _
"VALUES (@SK2SoakMainPCB)”
cmd.Parameters.AddWithValue("@SK2SoakMainPCB", SqlDbType.Bit).Value = 
FrmSLE1000.chkSoakInteruptionsMainPCB.Checked
cmd.ExecuteNonQuery()
        cmd.Parameters.Clear()
        con.Close()
        MsgBox("Data updated")
    Catch ex As Exception
        MessageBox.Show("Error while inserting record on table..." & ex.Message, "Insert Records")
    Finally
        con.Close()
    End Try

    Me.Hide()
    frmSelect.Show()
End Sub

So if anyone can see whats causing the syntax error or can tell me a neater way of causing the checkbox to return a false result (I know that at the moment if it did work it would return a true result, that's something I'm still thinking about) Or just an overall neater way to achieve my goal that would be much appreciated. All my knowlage of VB.net and SQLserver comes from a book so is therefore, unfortunately, pretty limited!

The code presented here is just a snippet, there's actually an awful lot more columns that I'm passing data to, but I believe that its the checkboxes that are causing the syntax error as there aren't any "=" anywhere else in this bit of code and using a step through its this section that it gets caught up on.

Anyway thanks for your help!

Aucun commentaire:

Enregistrer un commentaire