SQL Insert Command

tehon3299

Well-known member
Joined
Jan 6, 2003
Messages
155
Location
Liverpool, NY
Hey -

I was just wondering how I would use the SQL Insert Command on an ASPX page. I know its: INSERT INTO tableName VALUES ______. How do I use the value from a textbox on the page as the insert value?

Thanks
 
Using an SqlCommand and the .ExecuteNonQuery() method, then ...

Code:
"INSERT INTO tableName (field1) VALUES (" & textBox1.text & ")"
 
You can call the function from a button click...
Code:
    Protected Friend Function SetTableValue(ByVal sSql As String) As Integer
pass the INSERT INTO statement as the argument (as sSql)
        Dim cmd As SqlCommand
        Dim con As New SqlConnection("Your connection String")
        Dim recordsAffected As Integer
        Try
            If con.State = ConnectionState.Closed Then con.Open()
            cmd = New SqlCommand(sSql, con)
            recordsAffected = cmd.ExecuteNonQuery()
        Catch 
            recordsAffected = -1
        Finally
            If con.State = ConnectionState.Open Then con.Close()
            If Not cmd Is Nothing Then cmd.Dispose()
        End Try
        Return recordsAffected
    End Function
 
Heres my code. I have it in a button click. Can you see what is wrong? It inserts a blank field in SQL but NOT a NULL. I allow NULLS for the field but I just get a blank field.
 
Back
Top